lecoffre-back/src/app/api/super-admin/OfficeFoldersController.ts
2023-07-04 14:05:25 +02:00

184 lines
5.8 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
import { Service } from "typedi";
import { OfficeFolders, Prisma } from "@prisma/client";
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
import { validateOrReject } from "class-validator";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler";
@Controller()
@Service()
export default class OfficeFoldersController extends ApiController {
constructor(private officeFoldersService: OfficeFoldersService) {
super();
}
/**
* @description Get all folders
*/
@Get("/api/v1/super-admin/folders", [authHandler, ruleHandler])
protected async get(req: Request, response: Response) {
try {
//get query
const query = JSON.parse(req.query["q"] as string);
const officeId: string = req.body.user.office_Id;
const officeWhereInput: Prisma.OfficeFoldersWhereInput = { office: { uid: officeId } };
query.where = officeWhereInput;
//call service to get prisma entity
const officeFolderEntities: OfficeFolders[] = await this.officeFoldersService.get(query);
//Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrateArray<OfficeFolder>(officeFolderEntities, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Create a new folder
*/
@Post("/api/v1/super-admin/folders", [authHandler, ruleHandler, folderHandler])
protected async post(req: Request, response: Response) {
try {
//init OfficeFolder resource with request body values
const officeFolderRessource = OfficeFolder.hydrate<OfficeFolder>(req.body);
await officeFolderRessource.validateOrReject?.({ groups: ["createFolder"], forbidUnknownValues: false });
//call service to get prisma entity
const officeFolderEntity = await this.officeFoldersService.create(officeFolderRessource);
//Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, {
strategy: "excludeAll",
});
//success
this.httpCreated(response, officeFolders);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Modify a specific folder by uid
*/
@Put("/api/v1/super-admin/folders/:uid", [authHandler, ruleHandler, folderHandler])
protected async put(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const officeFolderFound = await this.officeFoldersService.getByUid(uid);
if (!officeFolderFound) {
this.httpNotFoundRequest(response, "office folder not found");
return;
}
//init OfficeFolder resource with request body values
const officeFolderEntity = OfficeFolder.hydrate<OfficeFolder>(req.body);
//validate folder
await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
//call service to get prisma entity
const officeFolderEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity);
//Hydrate ressource with prisma entity
const officeFolders = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntityUpdated, {
strategy: "excludeAll",
});
//success
this.httpSuccess(response, officeFolders);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Get a specific folder by uid
* @returns IFolder
*/
@Get("/api/v1/super-admin/folders/:uid", [authHandler, ruleHandler, folderHandler])
protected async getOneByUid(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
let officeFolderEntity: OfficeFolders | null;
//get query
if (req.query["q"]) {
const query = JSON.parse(req.query["q"] as string);
officeFolderEntity = await this.officeFoldersService.getByUid(uid, query);
} else {
//call service to get prisma entity
officeFolderEntity = await this.officeFoldersService.getByUid(uid);
}
if (!officeFolderEntity) {
this.httpNotFoundRequest(response, "folder not found");
return;
}
//Hydrate ressource with prisma entity
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, officeFolder);
} catch (error) {
this.httpInternalError(response, error);
return;
}
this.httpSuccess(response, await this.officeFoldersService.getByUid("uid"));
}
/**
* @description Delete a specific folder
*/
@Delete("/api/v1/super-admin/folders/:uid", [authHandler, ruleHandler, folderHandler])
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const officeFolderFound = await this.officeFoldersService.getByUid(uid);
if (!officeFolderFound) {
this.httpNotFoundRequest(response, "office folder not found");
return;
}
//call service to get prisma entity
const officeFoldertEntity: OfficeFolders = await this.officeFoldersService.delete(uid);
//Hydrate ressource with prisma entity
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFoldertEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, officeFolder);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}