diff --git a/src/app/api/notary/OfficeFolderAnchorsController.ts b/src/app/api/notary/OfficeFolderAnchorsController.ts index 8cedd895..1ae9f9b1 100644 --- a/src/app/api/notary/OfficeFolderAnchorsController.ts +++ b/src/app/api/notary/OfficeFolderAnchorsController.ts @@ -3,7 +3,7 @@ import { Controller, Get, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import { Document, OfficeFolder, File } from "le-coffre-resources/dist/Notary"; -import { getFolderHashes, getFolderFilesUid } from "@Common/optics/notary"; +import { getFolderFilesUid } from "@Common/optics/notary"; import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository"; import FilesService from "@Services/common/FilesService/FilesService"; @@ -49,6 +49,27 @@ export default class OfficeFoldersController extends ApiController { super(); } + private getValidatedDocumentHashes(officeFolder: OfficeFolder): string[] { + const documents = officeFolder.documents ?? []; + + if (documents.length === 0) { + 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); + }); + } + }); + + return folderHashes.sort(); + } + /** * @description Download a folder anchoring proof document along with all accessible files */ @@ -79,15 +100,14 @@ export default class OfficeFoldersController extends ApiController { } const officeFolder = OfficeFolder.hydrate(officeFolderFound, { strategy: "excludeAll" }); - const folderHashes = getFolderHashes(officeFolder); + const sortedHashes = this.getValidatedDocumentHashes(officeFolder); const folderFilesUid = getFolderFilesUid(officeFolder); - if (folderHashes.length === 0) { + if (sortedHashes.length === 0) { this.httpNotFoundRequest(response, "No file hash to anchor"); return; } - const sortedHashes = [...folderHashes].sort(); const anchoringProof = await this.secureService.download(sortedHashes, officeFolder.office!.name); @@ -180,18 +200,13 @@ export default class OfficeFoldersController extends ApiController { 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 = this.getValidatedDocumentHashes(officeFolder); + + if (sortedHashes.length === 0) { + this.httpBadRequest(response, "No file hash to anchor"); + return; + } - const sortedHashes = [...folderHashes].sort(); const data = await this.secureService.anchor(sortedHashes); const officeFolderAnchor = hydrateOfficeFolderAnchor(data); @@ -248,23 +263,13 @@ export default class OfficeFoldersController extends ApiController { 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 = this.getValidatedDocumentHashes(officeFolder); - if (folderHashes.length === 0) { + if (sortedHashes.length === 0) { this.httpNotFoundRequest(response, "No file hash to anchor"); return; } - const sortedHashes = [...folderHashes].sort(); const officeFolderAnchorFound = OfficeFolderAnchor.hydrate(officeFolderFound.folder_anchor, { strategy: "excludeAll", }); diff --git a/src/common/notifications/NotificationBuilder.ts b/src/common/notifications/NotificationBuilder.ts index e51abb68..01ddae5d 100644 --- a/src/common/notifications/NotificationBuilder.ts +++ b/src/common/notifications/NotificationBuilder.ts @@ -95,7 +95,7 @@ export default class NotificationBuilder { redirection_url: "", created_at: new Date(), updated_at: new Date(), - user: [user] || [], + user: user ? [user] : [], }); } @@ -105,7 +105,7 @@ export default class NotificationBuilder { redirection_url: "", created_at: new Date(), updated_at: new Date(), - user: [user] || [], + user: user ? [user] : [], }); }