check on already used email

This commit is contained in:
OxSaitama 2023-10-19 14:43:44 +02:00
parent d84d5aafd2
commit 1c175ac816
3 changed files with 139 additions and 6 deletions

View File

@ -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<Customer>(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);

View File

@ -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);

View File

@ -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<Customer>(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);