87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
import { Response, Request } from "express";
|
|
import { Controller, Get } from "@ControllerPattern/index";
|
|
import ApiController from "@Common/system/controller-pattern/ApiController";
|
|
import { Service } from "typedi";
|
|
import DocumentsNotaryService from "@Services/notary/DocumentsNotaryService/DocumentsNotaryService";
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import DocumentNotary from "le-coffre-resources/dist/Notary/DocumentNotary";
|
|
|
|
// import NotificationBuilder from "@Common/notifications/NotificationBuilder";
|
|
|
|
@Controller()
|
|
@Service()
|
|
export default class DocumentsNotaryController extends ApiController {
|
|
constructor(private documentsNotaryService: DocumentsNotaryService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Get all documents
|
|
* @returns IDocument[] list of documents
|
|
*/
|
|
@Get("/api/v1/customer/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 Get document by uid
|
|
* @returns IDocument document
|
|
*/
|
|
@Get("/api/v1/customer/documents_notary/:uid", [authHandler])
|
|
protected async getByUid(req: Request, response: Response) {
|
|
try {
|
|
const uid = req.params["uid"];
|
|
if (!uid) {
|
|
this.httpBadRequest(response, "uid is required");
|
|
return;
|
|
}
|
|
|
|
//get query
|
|
let query;
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
}
|
|
|
|
const documentEntity = await this.documentsNotaryService.getByUid(uid, query);
|
|
|
|
if (!documentEntity) {
|
|
this.httpNotFoundRequest(response, "Document not found");
|
|
return;
|
|
}
|
|
|
|
const document = DocumentNotary.hydrate<DocumentNotary>(documentEntity, { strategy: "excludeAll" });
|
|
|
|
//success
|
|
this.httpSuccess(response, document);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|