import { Response, Request } from "express"; import { Controller, Get, Post, Put } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import OfficeRolesService from "@Services/admin/OfficeRolesService/OfficeRolesService"; import { Service } from "typedi"; import { validateOrReject } from "class-validator"; import { OfficeRole } from "le-coffre-resources/dist/Admin"; import { Prisma } from "@prisma/client"; import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; import officeRoleHandler from "@App/middlewares/OfficeMembershipHandlers/OfficeRoleHandler"; import roleHandler from "@App/middlewares/RolesHandler"; @Controller() @Service() export default class OfficeRolesController extends ApiController { constructor(private officeRolesService: OfficeRolesService) { super(); } /** * @description Get all officeRoles */ @Get("/api/v1/admin/office-roles", [authHandler, roleHandler, ruleHandler]) protected async get(req: Request, response: Response) { try { //get query let query: Prisma.OfficeRolesFindManyArgs = {}; 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: { 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 officeRolesEntities = await this.officeRolesService.get(query); //Hydrate ressource with prisma entity const officeRoles = OfficeRole.hydrateArray(officeRolesEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, officeRoles); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Create a new officeRole */ @Post("/api/v1/admin/office-roles", [authHandler, roleHandler, ruleHandler, officeRoleHandler]) protected async getAddresses(req: Request, response: Response) { try { //init IOfficeRole resource with request body values const officeRoleEntity = OfficeRole.hydrate(req.body); //validate officeRole await validateOrReject(officeRoleEntity, { groups: ["createOfficeRole"] }); //call service to get prisma entity const officeRoleEntityCreated = await this.officeRolesService.create(officeRoleEntity); //Hydrate ressource with prisma entity const officeRole = OfficeRole.hydrate(officeRoleEntityCreated, { strategy: "excludeAll", }); //success this.httpCreated(response, officeRole); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Modify a specific officeRole by uid */ @Put("/api/v1/admin/office-roles/:uid", [authHandler, roleHandler, ruleHandler, officeRoleHandler]) protected async put(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const officeRoleFound = await this.officeRolesService.getByUid(uid); if (!officeRoleFound) { this.httpNotFoundRequest(response, "officeRole not found"); return; } //init IOfficeRole resource with request body values const officeRoleEntity = OfficeRole.hydrate(req.body); //validate officeRole await validateOrReject(officeRoleEntity, { groups: ["updateOfficeRole"] }); //call service to get prisma entity const officeRoleEntityUpdated = await this.officeRolesService.update(officeRoleEntity); //Hydrate ressource with prisma entity const officeRole = OfficeRole.hydrate(officeRoleEntityUpdated, { strategy: "excludeAll", }); //success this.httpSuccess(response, officeRole); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Get a specific officeRole by uid */ @Get("/api/v1/admin/office-roles/:uid", [authHandler, roleHandler, ruleHandler, officeRoleHandler]) 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 officeRoleEntity = await this.officeRolesService.getByUid(uid, query); if (!officeRoleEntity) { this.httpNotFoundRequest(response, "officeRole not found"); return; } //Hydrate ressource with prisma entity const officeRole = OfficeRole.hydrate(officeRoleEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, officeRole); } catch (error) { this.httpInternalError(response, error); return; } } }