Add private getValidatedDocumentHashes and fix download bug

This commit is contained in:
Sosthene 2025-07-30 12:26:50 +02:00
parent 008d21de8c
commit b2dbfbeaa2

View File

@ -3,7 +3,7 @@ import { Controller, Get, Post } from "@ControllerPattern/index";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Service } from "typedi"; import { Service } from "typedi";
import { Document, OfficeFolder, File } from "le-coffre-resources/dist/Notary"; 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 OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository"; import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository";
import FilesService from "@Services/common/FilesService/FilesService"; import FilesService from "@Services/common/FilesService/FilesService";
@ -49,6 +49,27 @@ export default class OfficeFoldersController extends ApiController {
super(); 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>(document, { strategy: "excludeAll" });
if (documentHydrated.document_status === "VALIDATED") {
documentHydrated.files?.forEach((file: any) => {
const fileHydrated = File.hydrate<File>(file, { strategy: "excludeAll" });
folderHashes.push(fileHydrated.hash);
});
}
});
return folderHashes.sort();
}
/** /**
* @description Download a folder anchoring proof document along with all accessible files * @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<OfficeFolder>(officeFolderFound, { strategy: "excludeAll" }); const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound, { strategy: "excludeAll" });
const folderHashes = getFolderHashes(officeFolder); const sortedHashes = this.getValidatedDocumentHashes(officeFolder);
const folderFilesUid = getFolderFilesUid(officeFolder); const folderFilesUid = getFolderFilesUid(officeFolder);
if (folderHashes.length === 0) { if (sortedHashes.length === 0) {
this.httpNotFoundRequest(response, "No file hash to anchor"); this.httpNotFoundRequest(response, "No file hash to anchor");
return; return;
} }
const sortedHashes = [...folderHashes].sort();
const anchoringProof = await this.secureService.download(sortedHashes, officeFolder.office!.name); const anchoringProof = await this.secureService.download(sortedHashes, officeFolder.office!.name);
@ -180,18 +200,13 @@ export default class OfficeFoldersController extends ApiController {
return; return;
} }
const folderHashes: string[] = []; const sortedHashes = this.getValidatedDocumentHashes(officeFolder);
documents.forEach((document: any) => {
const documentHydrated = Document.hydrate<Document>(document, { strategy: "excludeAll" }); if (sortedHashes.length === 0) {
if (documentHydrated.document_status === "VALIDATED") { this.httpBadRequest(response, "No file hash to anchor");
documentHydrated.files?.forEach((file: any) => { return;
const fileHydrated = File.hydrate<File>(file, { strategy: "excludeAll" }); }
folderHashes.push(fileHydrated.hash);
});
}
});
const sortedHashes = [...folderHashes].sort();
const data = await this.secureService.anchor(sortedHashes); const data = await this.secureService.anchor(sortedHashes);
const officeFolderAnchor = hydrateOfficeFolderAnchor(data); const officeFolderAnchor = hydrateOfficeFolderAnchor(data);
@ -248,23 +263,13 @@ export default class OfficeFoldersController extends ApiController {
return; return;
} }
const folderHashes: string[] = []; const sortedHashes = this.getValidatedDocumentHashes(officeFolder);
documents.forEach((document: any) => {
const documentHydrated = Document.hydrate<Document>(document, { strategy: "excludeAll" });
if (documentHydrated.document_status === "VALIDATED") {
documentHydrated.files?.forEach((file: any) => {
const fileHydrated = File.hydrate<File>(file, { strategy: "excludeAll" });
folderHashes.push(fileHydrated.hash);
});
}
});
if (folderHashes.length === 0) { if (sortedHashes.length === 0) {
this.httpNotFoundRequest(response, "No file hash to anchor"); this.httpNotFoundRequest(response, "No file hash to anchor");
return; return;
} }
const sortedHashes = [...folderHashes].sort();
const officeFolderAnchorFound = OfficeFolderAnchor.hydrate<OfficeFolderAnchor>(officeFolderFound.folder_anchor, { const officeFolderAnchorFound = OfficeFolderAnchor.hydrate<OfficeFolderAnchor>(officeFolderFound.folder_anchor, {
strategy: "excludeAll", strategy: "excludeAll",
}); });