link an anchor to folder when requesting a new job

This commit is contained in:
Loïs Mansot 2023-09-29 15:36:31 +02:00
parent 49e8f1d89b
commit ab4fac8e01
No known key found for this signature in database
GPG Key ID: 8CF1F4150DDA726D
2 changed files with 51 additions and 4 deletions

View File

@ -5,15 +5,35 @@ 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 officeFoldersService: OfficeFoldersService) {
constructor(private secureService: SecureService, private officeFolderAnchorsRepository: OfficeFolderAnchorsRepository, private officeFoldersService: OfficeFoldersService) {
super();
}
@ -83,16 +103,30 @@ export default class OfficeFoldersController extends ApiController {
files: true,
},
},
folder_anchor: true,
};
const officeFolderFound = await this.officeFoldersService.getByUid(uid, query);
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) {
@ -101,9 +135,20 @@ export default class OfficeFoldersController extends ApiController {
}
const sortedHashes = [...folderHashes].sort();
const anchor = await this.secureService.anchor(sortedHashes);
const data = await this.secureService.anchor(sortedHashes);
this.httpSuccess(response, anchor);
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;

View File

@ -84,6 +84,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
uid: document.uid!,
})),
},
folder_anchor_uid: officeFolder.folder_anchor?.uid,
},
};
@ -93,6 +94,7 @@ export default class OfficeFoldersRepository extends BaseRepository {
stakeholders: true,
customers: true,
documents: true,
folder_anchor: true,
},
});
}