Skip to content

Commit

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

describe("POST /contacts/:contactId/addresses", () => {
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();
});

const addressUpdated = {
street: "Test Street Updated",
city: "Test City Updated",
province: "Test Province Updated",
country: "Test Country Updated",
postal_code: "123450"
};

it("should return 200 - success updating address with empty street, city, and province", async () => {
const res = await supertest(web)
.put(`${basePath}/contacts/${contact.id}/addresses/${address.id}`)
.send({
country: AddressTestUtil.address.country + " recently updated",
postal_code: AddressTestUtil.address.postal_code + "9",
})
.set('Authorization', `Bearer ${token}`);

logger.info(res.body);
expect(res.status).toBe(200);
expect(res.body.data.street).toBe(AddressTestUtil.address.street);
expect(res.body.data.city).toBe(AddressTestUtil.address.city);
expect(res.body.data.province).toBe(AddressTestUtil.address.province);
expect(res.body.data.country).toBe(AddressTestUtil.address.country + " recently updated");
expect(res.body.data.postal_code).toBe(AddressTestUtil.address.postal_code + "9");
});

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

logger.info(res.body);
expect(res.status).toBe(200);
expect(res.body.data.street).toBe(addressUpdated.street);
expect(res.body.data.city).toBe(addressUpdated.city);
expect(res.body.data.province).toBe(addressUpdated.province);
expect(res.body.data.country).toBe(addressUpdated.country);
expect(res.body.data.postal_code).toBe(addressUpdated.postal_code);
});

it("should return 400 - invalid postal code", async () => {
const res = await supertest(web)
.put(`${basePath}/contacts/${contact.id}/addresses/${address.id}`)
.send({
country: addressUpdated.country,
postal_code: "123"
})
.set('Authorization', `Bearer ${token}`);

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

it("should return 404 - contact doesn't exists", async () => {
const res = await supertest(web)
.put(`${basePath}/contacts/${1000}/addresses/${address.id}`)
.send({
country: addressUpdated.country,
postal_code: addressUpdated.postal_code,
})
.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)
.put(`${basePath}/contacts/${contact.id}/addresses/${1000}`)
.send({
country: addressUpdated.country,
postal_code: addressUpdated.postal_code,
})
.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)
.put(`${basePath}/contacts/${contact.id}/addresses/${address.id}`)
.send({
country: addressUpdated.country,
postal_code: addressUpdated.postal_code,
});

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

class AddressTestUtil {
static user = {
name: "user_test_address",
Expand All @@ -169,7 +283,7 @@ class AddressTestUtil {
province: "Test Province",
country: "Test Country",
postal_code: "123456",
}
};

static async createUser() {
await prisma.user.create({
Expand Down
24 changes: 24 additions & 0 deletions src/controller/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,28 @@ export class AddressController {
next(e);
}
}

static async update(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, "contact doesn't exists");
}

const updateReq = req.body as AddressRequest;
const contactId = Number(req.params.contactId);
const addressId = Number(req.params.addressId);

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

return res.status(200)
.json({
data: updateRes
});
} 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 @@ -7,6 +7,7 @@ export const getAddressRouter = (basePath: string) => {
addressRouter.use(accessValidation);
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);

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

return toAddressResponse(address);
}

static async update(username: string, contactId: number, addressId: number, req: AddressRequest) {
const validatedReq = Validation.validate(AddressValidation.SAVE, req);

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");
}

const address = await prisma.address.update({
where: {
id: addressId,
contact_id: contactId,
},
data: validatedReq
});

return toAddressResponse(address);
}
}

0 comments on commit 6e422bd

Please sign in to comment.