import { Response, Request } from "express"; import { Controller, Get } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficesService from "@Services/notary/OfficesService/OfficesService"; import { Service } from "typedi"; import { Offices } from "@prisma/client"; import { Office as OfficeResource } from "le-coffre-resources/dist/Notary"; import ruleHandler from "@App/middlewares/RulesHandler"; import authHandler from "@App/middlewares/AuthHandler"; @Controller() @Service() export default class OfficesController extends ApiController { constructor(private officesService: OfficesService) { super(); } /** * @description Get all offices */ @Get("/api/v1/notary/offices", [authHandler, 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); } //call service to get prisma entity const officesEntities: Offices[] = await this.officesService.get(query); //Hydrate ressource with prisma entity const offices = OfficeResource.hydrateArray(officesEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, offices); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Get a specific office by uid */ @Get("/api/v1/notary/offices/:uid", [authHandler, ruleHandler]) 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 officeEntity = await this.officesService.getByUid(uid, query); if (!officeEntity) { this.httpNotFoundRequest(response, "office not found"); return; } //Hydrate ressource with prisma entity const office = OfficeResource.hydrate(officeEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, office); } catch (error) { this.httpInternalError(response, error); return; } } }