273 lines
8.3 KiB
TypeScript
273 lines
8.3 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import { Service } from "typedi";
|
|
import DocumentsService from "@Services/notary/DocumentsService/DocumentsService";
|
|
import { Documents, EDocumentStatus, Prisma } from "@prisma/client";
|
|
import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary";
|
|
import { validateOrReject } from "class-validator";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
import documentHandler from "@App/middlewares/OfficeMembershipHandlers/DocumentHandler";
|
|
import EmailBuilder from "@Common/emails/EmailBuilder";
|
|
import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
|
|
// import NotificationBuilder from "@Common/notifications/NotificationBuilder";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class DocumentsController extends ApiController {
|
|
constructor(
|
|
private documentsService: DocumentsService,
|
|
private emailBuilder: EmailBuilder,
|
|
private officeFoldersService: OfficeFoldersService,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all documents
|
|
* @returns IDocument[] list of documents
|
|
*/
|
|
@Get("/api/v1/notary/documents", [authHandler, ruleHandler])
|
|
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 officeId: string = req.body.user.office_Id;
|
|
const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId };
|
|
if (!query.where) query.where = { document_type: { office: officeWhereInput } };
|
|
query.where.document_type!.office = officeWhereInput;
|
|
|
|
//call service to get prisma entity
|
|
const documentEntities = 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, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Create a new document
|
|
* @returns IDocument created
|
|
*/
|
|
@Post("/api/v1/notary/documents", [authHandler, ruleHandler, documentHandler])
|
|
protected async post(req: Request, response: Response) {
|
|
try {
|
|
//init Document resource with request body values
|
|
const documentEntity = Document.hydrate<Document>(req.body);
|
|
|
|
const folder = await this.officeFoldersService.getByUid(documentEntity.folder?.uid!, {
|
|
folder_anchor: true,
|
|
});
|
|
if (!folder) {
|
|
this.httpBadRequest(response, "Folder not found");
|
|
return;
|
|
}
|
|
|
|
const folderRessource = OfficeFolder.hydrate<OfficeFolder>(folder);
|
|
if (folderRessource.folder_anchor) {
|
|
this.httpBadRequest(response, "Cannot ask document on an anchored or anchoring folder");
|
|
return;
|
|
}
|
|
|
|
//validate document
|
|
await validateOrReject(documentEntity, { groups: ["createDocument"], forbidUnknownValues: false });
|
|
|
|
//call service to get prisma entity
|
|
const documentEntityCreated = await this.documentsService.create(documentEntity);
|
|
|
|
//create email for asked document
|
|
await this.emailBuilder.sendDocumentEmails(documentEntityCreated);
|
|
|
|
//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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Update a specific document
|
|
*/
|
|
@Put("/api/v1/notary/documents/:uid", [authHandler, ruleHandler, documentHandler])
|
|
protected async update(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const documentFound = await this.documentsService.getByUid(uid);
|
|
|
|
if (!documentFound) {
|
|
this.httpNotFoundRequest(response, "document not found");
|
|
return;
|
|
}
|
|
|
|
if(documentFound.document_status === EDocumentStatus.REFUSED || documentFound.document_status === EDocumentStatus.VALIDATED) {
|
|
this.httpForbidden(response, "You are not allowed to update a VALIDATED or REFUSED document");
|
|
return;
|
|
}
|
|
|
|
//init Document resource with request body values
|
|
const documentEntity = Document.hydrate<Document>(req.body);
|
|
|
|
//validate document
|
|
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
|
|
|
|
//call service to get prisma entity
|
|
const documentEntityUpdated: Documents = await this.documentsService.update(uid, documentEntity);
|
|
|
|
//create email for asked document
|
|
// this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
|
|
// this.notificationBuilder.sendDocumentAnchoredNotificatiom(documentEntityUpdated);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, document);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Update a specific document
|
|
*/
|
|
@Put("/api/v1/notary/documents/:uid/refuse", [authHandler, ruleHandler, documentHandler])
|
|
protected async refuseDocument(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const documentFound = await this.documentsService.getByUid(uid, {
|
|
files: true,
|
|
});
|
|
|
|
if (!documentFound) {
|
|
this.httpNotFoundRequest(response, "document not found");
|
|
return;
|
|
}
|
|
|
|
//init Document resource with request body values
|
|
const documentEntity = Document.hydrate<Document>(documentFound);
|
|
|
|
// Status to refuse
|
|
documentEntity.document_status = EDocumentStatus.REFUSED;
|
|
|
|
//validate document
|
|
await validateOrReject(documentEntity, { groups: ["updateDocument"] });
|
|
|
|
//call service to get prisma entity
|
|
const documentEntityUpdated: Documents = await this.documentsService.refuse(uid, documentEntity, req.body.refused_reason);
|
|
|
|
|
|
//create email for asked document
|
|
await this.emailBuilder.sendDocumentEmails(documentEntityUpdated);
|
|
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const document = Document.hydrate<Document>(documentEntityUpdated, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, document);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Delete a specific document
|
|
*/
|
|
@Delete("/api/v1/notary/documents/:uid", [authHandler, ruleHandler, documentHandler])
|
|
protected async delete(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "No uid provided");
|
|
return;
|
|
}
|
|
|
|
const documentFound = await this.documentsService.getByUid(uid);
|
|
|
|
if (!documentFound) {
|
|
this.httpNotFoundRequest(response, "document not found");
|
|
return;
|
|
}
|
|
|
|
//call service to get prisma entity
|
|
const documentEntity: Documents = await this.documentsService.delete(uid);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const document = Document.hydrate<Document>(documentEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, document);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get a specific document by uid
|
|
*/
|
|
@Get("/api/v1/notary/documents/:uid", [authHandler, ruleHandler, 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);
|
|
}
|
|
|
|
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) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|