lecoffre-back/src/app/api/notary/OfficeRolesController.ts
2023-07-24 15:14:59 +02:00

84 lines
2.6 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Get } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import OfficeRolesService from "@Services/notary/OfficeRolesService/OfficeRolesService";
import { Service } from "typedi";
import { OfficeRole } from "le-coffre-resources/dist/Notary";
import { Prisma } from "@prisma/client";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
import officeRoleHandler from "@App/middlewares/OfficeMembershipHandlers/OfficeRoleHandler";
@Controller()
@Service()
export default class OfficeRolesController extends ApiController {
constructor(private officeRolesService: OfficeRolesService) {
super();
}
/**
* @description Get all officeRoles
*/
@Get("/api/v1/notary/office-roles", [authHandler, 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);
}
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<OfficeRole>(officeRolesEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, officeRoles);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific officeRole by uid
*/
@Get("/api/v1/notary/office-roles/:uid", [authHandler, 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<OfficeRole>(officeRoleEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, officeRole);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}