lecoffre-back/src/app/api/customer/FilesNotaryController.ts

55 lines
1.9 KiB
TypeScript

import authHandler from "@App/middlewares/AuthHandler";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Controller, Get } from "@ControllerPattern/index";
import { EDocumentNotaryStatus } from "@prisma/client";
import FilesNotaryService from "@Services/common/FilesNotaryService/FilesNotaryService";
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
import { Request, Response } from "express";
import { Service } from "typedi";
@Controller()
@Service()
export default class FilesNotaryController extends ApiController {
constructor(private filesNotaryService: FilesNotaryService, private documentsNotaryService: DocumentsNotaryService) {
super();
}
/**
* @description Get a specific File by uid
*/
@Get("/api/v1/customer/files-notary/:filesNotaryUid/documents-notary/:documentsNotaryUid/download", [authHandler])
protected async download(req: Request, response: Response) {
const filesNotaryUid = req.params["filesNotaryUid"];
const documentsNotaryUid = req.params["documentsNotaryUid"];
if (!filesNotaryUid) {
this.httpBadRequest(response, "filesNotaryUid not found");
return;
}
if (!documentsNotaryUid) {
this.httpBadRequest(response, "documentsNotaryUid not found");
return;
}
try {
const fileInfo = await this.filesNotaryService.download(filesNotaryUid);
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)}`);
await this.documentsNotaryService.changeStatus(documentsNotaryUid, EDocumentNotaryStatus.DOWNLOADED);
this.httpSuccess(response, fileInfo.buffer);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}