218 lines
6.4 KiB
TypeScript
218 lines
6.4 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get, Post, Put } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import CustomersService from "@Services/super-admin/CustomersService/CustomersService";
|
|
import { Service } from "typedi";
|
|
import { Customer } from "le-coffre-resources/dist/SuperAdmin";
|
|
import { validateOrReject } from "class-validator";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
import roleHandler from "@App/middlewares/RolesHandler";
|
|
import { Prisma } from "@prisma/client";
|
|
import customerHandler from "@App/middlewares/OfficeMembershipHandlers/CustomerHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class CustomersController extends ApiController {
|
|
constructor(private customersService: CustomersService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all customers
|
|
*/
|
|
@Get("/api/v1/super-admin/customers", [authHandler, roleHandler, ruleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query: Prisma.CustomersFindManyArgs = {};
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
if (query.where?.uid) {
|
|
this.httpBadRequest(response, "You can't filter by uid");
|
|
return;
|
|
}
|
|
}
|
|
|
|
const officeId: string = req.body.user.office_Id;
|
|
if (query.where?.office_folders?.some?.office_uid) delete query.where.office_folders.some.office_uid;
|
|
if (query.where?.office_folders?.some?.office?.uid) delete query.where?.office_folders?.some?.office?.uid;
|
|
const customerWhereInput: Prisma.CustomersWhereInput = { ...query.where, office_folders: { some: { office_uid: officeId } } };
|
|
query.where = customerWhereInput;
|
|
|
|
//call service to get prisma entity
|
|
const customersEntities = await this.customersService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const customers = Customer.hydrateArray<Customer>(customersEntities, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, customers);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Create a new customer
|
|
*/
|
|
@Post("/api/v1/super-admin/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
|
|
try {
|
|
await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false });
|
|
} catch (error) {
|
|
this.httpValidationError(response, error);
|
|
return;
|
|
}
|
|
|
|
if (!customerEntity.contact?.cell_phone_number) 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
|
|
const customer = Customer.hydrate<Customer>(customerEntityCreated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpCreated(response, customer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Modify a specific customer by uid
|
|
*/
|
|
@Put("/api/v1/notary/customers/:uid", [authHandler, ruleHandler, customerHandler])
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const userFound = await this.customersService.getByUid(uid);
|
|
|
|
if (!userFound) {
|
|
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<Customer>(req.body);
|
|
//validate user
|
|
try {
|
|
await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false });
|
|
} catch (error) {
|
|
this.httpValidationError(response, error);
|
|
return;
|
|
}
|
|
|
|
if (customerEntity.contact?.email) {
|
|
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);
|
|
//Hydrate ressource with prisma entity
|
|
const customer = Customer.hydrate<Customer>(customerEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, customer);
|
|
} catch (error) {
|
|
console.error(error);
|
|
this.httpValidationError(response, error);
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific customer by uid
|
|
*/
|
|
@Get("/api/v1/super-admin/customers/:uid", [authHandler, roleHandler, ruleHandler, customerHandler])
|
|
protected async getOneByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
let query;
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
}
|
|
|
|
const customerEntity = await this.customersService.getByUid(uid, query);
|
|
|
|
if (!customerEntity) {
|
|
this.httpNotFoundRequest(response, "customer not found");
|
|
return;
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const customer = Customer.hydrate<Customer>(customerEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, customer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|