Skip to content

Commit

Permalink
feat: address delete
Browse files Browse the repository at this point in the history
  • Loading branch information
n9mi committed Sep 17, 2024
1 parent 6e422bd commit 055cef4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
50 changes: 49 additions & 1 deletion __tests__/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe("POST /contacts/:contactId/addresses", () => {
});
});

describe("POST /contacts/:contactId/addresses", () => {
describe("PUT /contacts/:contactId/addresses/:addressId", () => {
let token: string = "";
let contact: Contact = {} as Contact;
let address: Address = {} as Address;
Expand Down Expand Up @@ -263,6 +263,54 @@ describe("POST /contacts/:contactId/addresses", () => {
});
});

describe("DELETE /contacts/:contactId/addresses/:addressId", () => {
let token: string = "";
let contact: Contact = {} as Contact;
let address: Address = {} as Address;

beforeAll(async () => {
token = await AddressTestUtil.getToken();
contact = await AddressTestUtil.createContact();
address = await AddressTestUtil.createAddress(contact.id);
});

afterAll(async () => {
await AddressTestUtil.deleteAddress();
await AddressTestUtil.deleteContact();
await AddressTestUtil.deleteUser();
});

it("should return 200 - success deleting an address", async () => {
const res = await supertest(web)
.delete(`${basePath}/contacts/${contact.id}/addresses/${address.id}`)
.set('Authorization', `Bearer ${token}`);

logger.info(res.body);
expect(res.status).toBe(200);
expect(res.body.status).toBe(true);
});

it("should return 404 - contact doesn't exists", async () => {
const res = await supertest(web)
.delete(`${basePath}/contacts/${1000}/addresses/${address.id}`)
.set('Authorization', `Bearer ${token}`);

logger.info(res.body);
expect(res.status).toBe(404);
expect(res.body.errors).toBeDefined();
});

it("should return 404 - address doesn't exists", async () => {
const res = await supertest(web)
.delete(`${basePath}/contacts/${contact.id}/addresses/${1000}`)
.set('Authorization', `Bearer ${token}`);

logger.info(res.body);
expect(res.status).toBe(404);
expect(res.body.errors).toBeDefined();
});
})

class AddressTestUtil {
static user = {
name: "user_test_address",
Expand Down
28 changes: 25 additions & 3 deletions src/controller/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AddressController {

const createRes = await AddressService.create(res.locals.user.usename, contactId, createReq);

return res.status(200)
res.status(200)
.json({
data: createRes
});
Expand All @@ -50,7 +50,7 @@ export class AddressController {
throw new ResponseError(404, "contact doesn't exists");
}
if (isNaN(Number(req.params.addressId)) || Number(req.params.addressId) < 1) {
throw new ResponseError(404, "contact doesn't exists");
throw new ResponseError(404, "address doesn't exists");
}

const updateReq = req.body as AddressRequest;
Expand All @@ -59,12 +59,34 @@ export class AddressController {

const updateRes = await AddressService.update(res.locals.user.usename, contactId, addressId, updateReq);

return res.status(200)
res.status(200)
.json({
data: updateRes
});
} catch (e) {
next(e);
}
}

static async delete(req: Request, res: Response, next: NextFunction) {
try {
if (isNaN(Number(req.params.contactId)) || Number(req.params.contactId) < 1) {
throw new ResponseError(404, "contact doesn't exists");
}
if (isNaN(Number(req.params.addressId)) || Number(req.params.addressId) < 1) {
throw new ResponseError(404, "address doesn't exists");
}
const contactId = Number(req.params.contactId);
const addressId = Number(req.params.addressId);

const deleteRes = await AddressService.delete(res.locals.user.username, contactId, addressId);

res.status(200)
.json({
status: deleteRes
});
} catch (e) {
next(e);
}
}
}
1 change: 1 addition & 0 deletions src/router/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const getAddressRouter = (basePath: string) => {
addressRouter.get(`${basePath}/contacts/:contactId/addresses`, AddressController.getAll);
addressRouter.post(`${basePath}/contacts/:contactId/addresses`, AddressController.create);
addressRouter.put(`${basePath}/contacts/:contactId/addresses/:addressId`, AddressController.update);
addressRouter.delete(`${basePath}/contacts/:contactId/addresses/:addressId`, AddressController.delete);

return addressRouter;
}
31 changes: 31 additions & 0 deletions src/service/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,35 @@ export default class AddressService {

return toAddressResponse(address);
}

static async delete(username: string, contactId: number, addressId: number): Promise<boolean> {
const isContactExists = await prisma.contact.count({
where: {
id: contactId,
username: username
}
}) === 1;
if (!isContactExists) {
throw new ResponseError(404, "contact doesn't exists");
}

const isAddressExists = await prisma.address.count({
where: {
id: addressId,
contact_id: contactId,
}
}) === 1;
if (!isAddressExists) {
throw new ResponseError(404, "address doesn't exists");
}

await prisma.address.delete({
where: {
id: addressId,
contact_id: contactId,
}
});

return true;
}
}

0 comments on commit 055cef4

Please sign in to comment.