lecoffre-back/src/app/api/notary/DocumentsNotaryController.ts

138 lines
4.4 KiB
TypeScript

import authHandler from "@App/middlewares/AuthHandler";
import ApiController from "@Common/system/controller-pattern/ApiController";
import { Controller, Delete, Get, Post } from "@ControllerPattern/index";
import { DocumentsNotary, Prisma } from "@prisma/client";
import CustomersService from "@Services/admin/CustomersService/CustomersService";
import FilesNotaryService from "@Services/common/FilesNotaryService/FilesService";
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
import UsersService from "@Services/notary/UsersService/UsersService";
import { Request, Response } from "express";
import { DocumentNotary, FileNotary } from "le-coffre-resources/dist/Notary";
import { Service } from "typedi";
@Controller()
@Service()
export default class DocumentsNotaryController extends ApiController {
constructor(
private documentsNotaryService: DocumentsNotaryService,
private officeFoldersService: OfficeFoldersService,
private customerService: CustomersService,
private userService: UsersService,
private filesNotaryService: FilesNotaryService,
) {
super();
}
/**
* @description Get all documents
* @returns IDocument[] list of documents
*/
@Get("/api/v1/notary/documents_notary", [authHandler])
protected async get(req: Request, response: Response) {
try {
//get query
let query: Prisma.DocumentsNotaryFindManyArgs = {};
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;
}
}
//call service to get prisma entity
const documentEntities = await this.documentsNotaryService.get(query);
//Hydrate ressource with prisma entity
const documents = DocumentNotary.hydrateArray<DocumentNotary>(documentEntities, { strategy: "excludeAll" });
//success
this.httpSuccess(response, documents);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Create a new document
* @returns IDocument created
*/
@Post("/api/v1/notary/documents_notary", [authHandler])
protected async post(req: Request, response: Response) {
try {
if (!req.file) return;
const customer = await this.customerService.getByUid(req.body.customerUid);
if (!customer) return;
const folder = await this.officeFoldersService.getByUid(req.body.folderUid);
if (!folder) return;
const user = await this.userService.getByUid(req.body.user.userId);
if (!user) return;
const documentNotaryEntity = DocumentNotary.hydrate<DocumentNotary>({
customer: customer,
folder: folder,
depositor: user,
});
const documentNotaryEntityCreated = await this.documentsNotaryService.create(documentNotaryEntity);
const query = JSON.stringify({ document: { uid: documentNotaryEntityCreated.uid } });
const fileEntity = FileNotary.hydrate<FileNotary>(JSON.parse(query));
const fileEntityCreated = await this.filesNotaryService.create(fileEntity, req.file!);
if (!fileEntityCreated) {
this.httpBadRequest(response, "File could not be created");
return;
}
const documentNotary = await this.documentsNotaryService.getByUid(documentNotaryEntityCreated.uid);
const document = DocumentNotary.hydrate<DocumentNotary>(documentNotary!);
//success
this.httpCreated(response, document);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Delete a specific document
*/
@Delete("/api/v1/notary/documents_notary/:uid", [authHandler])
protected async delete(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const documentNotaryFound = await this.documentsNotaryService.getByUid(uid);
if (!documentNotaryFound) {
this.httpNotFoundRequest(response, "document not found");
return;
}
//call service to get prisma entity
const documentNotaryEntity: DocumentsNotary = await this.documentsNotaryService.delete(uid);
//Hydrate ressource with prisma entity
const documentNotary = DocumentNotary.hydrate<DocumentNotary>(documentNotaryEntity, { strategy: "excludeAll" });
//success
this.httpSuccess(response, documentNotary);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}