import { Response, Request } from "express"; import { Controller, Get } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficeFoldersService from "@Services/customer/OfficeFoldersService/OfficeFoldersService"; import { Service } from "typedi"; import { OfficeFolders, Prisma } from "@prisma/client"; import { OfficeFolder } from "le-coffre-resources/dist/Customer"; import { OfficeFolder as OfficeFolderNotary } from "le-coffre-resources/dist/Notary"; import officeFolderHandler from "@App/middlewares/CustomerHandler/FolderHandler"; import authHandler from "@App/middlewares/AuthHandler"; @Controller() @Service() export default class OfficeFoldersController extends ApiController { constructor(private officeFoldersService: OfficeFoldersService) { super(); } /** * @description Get all folders */ @Get("/api/v1/customer/folders", [authHandler]) protected async get(req: Request, response: Response) { try { //get query let query: Prisma.OfficeFoldersFindManyArgs = {}; 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 email: string = req.body.user.email; if (!email) { this.httpBadRequest(response, "Missing customer email"); return; } if (query.where?.customers) delete query.where.customers; const officeFolderWhereInput: Prisma.OfficeFoldersWhereInput = { ...query.where, customers: { some: { contact: { email: email } } }, }; query.where = officeFolderWhereInput; if (query.include) delete query.include; query.include = { customers: { include: { contact: true } } }; //call service to get prisma entity const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query); //Hydrate ressource with prisma entity const officeFolders = OfficeFolder.hydrateArray(officeFolderEntities, { strategy: "excludeAll", }); officeFolders.forEach((officeFolder) => { officeFolder.customers = officeFolder.customers!.filter((customer) => customer.contact?.email === email); }); //success this.httpSuccess(response, officeFolders); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Get a specific folder by uid * @returns IFolder */ @Get("/api/v1/customer/folders/:uid", [authHandler, officeFolderHandler]) protected async getOneByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const email: string = req.body.user.email; let query; if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); if (query?.customers) delete query.customers; query.customers = { include: { contact: true } }; } const officeFolderEntity = await this.officeFoldersService.getByUid(uid, query); if (!officeFolderEntity) { this.httpNotFoundRequest(response, "folder not found"); return; } //Hydrate ressource with prisma entity const officeFolder = OfficeFolderNotary.hydrate(officeFolderEntity); if(officeFolder.customers) { officeFolder.customers = officeFolder.customers!.filter((customer) => customer.contact?.email === email); } //success this.httpSuccess(response, officeFolder); } catch (error) { this.httpInternalError(response, error); return; } } }