diff --git a/src/app/api/notary/OfficeFolderAnchorsController.ts b/src/app/api/notary/OfficeFolderAnchorsController.ts new file mode 100644 index 00000000..086162dc --- /dev/null +++ b/src/app/api/notary/OfficeFolderAnchorsController.ts @@ -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(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(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; + } + } +} diff --git a/src/app/index.ts b/src/app/index.ts index dc98b2e4..fa011b2c 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -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) }, };