[bug] Don't count refused documents when anchoring
All checks were successful
Demo - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m51s
Demo - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 3s
Demo - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s

This commit is contained in:
Sosthene 2025-07-18 14:30:06 +02:00
parent a2fef18b82
commit b70dee2afe

View File

@ -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<OfficeFolder>(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>(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>(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>(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);
});
}
});
const sortedHashes = [...folderHashes].sort();
const data = await this.secureService.anchor(sortedHashes);