192 lines
5.7 KiB
TypeScript
192 lines
5.7 KiB
TypeScript
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";
|
|
import RulesService from "@Services/admin/RulesService/RulesService";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class OfficeRolesController extends ApiController {
|
|
constructor(private officeRolesService: OfficeRolesService, private rulesService: RulesService) {
|
|
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(query.where?.uid) {
|
|
this.httpBadRequest(response, "You can't filter by uid");
|
|
return;
|
|
}
|
|
}
|
|
|
|
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<OfficeRole>(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<OfficeRole>(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<OfficeRole>(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;
|
|
}
|
|
|
|
if (req.body.rules) {
|
|
const allRules = await this.rulesService.get({
|
|
where: {
|
|
OR: [
|
|
{
|
|
namespace: "notary",
|
|
},
|
|
{
|
|
namespace: "collaborator",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
req.body.rules = req.body.rules.filter((rule: any) => {
|
|
const ruleFound = allRules.find((r) => r.uid === rule.uid && (r.namespace === "notary" || r.namespace === "collaborator"));
|
|
return ruleFound;
|
|
});
|
|
}
|
|
//init IOfficeRole resource with request body values
|
|
const officeRoleEntity = OfficeRole.hydrate<OfficeRole>(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<OfficeRole>(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<OfficeRole>(officeRoleEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, officeRole);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|