From a68d8b817a70d315b154cbde25f66066e4818343 Mon Sep 17 00:00:00 2001 From: Vins Date: Mon, 9 Sep 2024 15:10:24 +0200 Subject: [PATCH] Endpoint delete document notary --- .../api/customer/DocumentsNotaryController.ts | 50 +++++++ .../api/notary/DocumentsNotaryController.ts | 138 +++++------------- 2 files changed, 87 insertions(+), 101 deletions(-) create mode 100644 src/app/api/customer/DocumentsNotaryController.ts diff --git a/src/app/api/customer/DocumentsNotaryController.ts b/src/app/api/customer/DocumentsNotaryController.ts new file mode 100644 index 00000000..4fd90e4e --- /dev/null +++ b/src/app/api/customer/DocumentsNotaryController.ts @@ -0,0 +1,50 @@ +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 { DocumentNotary } from "le-coffre-resources/dist/Notary"; +import authHandler from "@App/middlewares/AuthHandler"; +// 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(documentEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, documents); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/notary/DocumentsNotaryController.ts b/src/app/api/notary/DocumentsNotaryController.ts index 1552646e..afcd826e 100644 --- a/src/app/api/notary/DocumentsNotaryController.ts +++ b/src/app/api/notary/DocumentsNotaryController.ts @@ -1,9 +1,9 @@ import { Response, Request } from "express"; -import { Controller, Get, Post } from "@ControllerPattern/index"; +import { Controller, Delete, Get, Post } 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 { DocumentsNotary, Prisma } from "@prisma/client"; import { DocumentNotary, FileNotary } from "le-coffre-resources/dist/Notary"; import authHandler from "@App/middlewares/AuthHandler"; import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; @@ -41,10 +41,6 @@ export default class DocumentsNotaryController extends ApiController { return; } } - const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId }; - if (!query.where) query.where = { folder: { office: officeWhereInput } }; - query.where.folder!.office = officeWhereInput; //call service to get prisma entity const documentEntities = await this.documentsNotaryService.get(query); @@ -96,112 +92,52 @@ export default class DocumentsNotaryController extends ApiController { const fileEntity = FileNotary.hydrate(JSON.parse(query)); const fileEntityCreated = await this.filesService.createFileNotary(fileEntity, req.file!); - console.log("fileEntityCreated", fileEntityCreated); - - + if(!fileEntityCreated) { + this.httpBadRequest(response, "File could not be created"); + return; + }; - // //init Document resource with request body values - // const documentNotaryEntity = DocumentNotary.hydrate(req.body); - // console.log(documentNotaryEntity); + const documentNotary = await this.documentsNotaryService.getByUid(documentNotaryEntityCreated.uid); - // const folder = await this.officeFoldersService.getByUid(documentNotaryEntity.folder?.uid!, { - // folder_anchor: true, - // }); - // if (!folder) { - // this.httpBadRequest(response, "Folder not found"); - // return; - // } - - // const folderRessource = OfficeFolder.hydrate(folder); - // if (folderRessource.folder_anchor) { - // this.httpBadRequest(response, "Cannot add document on an anchored or anchoring folder"); - // return; - // } - - // //validate document - // await validateOrReject(documentNotaryEntity, { groups: ["createDocument"], forbidUnknownValues: false }); - - // //call service to get prisma entity - // const documentNotaryEntityCreated = await this.documentsNotaryService.create(documentNotaryEntity); - - // //Hydrate ressource with prisma entity - // const documentNotary = DocumentNotary.hydrate(documentNotaryEntityCreated, { - // strategy: "excludeAll", - // }); - - // //success - // this.httpCreated(response, documentNotary); + const document = DocumentNotary.hydrate(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, ruleHandler, documentHandler]) - // protected async delete(req: Request, response: Response) { - // try { - // const uid = req.params["uid"]; - // if (!uid) { - // this.httpBadRequest(response, "No uid provided"); - // 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 documentFound = await this.documentsNotaryService.getByUid(uid); + const documentNotaryFound = await this.documentsNotaryService.getByUid(uid); - // if (!documentFound) { - // this.httpNotFoundRequest(response, "document not found"); - // return; - // } + if (!documentNotaryFound) { + this.httpNotFoundRequest(response, "document not found"); + return; + } - // //call service to get prisma entity - // const documentEntity: DocumentsNotary = await this.documentsNotaryService.delete(uid); + //call service to get prisma entity + const documentNotaryEntity: DocumentsNotary = await this.documentsNotaryService.delete(uid); - // //Hydrate ressource with prisma entity - // const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); + //Hydrate ressource with prisma entity + const documentNotary = DocumentNotary.hydrate(documentNotaryEntity, { 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_notary/: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.documentsNotaryService.getByUid(uid, query); - - // if (!documentEntity) { - // this.httpNotFoundRequest(response, "document not found"); - // return; - // } - - // //Hydrate ressource with prisma entity - // const document = Document.hydrate(documentEntity, { strategy: "excludeAll" }); - - // //success - // this.httpSuccess(response, document); - // } catch (error) { - // this.httpInternalError(response, error); - // return; - // } - // } + //success + this.httpSuccess(response, documentNotary); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } }