lecoffre-back/src/app/api/customer/OfficeFoldersController.ts
2023-08-10 14:35:38 +02:00

114 lines
3.2 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Get } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import OfficeFoldersService from "@Services/customer/OfficeFoldersService/OfficeFoldersService";
import { Service } from "typedi";
import { OfficeFolders, Prisma } from "@prisma/client";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
// 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/customer/folders")
protected async get(req: Request, response: Response) {
try {
//get query
let query: Prisma.OfficeFoldersFindManyArgs = {};
if (req.query["q"]) {
query = JSON.parse(req.query["q"] as string);
}
if (req.query["search"] && typeof req.query["search"] === "string") {
const filter = req.query["search"];
query = {
where: {
OR: [
{
name: { contains: filter, mode: "insensitive" },
},
{
folder_number: { contains: filter, mode: "insensitive" },
},
{
customers: {
some: {
contact: {
OR: [
{ first_name: { contains: filter, mode: "insensitive" } },
{ last_name: { contains: filter, mode: "insensitive" } },
],
},
},
},
},
],
},
};
}
const officeWhereInput: Prisma.OfficesWhereInput = {};
if (!query.where) query.where = { office: officeWhereInput };
query.where.office = 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 Get a specific folder by uid
* @returns IFolder
*/
@Get("/api/v1/customer/folders/:uid")
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 officeFolderEntity = await this.officeFoldersService.getByUid(uid, query);
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;
}
}
}