[bug] Don't count refused documents when anchoring
All checks were successful
All checks were successful
This commit is contained in:
parent
a2fef18b82
commit
b70dee2afe
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user