340 lines
10 KiB
TypeScript
340 lines
10 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 { 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";
|
|
import FilesService from "@Services/common/FilesService/FilesService";
|
|
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";
|
|
import NotificationBuilder from "@Common/notifications/NotificationBuilder";
|
|
import { Prisma } from "@prisma/client";
|
|
import OfficeFolderAnchorsService from "@Services/notary/OfficeFolderAnchorsService/OfficeFolderAnchorsService";
|
|
import Zip from "adm-zip";
|
|
|
|
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() ?? undefined,
|
|
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 officeFolderAnchorsService: OfficeFolderAnchorsService,
|
|
private officeFoldersService: OfficeFoldersService,
|
|
private filesService: FilesService,
|
|
private notificationBuilder: NotificationBuilder,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* @description Download a folder anchoring proof document along with all accessible files
|
|
*/
|
|
@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,
|
|
},
|
|
},
|
|
office: 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);
|
|
const folderFilesUid = getFolderFilesUid(officeFolder);
|
|
|
|
if (folderHashes.length === 0) {
|
|
this.httpNotFoundRequest(response, "No file hash to anchor");
|
|
return;
|
|
}
|
|
|
|
const sortedHashes = [...folderHashes].sort();
|
|
const anchoringProof = await this.secureService.download(sortedHashes, officeFolder.office!.name);
|
|
|
|
|
|
const addFileToZip =
|
|
(zip: Zip) =>
|
|
(uid: string): Promise<void> =>
|
|
(async () => {
|
|
const data = await this.filesService.download(uid);
|
|
if (!data?.buffer) return;
|
|
zip.addFile(`Documents du client/${uid}-${data.file.file_name}`, data.buffer);
|
|
})();
|
|
|
|
const uids: string[] = folderFilesUid.filter((uid): uid is string => uid !== undefined);
|
|
|
|
const zip = new Zip();
|
|
zip.addFile("Certificat de dépôt du dossier.pdf", anchoringProof);
|
|
await Promise.allSettled(uids.map(addFileToZip(zip)));
|
|
|
|
response.setHeader("Content-Type", "application/zip");
|
|
this.httpSuccess(response, zip.toBuffer());
|
|
} 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,
|
|
},
|
|
},
|
|
documents_notary: {
|
|
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 documents = officeFolder.documents ?? [];
|
|
|
|
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 (hasInvalidDocument) {
|
|
this.httpBadRequest(response, "OfficeFolder has non validated documents");
|
|
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);
|
|
|
|
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 getOneByUid(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 documents = officeFolder.documents ?? [];
|
|
|
|
if (documents.length === 0) {
|
|
this.httpNotFoundRequest(response, "Office folder has no documents");
|
|
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);
|
|
});
|
|
}
|
|
});
|
|
|
|
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,
|
|
);
|
|
|
|
if (officeFolderAnchorFound.status !== "VERIFIED_ON_CHAIN" && officeFolderAnchor.status === "VERIFIED_ON_CHAIN")
|
|
this.notificationBuilder.sendFolderAnchoredNotification(officeFolderFound);
|
|
|
|
this.httpSuccess(response, updatedOfficeFolderAnchor);
|
|
return;
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description Get all folders
|
|
*/
|
|
@Get("/api/v1/notary/anchors", [authHandler, ruleHandler])
|
|
protected async get(req: Request, response: Response) {
|
|
try {
|
|
//get query
|
|
let query: Prisma.OfficeFolderAnchorsFindManyArgs = {};
|
|
if (req.query["q"]) {
|
|
query = JSON.parse(req.query["q"] as string);
|
|
if (query.where?.uid) {
|
|
this.httpBadRequest(response, "You can't filter by uid");
|
|
return;
|
|
}
|
|
}
|
|
|
|
query.where = {
|
|
...query.where,
|
|
folder: {
|
|
office_uid: req.body.user.office_Id as string,
|
|
},
|
|
};
|
|
|
|
//call service to get prisma entity
|
|
const officeFolderAnchorsEntities: OfficeFolderAnchor[] = await this.officeFolderAnchorsService.get(query);
|
|
|
|
//Hydrate ressource with prisma entity
|
|
const officeFolderAnchors = OfficeFolderAnchor.hydrateArray<OfficeFolderAnchor>(officeFolderAnchorsEntities, {
|
|
strategy: "excludeAll",
|
|
});
|
|
//success
|
|
this.httpSuccess(response, officeFolderAnchors);
|
|
} catch (error) {
|
|
this.httpInternalError(response, error);
|
|
return;
|
|
}
|
|
}
|
|
}
|