import { Response, Request } from "express"; import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; import { Service } from "typedi"; import { OfficeFolders, Prisma } from "@prisma/client"; import { OfficeFolder } from "le-coffre-resources/dist/Notary"; import { validateOrReject } from "class-validator"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; @Controller() @Service() export default class OfficeFoldersController extends ApiController { constructor(private officeFoldersService: OfficeFoldersService) { super(); } /** * @description Get all folders */ @Get("/api/v1/notary/folders", [authHandler, ruleHandler]) 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 (req.query["search"] && typeof req.query["search"] === "string") { const filter = req.query["search"]; query = { where: { OR: [ { name: { contains: filter, mode: "insensitive" }, }, { folder_number: { contains: filter, mode: "insensitive" }, }, { customers: { some: { contact: { OR: [ { first_name: { contains: filter, mode: "insensitive" } }, { last_name: { contains: filter, mode: "insensitive" } }, ], }, }, }, }, ], }, }; } const officeId: string = req.body.user.office_Id; const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; if(!query.where) query.where = { office: officeWhereInput}; query.where.office = officeWhereInput; //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", }); //success this.httpSuccess(response, officeFolders); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Create a new folder */ @Post("/api/v1/notary/folders", [authHandler, ruleHandler, folderHandler]) protected async post(req: Request, response: Response) { try { //init OfficeFolder resource with request body values const officeFolderRessource = OfficeFolder.hydrate(req.body); await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false }); //call service to get prisma entity const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource); //Hydrate ressource with prisma entity const officeFolders = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll", }); //success this.httpCreated(response, officeFolders); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Modify a specific folder by uid */ @Put("/api/v1/notary/folders/:uid", [authHandler, ruleHandler, folderHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const officeFolderFound = await this.officeFoldersService.getByUid(uid); if (!officeFolderFound) { this.httpNotFoundRequest(response, "office folder not found"); return; } //init OfficeFolder resource with request body values const officeFolderEntity = OfficeFolder.hydrate(req.body); //validate folder await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false }); //call service to get prisma entity const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity); //Hydrate ressource with prisma entity const officeFolders = OfficeFolder.hydrate(officeFolderEntityUpdated, { strategy: "excludeAll", }); //success this.httpSuccess(response, officeFolders); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Get a specific folder by uid * @returns IFolder */ @Get("/api/v1/notary/folders/:uid", [authHandler, ruleHandler, folderHandler]) 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 officeFolderEntity = await this.officeFoldersService.getByUid(uid, query); if (!officeFolderEntity) { this.httpNotFoundRequest(response, "folder not found"); return; } //Hydrate ressource with prisma entity const officeFolder = OfficeFolder.hydrate(officeFolderEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, officeFolder); } catch (error) { this.httpInternalError(response, error); return; } this.httpSuccess(response, await this.officeFoldersService.getByUid("uid")); } /** * @description Delete a specific folder */ @Delete("/api/v1/notary/folders/:uid", [authHandler, ruleHandler, folderHandler]) protected async delete(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const officeFolderFound = await this.officeFoldersService.getByUid(uid); if (!officeFolderFound) { this.httpNotFoundRequest(response, "office folder not found"); return; } //call service to get prisma entity const officeFoldertEntity: OfficeFolders = await this.officeFoldersService.delete(uid); //Hydrate ressource with prisma entity const officeFolder = OfficeFolder.hydrate(officeFoldertEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, officeFolder); } catch (error) { this.httpInternalError(response, error); return; } } }