155 lines
4.7 KiB
TypeScript
155 lines
4.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 OfficesService from "@Services/super-admin/OfficesService/OfficesService";
|
|
import { Service } from "typedi";
|
|
import { Offices, Prisma } from "@prisma/client";
|
|
import { Office as OfficeResource } from "le-coffre-resources/dist/SuperAdmin";
|
|
import { validateOrReject } from "class-validator";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import roleHandler from "@App/middlewares/RolesHandler";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class OfficesController extends ApiController {
|
|
constructor(private officesService: OfficesService) {
|
|
super();
|
|
}
|
|
/**
|
|
* @description Get all offices
|
|
*/
|
|
@Get("/api/v1/super-admin/offices", [authHandler, roleHandler, ruleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query: Prisma.OfficesFindManyArgs = {};
|
|
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",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(query.where?.office_folders) delete query.where.office_folders;
|
|
if(query.include?.office_folders) {
|
|
this.httpForbidden(response, "You can't include office_folders");
|
|
return;
|
|
};
|
|
|
|
//call service to get prisma entity
|
|
const officesEntities: Offices[] = await this.officesService.get(query);
|
|
//Hydrate ressource with prisma entity
|
|
const offices = OfficeResource.hydrateArray<OfficeResource>(officesEntities, { strategy: "excludeAll" });
|
|
//success
|
|
this.httpSuccess(response, offices);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
/**
|
|
* @description Create a new office
|
|
*/
|
|
@Post("/api/v1/super-admin/offices", [authHandler, roleHandler, ruleHandler])
|
|
protected async post(req: Request, response: Response) {
|
|
try {
|
|
//init IUser resource with request body values
|
|
const officeEntity = OfficeResource.hydrate<OfficeResource>(req.body);
|
|
//validate user
|
|
await validateOrReject(officeEntity, { groups: ["createOffice"], forbidUnknownValues: false });
|
|
//call service to get prisma entity
|
|
const officeEntityCreated = await this.officesService.create(officeEntity);
|
|
//Hydrate ressource with prisma entity
|
|
const office = OfficeResource.hydrate<OfficeResource>(officeEntityCreated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
//success
|
|
this.httpCreated(response, office);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
/**
|
|
* @description Modify a specific office by uid
|
|
*/
|
|
@Put("/api/v1/super-admin/offices/:uid", [authHandler, roleHandler, ruleHandler])
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const officeFound = await this.officesService.getByUid(uid);
|
|
|
|
if (!officeFound) {
|
|
this.httpNotFoundRequest(response, "office not found");
|
|
return;
|
|
}
|
|
//init IUser resource with request body values
|
|
const officeEntity = OfficeResource.hydrate<OfficeResource>(req.body);
|
|
|
|
//call service to get prisma entity
|
|
const officeEntityUpdated = await this.officesService.update(uid, officeEntity);
|
|
//Hydrate ressource with prisma entity
|
|
const office = OfficeResource.hydrate<OfficeResource>(officeEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
//success
|
|
this.httpSuccess(response, office);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
/**
|
|
* @description Get a specific office by uid
|
|
*/
|
|
@Get("/api/v1/super-admin/offices/:uid", [authHandler, roleHandler, ruleHandler])
|
|
protected async getOneByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
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<OfficeResource>(officeEntity, { strategy: "excludeAll" });
|
|
//success
|
|
this.httpSuccess(response, office);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|