From 2e47e7a8209785c37a21d65940fb4ce2a777e5f0 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Wed, 4 Oct 2023 17:45:03 +0200 Subject: [PATCH] :sparkles: Cannot ask documents on an anchored folder --- src/app/api/notary/DocumentsController.ts | 29 ++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/app/api/notary/DocumentsController.ts b/src/app/api/notary/DocumentsController.ts index 0be80d12..9c75455d 100644 --- a/src/app/api/notary/DocumentsController.ts +++ b/src/app/api/notary/DocumentsController.ts @@ -4,18 +4,23 @@ import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import DocumentsService from "@Services/notary/DocumentsService/DocumentsService"; import { Documents, Prisma } from "@prisma/client"; -import { Document } from "le-coffre-resources/dist/Notary"; +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) { + constructor( + private documentsService: DocumentsService, + private emailBuilder: EmailBuilder, + private officeFoldersService: OfficeFoldersService, + ) { super(); } @@ -32,8 +37,8 @@ export default class DocumentsController extends ApiController { query = JSON.parse(req.query["q"] as string); } const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId } ; - if(!query.where) query.where = { document_type : {office: officeWhereInput}}; + 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 @@ -60,6 +65,20 @@ export default class DocumentsController extends ApiController { //init Document resource with request body values const documentEntity = Document.hydrate(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(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 }); @@ -104,8 +123,6 @@ export default class DocumentsController extends ApiController { //init Document resource with request body values const documentEntity = Document.hydrate(req.body); - - //validate document await validateOrReject(documentEntity, { groups: ["updateDocument"] });