check on already used email
This commit is contained in:
parent
d84d5aafd2
commit
1c175ac816
@ -50,17 +50,38 @@ export default class CustomersController extends ApiController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Create a new customer
|
* @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) {
|
protected async post(req: Request, response: Response) {
|
||||||
try {
|
try {
|
||||||
//init IUser resource with request body values
|
//init IUser resource with request body values
|
||||||
const customerEntity = Customer.hydrate<Customer>(req.body);
|
const customerEntity = Customer.hydrate<Customer>(req.body);
|
||||||
//validate user
|
//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
|
//call service to get prisma entity
|
||||||
const customerEntityCreated = await this.customersService.create(customerEntity);
|
const customerEntityCreated = await this.customersService.create(customerEntity);
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
@ -106,6 +127,31 @@ export default class CustomersController extends ApiController {
|
|||||||
this.httpValidationError(response, error);
|
this.httpValidationError(response, error);
|
||||||
return;
|
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
|
//call service to get prisma entity
|
||||||
try {
|
try {
|
||||||
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
||||||
|
@ -63,6 +63,22 @@ export default class CustomersController extends ApiController {
|
|||||||
this.httpValidationError(response, error);
|
this.httpValidationError(response, error);
|
||||||
return;
|
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
|
//call service to get prisma entity
|
||||||
const customerEntityCreated = await this.customersService.create(customerEntity);
|
const customerEntityCreated = await this.customersService.create(customerEntity);
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
@ -108,6 +124,31 @@ export default class CustomersController extends ApiController {
|
|||||||
this.httpValidationError(response, error);
|
this.httpValidationError(response, error);
|
||||||
return;
|
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
|
//call service to get prisma entity
|
||||||
try {
|
try {
|
||||||
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
||||||
|
@ -50,17 +50,38 @@ export default class CustomersController extends ApiController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Create a new customer
|
* @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) {
|
protected async post(req: Request, response: Response) {
|
||||||
try {
|
try {
|
||||||
//init IUser resource with request body values
|
//init IUser resource with request body values
|
||||||
const customerEntity = Customer.hydrate<Customer>(req.body);
|
const customerEntity = Customer.hydrate<Customer>(req.body);
|
||||||
//validate user
|
//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
|
//call service to get prisma entity
|
||||||
const customerEntityCreated = await this.customersService.create(customerEntity);
|
const customerEntityCreated = await this.customersService.create(customerEntity);
|
||||||
//Hydrate ressource with prisma entity
|
//Hydrate ressource with prisma entity
|
||||||
@ -94,9 +115,9 @@ export default class CustomersController extends ApiController {
|
|||||||
this.httpNotFoundRequest(response, "user not found");
|
this.httpNotFoundRequest(response, "user not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
req.body.contact.uid = userFound.contact_uid;
|
req.body.contact.uid = userFound.contact_uid;
|
||||||
|
|
||||||
//init IUser resource with request body values
|
//init IUser resource with request body values
|
||||||
const customerEntity = Customer.hydrate<Customer>(req.body);
|
const customerEntity = Customer.hydrate<Customer>(req.body);
|
||||||
//validate user
|
//validate user
|
||||||
@ -106,6 +127,31 @@ export default class CustomersController extends ApiController {
|
|||||||
this.httpValidationError(response, error);
|
this.httpValidationError(response, error);
|
||||||
return;
|
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
|
//call service to get prisma entity
|
||||||
try {
|
try {
|
||||||
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
const customerEntityUpdated = await this.customersService.update(uid, customerEntity);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user