227 lines
6.6 KiB
TypeScript
227 lines
6.6 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 OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository";
|
|
import SecureService from "@Services/common/SecureService/SecureService";
|
|
import authHandler from "@App/middlewares/AuthHandler";
|
|
import ruleHandler from "@App/middlewares/RulesHandler";
|
|
import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler";
|
|
import OfficeFolderAnchor from "le-coffre-resources/dist/Notary/OfficeFolderAnchor";
|
|
|
|
const hydrateOfficeFolderAnchor = (data: any): OfficeFolderAnchor =>
|
|
OfficeFolderAnchor.hydrate<OfficeFolderAnchor>(
|
|
{
|
|
hash_sources: data.hash_sources,
|
|
root_hash: data.root_hash,
|
|
|
|
blockchain: data.transactions[0].blockchain,
|
|
status: data.transactions[0].status,
|
|
|
|
anchor_nb_try: data.transactions[0].anchor_nb_try,
|
|
tx_id: data.transactions[0].tx_id.toString(),
|
|
tx_link: data.transactions[0].tx_link,
|
|
tx_hash: data.transactions[0].tx_hash,
|
|
|
|
anchored_at: data.transactions[0].anchoring_timestamp,
|
|
},
|
|
{ strategy: "excludeAll" },
|
|
);
|
|
@Controller()
|
|
@Service()
|
|
export default class OfficeFoldersController extends ApiController {
|
|
constructor(private secureService: SecureService, private officeFolderAnchorsRepository: OfficeFolderAnchorsRepository, private officeFoldersService: OfficeFoldersService) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Download a folder anchoring proof document
|
|
*/
|
|
@Get("/api/v1/notary/anchors/download/:uid", [authHandler, ruleHandler, folderHandler])
|
|
protected async download(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 buffer = await this.secureService.download(sortedHashes);
|
|
|
|
response.setHeader('Content-Type', 'application/pdf');
|
|
this.httpSuccess(response, buffer);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Create a new folder anchor
|
|
*/
|
|
@Post("/api/v1/notary/anchors/:uid", [authHandler, ruleHandler, folderHandler])
|
|
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,
|
|
},
|
|
},
|
|
folder_anchor: true,
|
|
};
|
|
|
|
const officeFolderFound: any = await this.officeFoldersService.getByUid(uid, query);
|
|
|
|
if (!officeFolderFound) {
|
|
this.httpNotFoundRequest(response, "Office folder not found");
|
|
return;
|
|
}
|
|
|
|
const officeFolderAnchorFound = OfficeFolderAnchor.hydrate<OfficeFolderAnchor>(officeFolderFound.folder_anchor, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
if (officeFolderAnchorFound) {
|
|
this.httpBadRequest(response, {
|
|
error: "Office folder already anchored",
|
|
folder_anchor: officeFolderAnchorFound,
|
|
});
|
|
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 data = await this.secureService.anchor(sortedHashes);
|
|
|
|
const officeFolderAnchor = hydrateOfficeFolderAnchor(data);
|
|
|
|
const newOfficeFolderAnchor = await this.officeFolderAnchorsRepository.create(
|
|
officeFolderAnchor
|
|
);
|
|
|
|
await this.officeFoldersService.update(
|
|
uid,
|
|
OfficeFolder.hydrate<OfficeFolder>({ uid: uid, folder_anchor: newOfficeFolderAnchor }, { strategy: "excludeAll" }),
|
|
);
|
|
|
|
this.httpSuccess(response, officeFolderAnchor);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Verify a folder anchor status
|
|
*/
|
|
@Get("/api/v1/notary/anchors/:uid", [authHandler, ruleHandler, folderHandler])
|
|
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,
|
|
},
|
|
},
|
|
folder_anchor: true,
|
|
};
|
|
|
|
const officeFolderFound: any = 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 officeFolderAnchorFound = OfficeFolderAnchor.hydrate<OfficeFolderAnchor>(officeFolderFound.folder_anchor, {
|
|
strategy: "excludeAll",
|
|
});
|
|
|
|
if (!officeFolderAnchorFound || !officeFolderAnchorFound.uid) {
|
|
this.httpNotFoundRequest(response, {error: "Not anchored", hash_sources: sortedHashes});
|
|
return;
|
|
}
|
|
|
|
const data = await this.secureService.verify(sortedHashes);
|
|
|
|
if (data.errors || data.transactions.length === 0) {
|
|
this.httpNotFoundRequest(response, {error: "Not anchored", hash_sources: sortedHashes});
|
|
return;
|
|
}
|
|
|
|
const officeFolderAnchor = hydrateOfficeFolderAnchor(data);
|
|
|
|
const updatedOfficeFolderAnchor = await this.officeFolderAnchorsRepository.update(
|
|
officeFolderAnchorFound.uid,
|
|
officeFolderAnchor
|
|
);
|
|
|
|
this.httpSuccess(response, updatedOfficeFolderAnchor);
|
|
return;
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|