lecoffre-back/src/app/api/customer/DocumentsController.ts
2024-12-04 15:23:08 +01:00

157 lines
4.9 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Get, Post } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi";
import DocumentsService from "@Services/customer/DocumentsService/DocumentsService";
import { Documents, Prisma } from "@prisma/client";
import { Document, OfficeFolder } from "le-coffre-resources/dist/Customer";
import authHandler from "@App/middlewares/AuthHandler";
import documentHandler from "@App/middlewares/CustomerHandler/DocumentHandler";
import { validateOrReject } from "class-validator";
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
@Controller()
@Service()
export default class DocumentsController extends ApiController {
constructor(private documentsService: DocumentsService, private officeFoldersService: OfficeFoldersService) {
super();
}
/**
* @description Get all documents
* @returns IDocument[] list of documents
*/
@Get("/api/v1/customer/documents", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query: Prisma.DocumentsFindManyArgs = {};
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;
}
}
const email: string = req.body.user.email;
if (!email) {
this.httpBadRequest(response, "Missing customer email");
return;
}
if (query.where?.depositor) delete query.where.depositor;
if (query.where?.depositor_uid) delete query.where.depositor_uid;
const customerWhereInput: Prisma.DocumentsWhereInput = { ...query.where, depositor: { contact: { email: email } } };
query.where = customerWhereInput;
if (query.include?.folder) delete query.include.folder;
//call service to get prisma entity
const documentEntities: Documents[] = await this.documentsService.get(query);
//Hydrate ressource with prisma entity
const documents = Document.hydrateArray<Document>(documentEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, documents);
} catch (error) {
this.httpInternalError(response);
return;
}
}
/**
* @description Get a specific document by uid
*/
@Get("/api/v1/customer/documents/:uid", [authHandler, documentHandler])
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);
if (query.folder) delete query.folder;
}
const documentEntity = await this.documentsService.getByUid(uid, query);
if (!documentEntity) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, document);
} catch (error) {
console.error(error);
this.httpInternalError(response);
return;
}
}
/**
* @description Create a new File
* @returns File created
*/
@Post("/api/v1/customer/documents", [authHandler, documentHandler])
protected async post(req: Request, response: Response) {
try {
//init Document resource with request body values
const documentEntity = Document.hydrate<Document>(req.body);
const email = req.body.user.email;
if (!documentEntity.folder?.uid) {
this.httpBadRequest(response, "No folder uid provided");
return;
}
const folder = await this.officeFoldersService.getByUid(documentEntity.folder.uid, {
folder_anchor: true,
customers: { include: { contact: true } },
office: true,
});
if (!folder) {
this.httpBadRequest(response, "Folder not found");
return;
}
const folderEntity = OfficeFolder.hydrate<OfficeFolder>(folder, { strategy: "excludeAll" });
if (!folderEntity.customers) {
this.httpBadRequest(response, "No customers found in folder");
return;
}
const depositor = folderEntity.customers.find((customer) => customer.contact?.email === email);
delete documentEntity.depositor;
documentEntity.depositor = depositor;
try {
//validate document
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });
} catch (error) {
this.httpValidationError(response, error);
return;
}
//call service to get prisma entity
const documentEntityCreated = await this.documentsService.create(documentEntity, folder.office_uid);
//Hydrate ressource with prisma entity
const document = Document.hydrate<Document>(documentEntityCreated, {
strategy: "excludeAll",
});
//success
this.httpCreated(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}