Skip to content

Commit

Permalink
Add delete address and test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevariable committed May 5, 2024
1 parent b45933d commit 1897acd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/action/address/delete-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Address, User } from "@prisma/client";
import GetAddress from "./get-address";
import { prismaClient } from "../../application/database";

export default class DeleteAddress {
static async execute(user: User, addressId: bigint, contactId: bigint): Promise<Address> {
const address = await GetAddress.execute(user, addressId, contactId)

const deletedAddress = await prismaClient.address.delete({
where: {
id: address.id
}
})

return deletedAddress
}
}
15 changes: 15 additions & 0 deletions src/controller/address-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import UserRequest from '../request/user-request'
import { CreateAddressRequest, toAddressResponse } from '../model/address-model'
import GetAddress from '../action/address/get-address'
import UpdateAddress from '../action/address/update-address'
import DeleteAddress from '../action/address/delete-address'

export default class AddressController {
static async create(req: UserRequest, res: Response, next: NextFunction) {
Expand Down Expand Up @@ -49,4 +50,18 @@ export default class AddressController {
next(e)
}
}

static async delete(req: UserRequest, res: Response, next: NextFunction) {
try {
const addressId = BigInt(req.params.addressId)

const contactId = BigInt(req.params.contactId)

const response = await DeleteAddress.execute(req.user !, addressId, contactId)

res.json(toAddressResponse(response)).status(200)
} catch (e: unknown) {
next(e)
}
}
}
5 changes: 5 additions & 0 deletions src/route/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ apiRouter.put(
'/api/contacts/:contactId/addresses/:addressId',
AddressController.update
)

apiRouter.delete(
'/api/contacts/:contactId/addresses/:addressId',
AddressController.delete
)
33 changes: 33 additions & 0 deletions tests/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,36 @@ describe('PUT /api/contacts/:contactId/addresses/:addressId', () => {
expect(body.data.postal_code).toBe(request.postal_code)
})
})

describe('DELETE: /api/contacts/:contactId/addresses/:addressId', () => {
it('can delete contact with specific id', async () => {
const user = await createUser()

const contact = await createContact(user)

const address = await createAddress(user, contact)

const response = await supertest(web)
.delete(`/api/contacts/${contact.id}/addresses/${address.id}`)
.set('X-API-TOKEN', user.token!)
.send()

const body: Response<AddressResponse> = response.body

expect(response.status).toBe(200)
expect(BigInt(body.data.id)).toBe(address.id)
})

it('cant delete contact with incorrect id', async () => {
const user = await createUser()

const contact = await createContact(user)

const response = await supertest(web)
.delete(`/api/contacts/${contact.id}/addresses/0`)
.set('X-API-TOKEN', user.token!)
.send()

expect(response.status).toBe(404)
})
})

0 comments on commit 1897acd

Please sign in to comment.