lecoffre-back/src/app/api/notary/FilesController.ts
2023-07-06 14:18:41 +02:00

149 lines
4.1 KiB
TypeScript

import { Response, Request } from "express";
import { Controller, Delete, Get } 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/Notary";
import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler";
import fileHandler from "@App/middlewares/OfficeMembershipHandlers/FileHandler";
@Controller()
@Service()
export default class FilesController extends ApiController {
constructor(private filesService: FilesService) {
super();
}
/**
* @description Get all Files
* @returns File[] list of Files
*/
@Get("/api/v1/notary/files", [authHandler, ruleHandler])
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);
}
const officeId: string = req.body.user.office_Id;
const officeWhereInput: Prisma.FilesWhereInput = { document: { folder: { office: { uid: officeId } } } };
query.where = officeWhereInput;
//call service to get prisma entity
const fileEntities = await this.filesService.get(query);
//Hydrate ressource with prisma entity
const files = File.hydrateArray<File>(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/notary/files/download/:uid", [authHandler, ruleHandler, 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 Delete a specific File
*/
@Delete("/api/v1/notary/files/:uid", [authHandler, ruleHandler, 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);
if (!fileFound) {
this.httpNotFoundRequest(response, "file not found");
return;
}
//call service to get prisma entity
const fileEntity = await this.filesService.deleteKeyAndArchive(uid);
if (!fileEntity) {
this.httpNotFoundRequest(response, "file not found");
return;
}
//Hydrate ressource with prisma entity
const file = File.hydrate<File>(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/notary/files/:uid", [authHandler, ruleHandler, 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);
}
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<File>(fileEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, file);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}