Skip to content

Commit

Permalink
feat: delete contact
Browse files Browse the repository at this point in the history
  • Loading branch information
n9mi committed Sep 16, 2024
1 parent 46660fc commit 6e0b8fe
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
44 changes: 44 additions & 0 deletions __tests__/contact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,50 @@ describe("PUT /contacts/:id", () => {
});
});

describe("DELETE /contacts/:id", () => {
let token: string = "";
let createdContact: Contact = {} as Contact;

beforeAll(async () => {
token = await ContactTestUtil.getToken();
createdContact = await ContactTestUtil.createContact();
});

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

it("should return 200 - success deleting a contact", async () => {
const res = await supertest(web)
.delete(`${basePath}/contacts/${createdContact.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`)
.set('Authorization', `Bearer ${token}`);

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

it("should return 401 - empty authorization", async () => {
const res = await supertest(web)
.delete(`${basePath}/contacts/${createdContact.id}`);

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

class ContactTestUtil {
static user = {
name: "user_test_contact",
Expand Down
16 changes: 16 additions & 0 deletions src/controller/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,20 @@ export class ContactController {
next(e);
}
}

static async delete(req: Request, res: Response, next: NextFunction) {
try {
if (isNaN(Number(req.params.id))) {
throw new ResponseError(404, "contact doesn't exists");
}
const deleteRes = await ContactService.delete(res.locals.username, Number(req.params.id));

res.status(200)
.json({
status: deleteRes
});
} catch (e) {
next(e);
}
}
}
2 changes: 2 additions & 0 deletions src/router/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const getContactRouter = (basePath: string) => {
contactRouter.get(`${basePath}/contacts/:id`, ContactController.getById);
contactRouter.post(`${basePath}/contacts`, ContactController.create);
contactRouter.put(`${basePath}/contacts/:id`, ContactController.update);
contactRouter.delete(`${basePath}/contacts/:id`, ContactController.delete);


return contactRouter;
}
21 changes: 21 additions & 0 deletions src/service/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,25 @@ export default class ContactService {

return toContactResponse(contactUpdated);
}

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

await prisma.contact.delete({
where: {
id: id,
username: username
}
});

return true;
}
}

0 comments on commit 6e0b8fe

Please sign in to comment.