add OfficeFolderAnchorsController

- create a job for anchoring all hashes in a folder
- monitor the status of an anchoring
This commit is contained in:
Loïs Mansot 2023-09-21 16:33:30 +02:00
parent b448aea01e
commit dc5eb4cb52
No known key found for this signature in database
GPG Key ID: 8CF1F4150DDA726D
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,108 @@
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 { OfficeFolder } from "le-coffre-resources/dist/Notary";
import { getFolderHashes } from "@Common/optics/notary";
import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
import SecureService from "@Services/common/SecureService/SecureService";
@Controller()
@Service()
export default class OfficeFoldersController extends ApiController {
constructor(private secureService: SecureService, private officeFoldersService: OfficeFoldersService) {
super();
}
/**
* @description Create a new folder anchor
*/
@Post("/api/v1/notary/anchors/:uid")
protected async post(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const query = {
documents: {
include: {
files: true,
},
},
};
const officeFolderFound = await this.officeFoldersService.getByUid(uid, query);
if (!officeFolderFound) {
this.httpNotFoundRequest(response, "Office folder not found");
return;
}
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound, { strategy: "excludeAll" });
const folderHashes = getFolderHashes(officeFolder);
if (folderHashes.length === 0) {
this.httpNotFoundRequest(response, "No file hash to anchor");
return;
}
const sortedHashes = [...folderHashes].sort();
const anchor = await this.secureService.anchor(sortedHashes);
this.httpSuccess(response, anchor);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
/**
* @description Verify a folder anchor status
*/
@Get("/api/v1/notary/anchors/:uid")
protected async get(req: Request, response: Response) {
try {
const uid = req.params["uid"];
if (!uid) {
this.httpBadRequest(response, "No uid provided");
return;
}
const query = {
documents: {
include: {
files: true,
},
},
};
const officeFolderFound = await this.officeFoldersService.getByUid(uid, query);
if (!officeFolderFound) {
this.httpNotFoundRequest(response, "Office folder not found");
return;
}
const officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound, { strategy: "excludeAll" });
const folderHashes = getFolderHashes(officeFolder);
if (folderHashes.length === 0) {
this.httpNotFoundRequest(response, "No file hash to anchor");
return;
}
const sortedHashes = [...folderHashes].sort();
const anchor = await this.secureService.verify(sortedHashes);
this.httpSuccess(response, anchor);
} catch (error) {
this.httpInternalError(response, error);
return;
}
}
}

View File

@ -41,6 +41,7 @@ import OfficeRolesControllerNotary from "./api/notary/OfficeRolesController";
import FilesControllerCustomer from "./api/customer/FilesController";
import DocumentsControllerCustomer from "./api/customer/DocumentsController";
import OfficeFoldersController from "./api/customer/OfficeFoldersController";
import OfficeFolderAnchorsController from "./api/notary/OfficeFolderAnchorsController";
import CustomersController from "./api/customer/CustomersController";
import AppointmentsController from "./api/super-admin/AppointmentsController";
import VotesController from "./api/super-admin/VotesController";
@ -98,6 +99,7 @@ export default {
Container.get(FilesControllerCustomer);
Container.get(DocumentsControllerCustomer);
Container.get(OfficeFoldersController);
Container.get(OfficeFolderAnchorsController);
Container.get(CustomersController)
},
};