109 lines
2.8 KiB
TypeScript
109 lines
2.8 KiB
TypeScript
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;
|
|
}
|
|
}
|
|
}
|