diff --git a/src/app/api/admin/CustomersController.ts b/src/app/api/admin/CustomersController.ts index 5f9ed4ec..c0aa1c49 100644 --- a/src/app/api/admin/CustomersController.ts +++ b/src/app/api/admin/CustomersController.ts @@ -50,17 +50,38 @@ export default class CustomersController extends ApiController { } } + /** * @description Create a new customer */ - @Post("/api/v1/admin/customers", [authHandler, ruleHandler]) + @Post("/api/v1/notary/customers", [authHandler, ruleHandler]) protected async post(req: Request, response: Response) { try { //init IUser resource with request body values const customerEntity = Customer.hydrate(req.body); //validate user - await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); + try { + await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); + } catch (error) { + this.httpValidationError(response, error); + return; + } + const customers = await this.customersService.get({ + where: { + contact: { email: customerEntity.contact?.email }, + office_folders: { + some: { + office_uid: req.body.user.office_Id, + }, + }, + }, + }); + + if (customers.length > 0) { + this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]); + return; + } //call service to get prisma entity const customerEntityCreated = await this.customersService.create(customerEntity); //Hydrate ressource with prisma entity @@ -106,6 +127,31 @@ export default class CustomersController extends ApiController { this.httpValidationError(response, error); return; } + + const customers = await this.customersService.get({ + where: { + contact: { email: customerEntity.contact?.email }, + office_folders: { + some: { + office_uid: req.body.user.office_Id, + }, + }, + }, + }); + + if (customers.length != 0) { + try { + customers.forEach((customer) => { + if (customer.uid != uid) { + throw new Error("email déjà utilisé"); + } + }); + } catch (error) { + this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]); + return; + } + } + //call service to get prisma entity try { const customerEntityUpdated = await this.customersService.update(uid, customerEntity); diff --git a/src/app/api/notary/CustomersController.ts b/src/app/api/notary/CustomersController.ts index 69b6bb7f..349229cf 100644 --- a/src/app/api/notary/CustomersController.ts +++ b/src/app/api/notary/CustomersController.ts @@ -63,6 +63,22 @@ export default class CustomersController extends ApiController { this.httpValidationError(response, error); return; } + + const customers = await this.customersService.get({ + where: { + contact: { email: customerEntity.contact?.email }, + office_folders: { + some: { + office_uid: req.body.user.office_Id, + }, + }, + }, + }); + + if (customers.length > 0) { + this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]); + return; + } //call service to get prisma entity const customerEntityCreated = await this.customersService.create(customerEntity); //Hydrate ressource with prisma entity @@ -108,6 +124,31 @@ export default class CustomersController extends ApiController { this.httpValidationError(response, error); return; } + + const customers = await this.customersService.get({ + where: { + contact: { email: customerEntity.contact?.email }, + office_folders: { + some: { + office_uid: req.body.user.office_Id, + }, + }, + }, + }); + + if (customers.length != 0) { + try { + customers.forEach((customer) => { + if (customer.uid != uid) { + throw new Error("email déjà utilisé"); + } + }); + } catch (error) { + this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]); + return; + } + } + //call service to get prisma entity try { const customerEntityUpdated = await this.customersService.update(uid, customerEntity); diff --git a/src/app/api/super-admin/CustomersController.ts b/src/app/api/super-admin/CustomersController.ts index b3402942..05ef2c6a 100644 --- a/src/app/api/super-admin/CustomersController.ts +++ b/src/app/api/super-admin/CustomersController.ts @@ -50,17 +50,38 @@ export default class CustomersController extends ApiController { } } + /** * @description Create a new customer */ - @Post("/api/v1/super-admin/customers", [authHandler, roleHandler, ruleHandler]) + @Post("/api/v1/notary/customers", [authHandler, ruleHandler]) protected async post(req: Request, response: Response) { try { //init IUser resource with request body values const customerEntity = Customer.hydrate(req.body); //validate user - await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); + try { + await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false }); + } catch (error) { + this.httpValidationError(response, error); + return; + } + const customers = await this.customersService.get({ + where: { + contact: { email: customerEntity.contact?.email }, + office_folders: { + some: { + office_uid: req.body.user.office_Id, + }, + }, + }, + }); + + if (customers.length > 0) { + this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]); + return; + } //call service to get prisma entity const customerEntityCreated = await this.customersService.create(customerEntity); //Hydrate ressource with prisma entity @@ -94,9 +115,9 @@ export default class CustomersController extends ApiController { this.httpNotFoundRequest(response, "user not found"); return; } - + req.body.contact.uid = userFound.contact_uid; - + //init IUser resource with request body values const customerEntity = Customer.hydrate(req.body); //validate user @@ -106,6 +127,31 @@ export default class CustomersController extends ApiController { this.httpValidationError(response, error); return; } + + const customers = await this.customersService.get({ + where: { + contact: { email: customerEntity.contact?.email }, + office_folders: { + some: { + office_uid: req.body.user.office_Id, + }, + }, + }, + }); + + if (customers.length != 0) { + try { + customers.forEach((customer) => { + if (customer.uid != uid) { + throw new Error("email déjà utilisé"); + } + }); + } catch (error) { + this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]); + return; + } + } + //call service to get prisma entity try { const customerEntityUpdated = await this.customersService.update(uid, customerEntity);