import { Response, Request } from "express"; import { Controller, Delete, Get, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import FilesService from "@Services/common/FilesService/FilesService"; import { Prisma } from "@prisma/client"; import { File } from "le-coffre-resources/dist/Customer"; import { Document } from "le-coffre-resources/dist/Customer"; import DocumentsService from "@Services/customer/DocumentsService/DocumentsService"; import authHandler from "@App/middlewares/AuthHandler"; import fileHandler from "@App/middlewares/CustomerHandler/FileHandler"; import NotificationBuilder from "@Common/notifications/NotificationBuilder"; import { validateOrReject } from "class-validator"; @Controller() @Service() export default class FilesController extends ApiController { constructor( private filesService: FilesService, private documentService: DocumentsService, private notificationBuilder: NotificationBuilder, ) { super(); } /** * @description Get all Files * @returns File[] list of Files */ @Get("/api/v1/customer/files", [authHandler]) protected async get(req: Request, response: Response) { try { //get query let query: Prisma.FilesFindManyArgs = {}; 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?.document?.depositor) delete query.where.document.depositor; const customerWhereInput: Prisma.FilesWhereInput = { ...query.where, document: { depositor: { contact: { email: email } } } }; query.where = customerWhereInput; if (query.include?.document) delete query.include.document; //call service to get prisma entity const fileEntities = await this.filesService.get(query); //Hydrate ressource with prisma entity const files = File.hydrateArray(fileEntities, { strategy: "excludeAll" }); //success this.httpSuccess(response, files); } catch (error) { this.httpBadRequest(response, error); return; } } /** * @description Get a specific File by uid */ @Get("/api/v1/customer/files/download/:uid", [authHandler, fileHandler]) protected async download(req: Request, response: Response) { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "uid not found"); return; } try { const fileInfo = await this.filesService.download(uid); if (!fileInfo) { this.httpNotFoundRequest(response, "file not found"); return; } response.setHeader("Content-Type", fileInfo.file.mimetype); response.setHeader("Content-Disposition", `inline; filename=${encodeURIComponent(fileInfo.file.file_name)}`); this.httpSuccess(response, fileInfo.buffer); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Create a new File * @returns File created */ @Post("/api/v1/customer/files", [authHandler, fileHandler]) protected async post(req: Request, response: Response) { try { //get file if (!req.file) throw new Error("No file provided"); //init File resource with request body values const fileEntity = File.hydrate(JSON.parse(req.body["q"])); //validate File try { await validateOrReject(fileEntity, { groups: ["createFile"] }); } catch (error) { this.httpBadRequest(response, error); return; } //call service to get prisma entity const fileEntityCreated = await this.filesService.create(fileEntity, req.file); const document = await this.documentService.getByUid(fileEntity.document!.uid!); const documentToUpdate = Document.hydrate(document!); documentToUpdate!.document_status = "DEPOSITED"; const documentUpdated = await this.documentService.update(document!.uid!, documentToUpdate); await this.notificationBuilder.sendDocumentDepositedNotification(documentUpdated!); //Hydrate ressource with prisma entity const fileEntityHydrated = File.hydrate(fileEntityCreated, { strategy: "excludeAll", }); //success this.httpCreated(response, fileEntityHydrated); } catch (error) { this.httpBadRequest(response, error); return; } } /** * @description Delete a specific File */ @Delete("/api/v1/customer/files/:uid", [authHandler, fileHandler]) protected async delete(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const fileFound = await this.filesService.getByUid(uid, { document: { include: { files: true, document_type: true } } }); if (!fileFound) { this.httpNotFoundRequest(response, "file not found"); return; } const fileFoundEntity = File.hydrate(fileFound, { strategy: "excludeAll" }); //call service to get prisma entity const fileEntity = await this.filesService.deleteKeyAndArchive(uid); if ( !(fileFoundEntity.document!.files?.find((file) => file.archived_at === null && file.uid !== uid)) && fileFoundEntity.document!.document_type!.name === "Autres documents" ) { await this.documentService.delete(fileFoundEntity.document!.uid!); } //Hydrate ressource with prisma entity const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, file); } catch (error) { this.httpInternalError(response, error); return; } } /** * @description Get a specific File by uid */ @Get("/api/v1/customer/files/:uid", [authHandler, fileHandler]) 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.document) delete query.document; } const fileEntity = await this.filesService.getByUid(uid, query); if (!fileEntity) { this.httpNotFoundRequest(response, "file not found"); return; } //Hydrate ressource with prisma entity const file = File.hydrate(fileEntity, { strategy: "excludeAll" }); //success this.httpSuccess(response, file); } catch (error) { this.httpInternalError(response, error); return; } } }