diff --git a/src/app/api/notary/OfficeFolderAnchorsController.ts b/src/app/api/notary/OfficeFolderAnchorsController.ts index bf8141ed..3f84a5bd 100644 --- a/src/app/api/notary/OfficeFolderAnchorsController.ts +++ b/src/app/api/notary/OfficeFolderAnchorsController.ts @@ -2,7 +2,7 @@ import { Response, Request } from "express"; import { Controller, Get, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; -import { Document, OfficeFolder } from "le-coffre-resources/dist/Notary"; +import { Document, OfficeFolder, File } from "le-coffre-resources/dist/Notary"; import { getFolderHashes, getFolderFilesUid } from "@Common/optics/notary"; import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository"; @@ -162,24 +162,34 @@ export default class OfficeFoldersController extends ApiController { const officeFolder = OfficeFolder.hydrate(officeFolderFound, { strategy: "excludeAll" }); - // Check if every document is validated in a folder const documents = officeFolder.documents ?? []; - const documentsValidated = documents.filter((document) => { - let documentHydrated = Document.hydrate(document, { strategy: "excludeAll" }); - return documentHydrated.document_status === "VALIDATED"; + + if (documents.length === 0) { + this.httpBadRequest(response, "OfficeFolder has no documents at all"); + return; + } + + const hasInvalidDocument = documents.some((document: any) => { + const documentHydrated = Document.hydrate(document, { strategy: "excludeAll" }); + return documentHydrated.document_status !== "VALIDATED" && + documentHydrated.document_status !== "REFUSED"; }); - if (documentsValidated.length !== documents.length && documents.length !== 0) { - this.httpBadRequest(response, "Cannot anchor a folder with non validated documents"); + if (hasInvalidDocument) { + this.httpBadRequest(response, "OfficeFolder has non validated documents"); return; } - const folderHashes = getFolderHashes(officeFolder); - - if (folderHashes.length === 0) { - this.httpNotFoundRequest(response, "No file hash to anchor"); - return; - } + const folderHashes: string[] = []; + documents.forEach((document: any) => { + const documentHydrated = Document.hydrate(document, { strategy: "excludeAll" }); + if (documentHydrated.document_status === "VALIDATED") { + documentHydrated.files?.forEach((file: any) => { + const fileHydrated = File.hydrate(file, { strategy: "excludeAll" }); + folderHashes.push(fileHydrated.hash); + }); + } + }); const sortedHashes = [...folderHashes].sort(); const data = await this.secureService.anchor(sortedHashes);