lecoffre-back/src/app/api/admin/CustomersController.ts
2023-10-11 19:17:56 +02:00

165 lines
5.0 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/admin/CustomersService/CustomersService";
import { Service } from "typedi";
import { Customer } from "le-coffre-resources/dist/Admin";
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/admin/customers", [authHandler, roleHandler, ruleHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query;
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
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/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
await validateOrReject(customerEntity, { groups: ["createCustomer"], forbidUnknownValues: false });
//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;
}
//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.log(error);
this.httpValidationError(response, error);
return;
}
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific customer by uid
*/
@Get("/api/v1/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;
}
//get query
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;
}
}
}