Merge branch 'feature/anchoring-state' into dev (#104)

Two tasks to keep track of the anchoring state:

- store newly posted anchor and link them to a folder ;
- update an anchor after requesting its status on secure.
This commit is contained in:
Arnaud D. Natali 2023-09-29 16:46:58 +02:00 committed by GitHub
commit 9031aaf73c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 7 deletions

View File

@ -5,15 +5,35 @@ import { Service } from "typedi";
import { OfficeFolder } from "le-coffre-resources/dist/Notary"; import { OfficeFolder } from "le-coffre-resources/dist/Notary";
import { getFolderHashes } from "@Common/optics/notary"; import { getFolderHashes } from "@Common/optics/notary";
import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService"; import OfficeFoldersService from "@Services/notary/OfficeFoldersService/OfficeFoldersService";
import OfficeFolderAnchorsRepository from "@Repositories/OfficeFolderAnchorsRepository";
import SecureService from "@Services/common/SecureService/SecureService"; import SecureService from "@Services/common/SecureService/SecureService";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
import ruleHandler from "@App/middlewares/RulesHandler"; import ruleHandler from "@App/middlewares/RulesHandler";
import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; 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() @Controller()
@Service() @Service()
export default class OfficeFoldersController extends ApiController { export default class OfficeFoldersController extends ApiController {
constructor(private secureService: SecureService, private officeFoldersService: OfficeFoldersService) { constructor(private secureService: SecureService, private officeFolderAnchorsRepository: OfficeFolderAnchorsRepository, private officeFoldersService: OfficeFoldersService) {
super(); super();
} }
@ -83,16 +103,30 @@ export default class OfficeFoldersController extends ApiController {
files: true, files: true,
}, },
}, },
folder_anchor: true,
}; };
const officeFolderFound = await this.officeFoldersService.getByUid(uid, query); const officeFolderFound: any = await this.officeFoldersService.getByUid(uid, query);
if (!officeFolderFound) { if (!officeFolderFound) {
this.httpNotFoundRequest(response, "Office folder not found"); this.httpNotFoundRequest(response, "Office folder not found");
return; 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 officeFolder = OfficeFolder.hydrate<OfficeFolder>(officeFolderFound, { strategy: "excludeAll" });
const folderHashes = getFolderHashes(officeFolder); const folderHashes = getFolderHashes(officeFolder);
if (folderHashes.length === 0) { if (folderHashes.length === 0) {
@ -101,9 +135,20 @@ export default class OfficeFoldersController extends ApiController {
} }
const sortedHashes = [...folderHashes].sort(); 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) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;
@ -129,9 +174,10 @@ export default class OfficeFoldersController extends ApiController {
files: true, files: true,
}, },
}, },
folder_anchor: true,
}; };
const officeFolderFound = await this.officeFoldersService.getByUid(uid, query); const officeFolderFound: any = await this.officeFoldersService.getByUid(uid, query);
if (!officeFolderFound) { if (!officeFolderFound) {
this.httpNotFoundRequest(response, "Office folder not found"); this.httpNotFoundRequest(response, "Office folder not found");
@ -147,9 +193,31 @@ export default class OfficeFoldersController extends ApiController {
} }
const sortedHashes = [...folderHashes].sort(); const sortedHashes = [...folderHashes].sort();
const anchor = await this.secureService.verify(sortedHashes); const officeFolderAnchorFound = OfficeFolderAnchor.hydrate<OfficeFolderAnchor>(officeFolderFound.folder_anchor, {
strategy: "excludeAll",
});
this.httpSuccess(response, anchor); 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) { } catch (error) {
this.httpInternalError(response, error); this.httpInternalError(response, error);
return; return;

View File

@ -0,0 +1,68 @@
import Database from "@Common/databases/database";
import BaseRepository from "@Repositories/BaseRepository";
import { Service } from "typedi";
import { OfficeFolderAnchors, Prisma } from "@prisma/client";
import { OfficeFolderAnchor } from "le-coffre-resources/dist/SuperAdmin";
@Service()
export default class OfficeFolderAnchorsRepository extends BaseRepository {
constructor(private database: Database) {
super();
}
protected get model() {
return this.database.getClient().officeFolderAnchors;
}
protected get instanceDb() {
return this.database.getClient();
}
/**
* @description : Create new office folder anchor
*/
public async create(officeFolderAnchor: OfficeFolderAnchor): Promise<OfficeFolderAnchors> {
const createArgs: Prisma.OfficeFolderAnchorsCreateArgs = {
data: {
hash_sources: officeFolderAnchor.hash_sources,
root_hash: officeFolderAnchor.root_hash!,
blockchain: officeFolderAnchor.blockchain as OfficeFolderAnchors["blockchain"],
status: officeFolderAnchor.status as OfficeFolderAnchors["status"],
anchor_nb_try: officeFolderAnchor.anchor_nb_try,
tx_id: officeFolderAnchor.tx_id,
tx_link: officeFolderAnchor.tx_link,
tx_hash: officeFolderAnchor.tx_hash,
anchored_at: officeFolderAnchor.anchored_at,
},
};
return this.model.create({ ...createArgs });
}
/**
* @description : Update data of an office folder anchor
*/
public async update(officeFolderAnchorUid: string, officeFolderAnchor: OfficeFolderAnchor): Promise<OfficeFolderAnchors> {
const updateArgs: Prisma.OfficeFolderAnchorsUpdateArgs = {
where: {
uid: officeFolderAnchorUid,
},
data: {
blockchain: officeFolderAnchor.blockchain as OfficeFolderAnchors["blockchain"],
status: officeFolderAnchor.status as OfficeFolderAnchors["status"],
anchor_nb_try: officeFolderAnchor.anchor_nb_try,
tx_id: officeFolderAnchor.tx_id,
tx_link: officeFolderAnchor.tx_link,
tx_hash: officeFolderAnchor.tx_hash,
anchored_at: officeFolderAnchor.anchored_at,
},
};
return this.model.update({
...updateArgs,
});
}
}

View File

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