From 027c1af841899f9fe7a9dbcb06d60d89ace9f565 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Tue, 10 Nov 2020 12:25:18 +0200 Subject: [PATCH] fix: customer type. --- .../src/api/controllers/Contacts/Customers.ts | 42 +++++++++++++++---- .../src/services/Contacts/CustomersService.ts | 6 ++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/server/src/api/controllers/Contacts/Customers.ts b/server/src/api/controllers/Contacts/Customers.ts index 5f12cc1ae..0b14944c0 100644 --- a/server/src/api/controllers/Contacts/Customers.ts +++ b/server/src/api/controllers/Contacts/Customers.ts @@ -77,7 +77,11 @@ export default class CustomersController extends ContactsController { */ get customerDTOSchema() { return [ - check('customer_type').exists().trim().escape(), + check('customer_type') + .exists() + .isIn(['business', 'individual']) + .trim() + .escape(), ]; } @@ -121,7 +125,11 @@ export default class CustomersController extends ContactsController { try { const contact = await this.customersService.newCustomer(tenantId, contactDTO); - return res.status(200).send({ id: contact.id }); + + return res.status(200).send({ + id: contact.id, + message: 'The customer has been created successfully.', + }); } catch (error) { next(error); } @@ -140,7 +148,11 @@ export default class CustomersController extends ContactsController { try { await this.customersService.editCustomer(tenantId, contactId, contactDTO); - return res.status(200).send({ id: contactId }); + + return res.status(200).send({ + id: contactId, + message: 'The customer has been edited successfully.', + }); } catch (error) { next(error); } @@ -176,7 +188,10 @@ export default class CustomersController extends ContactsController { try { const customer = await this.customersService.getCustomer(tenantId, contactId); - return res.status(200).send({ customer }); + + return res.status(200).send({ + customer: this.transfromToResponse(customer), + }); } catch (error) { next(error); } @@ -221,10 +236,14 @@ export default class CustomersController extends ContactsController { } try { - const { customers, pagination, filterMeta } = await this.customersService.getCustomersList(tenantId, filter); + const { + customers, + pagination, + filterMeta, + } = await this.customersService.getCustomersList(tenantId, filter); return res.status(200).send({ - customers, + customers: this.transfromToResponse(customers), pagination: this.transfromToResponse(pagination), filter_meta: this.transfromToResponse(filterMeta), }); @@ -242,19 +261,24 @@ export default class CustomersController extends ContactsController { */ handlerServiceErrors(error: Error, req: Request, res: Response, next: NextFunction) { if (error instanceof ServiceError) { + if (error.errorType === 'contact_not_found') { + return res.boom.badRequest(null, { + errors: [{ type: 'CUSTOMER.NOT.FOUND', code: 100 }], + }); + } if (error.errorType === 'contacts_not_found') { return res.boom.badRequest(null, { - errors: [{ type: 'CUSTOMERS.NOT.FOUND', code: 100 }], + errors: [{ type: 'CUSTOMERS.NOT.FOUND', code: 200 }], }); } if (error.errorType === 'some_customers_have_invoices') { return res.boom.badRequest(null, { - errors: [{ type: 'SOME.CUSTOMERS.HAVE.SALES_INVOICES', code: 200 }], + errors: [{ type: 'SOME.CUSTOMERS.HAVE.SALES_INVOICES', code: 300 }], }); } if (error.errorType === 'customer_has_invoices') { return res.boom.badRequest(null, { - errors: [{ type: 'CUSTOMER.HAS.SALES_INVOICES', code: 200 }], + errors: [{ type: 'CUSTOMER.HAS.SALES_INVOICES', code: 400 }], }); } } diff --git a/server/src/services/Contacts/CustomersService.ts b/server/src/services/Contacts/CustomersService.ts index 089de0612..e26faf14f 100644 --- a/server/src/services/Contacts/CustomersService.ts +++ b/server/src/services/Contacts/CustomersService.ts @@ -45,7 +45,9 @@ export default class CustomersService { * @param {ICustomerNewDTO|ICustomerEditDTO} customerDTO * @returns {IContactDTO} */ - private customerToContactDTO(customerDTO: ICustomerNewDTO|ICustomerEditDTO): IContactNewDTO|IContactEditDTO { + private customerToContactDTO( + customerDTO: ICustomerNewDTO | ICustomerEditDTO, + ): IContactNewDTO | IContactEditDTO { return { ...omit(customerDTO, ['customerType']), contactType: customerDTO.customerType, @@ -69,7 +71,7 @@ export default class CustomersService { private transformContactToCustomer(contactModel: IContact) { return { ...omit(contactModel, ['contactService', 'contactType']), - customerType: contactModel.contactService, + customerType: contactModel.contactType, }; }