133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, 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 } from "@prisma/client";
|
|
import ObjectHydrate from "@Common/helpers/ObjectHydrate";
|
|
import { OfficeFolder } from "le-coffre-resources/dist/SuperAdmin";
|
|
import { validateOrReject } from "class-validator";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class OfficeFoldersController extends ApiController {
|
|
constructor(private officeFoldersService: OfficeFoldersService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all folders
|
|
*/
|
|
@Get("/api/v1/super-admin/folders")
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
const query = JSON.parse(req.query["q"] as string);
|
|
//call service to get prisma entity
|
|
const prismaEntity: OfficeFolders[] = await this.officeFoldersService.get(query);
|
|
//Hydrate ressource with prisma entity
|
|
const officeFolders = OfficeFolder.map<OfficeFolder>(OfficeFolder, prismaEntity, {
|
|
strategy: "excludeAll",
|
|
});
|
|
//success
|
|
this.httpSuccess(response, officeFolders);
|
|
} catch (error) {
|
|
this.httpBadRequest(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Create a new folder
|
|
*/
|
|
@Post("/api/v1/super-admin/folders")
|
|
protected async post(req: Request, response: Response) {
|
|
try {
|
|
//init OfficeFolder resource with request body values
|
|
const officeFolderEntity = new OfficeFolder();
|
|
ObjectHydrate.hydrate(officeFolderEntity, req.body);
|
|
|
|
//validate folder
|
|
await validateOrReject(officeFolderEntity, { groups: ["create"] });
|
|
//call service to get prisma entity
|
|
const prismaEntityCreated = await this.officeFoldersService.create(officeFolderEntity);
|
|
//Hydrate ressource with prisma entity
|
|
const officeFolderEntityCreated = OfficeFolder.hydrate<OfficeFolder>(prismaEntityCreated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
//success
|
|
this.httpSuccess(response, officeFolderEntityCreated);
|
|
} catch (error) {
|
|
this.httpBadRequest(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Modify a specific folder by uid
|
|
*/
|
|
@Put("/api/v1/super-admin/folders/:uid")
|
|
protected async put(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
throw new Error("No uid provided");
|
|
}
|
|
//init IUser resource with request body values
|
|
const officeFolderEntity = new OfficeFolder();
|
|
ObjectHydrate.hydrate(officeFolderEntity, req.body);
|
|
|
|
//validate user
|
|
await validateOrReject(officeFolderEntity, { groups: ["updateFolder"], forbidUnknownValues: false });
|
|
|
|
//call service to get prisma entity
|
|
const prismaEntityUpdated = await this.officeFoldersService.update(uid, officeFolderEntity);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const officeFolderEntityUpdated = OfficeFolder.hydrate<OfficeFolder>(prismaEntityUpdated, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
//success
|
|
this.httpSuccess(response, officeFolderEntityUpdated);
|
|
} catch (error) {
|
|
this.httpBadRequest(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific folder by uid
|
|
* @returns IFolder
|
|
*/
|
|
@Get("/api/v1/super-admin/folders/:uid")
|
|
protected async getOneByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
throw new Error("No uid provided");
|
|
}
|
|
|
|
let officeFolderEntity: OfficeFolders;
|
|
//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);
|
|
}
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, officeFolder);
|
|
} catch (error) {
|
|
this.httpBadRequest(response, error);
|
|
return;
|
|
}
|
|
this.httpSuccess(response, await this.officeFoldersService.getByUid("uid"));
|
|
}
|
|
}
|