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; 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(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, roleHandler, 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 }); //call service to get prisma entity const customerEntityCreated = await this.customersService.create(customerEntity); //Hydrate ressource with prisma entity const customer = Customer.hydrate(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/super-admin/customers/:uid", [authHandler, roleHandler, 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; } //init IUser resource with request body values const customerEntity = Customer.hydrate(req.body); //validate user await validateOrReject(customerEntity, { groups: ["updateCustomer"], forbidUnknownValues: false }); //call service to get prisma entity const customerEntityUpdated = await this.customersService.update(uid, customerEntity); //Hydrate ressource with prisma entity const customer = Customer.hydrate(customerEntityUpdated, { strategy: "excludeAll", }); //success this.httpSuccess(response, customer); } 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(customerEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, customer); } catch (error) { this.httpInternalError(response, error); return; } } }