From 0f28476aedbf221ab21e29e9102a9cc4cb6fc9ca Mon Sep 17 00:00:00 2001 From: Anthony Janin Date: Mon, 4 Aug 2025 17:54:36 +0200 Subject: [PATCH] Fix some issues --- .../Api/LeCoffreApi/sdk/AbstractService.ts | 2 - .../Api/LeCoffreApi/sdk/DeedTypeService.ts | 6 +- src/common/Api/LeCoffreApi/sdk/ImportData.ts | 89 ++++++++++++++++++- .../Layouts/LoginCallback/index.tsx | 75 ++++++++-------- 4 files changed, 131 insertions(+), 41 deletions(-) diff --git a/src/common/Api/LeCoffreApi/sdk/AbstractService.ts b/src/common/Api/LeCoffreApi/sdk/AbstractService.ts index a045da9d..17ddeada 100644 --- a/src/common/Api/LeCoffreApi/sdk/AbstractService.ts +++ b/src/common/Api/LeCoffreApi/sdk/AbstractService.ts @@ -1,5 +1,3 @@ -import { v4 as uuidv4 } from 'uuid'; - import MessageBus from 'src/sdk/MessageBus'; export default abstract class AbstractService { diff --git a/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts b/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts index 7fecfd5c..e4efd224 100644 --- a/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts @@ -176,14 +176,14 @@ export default class DeedTypeService extends AbstractService { } public static updateDeedType(process: any, newData: any): Promise { - // Update cache - this.setItem('_deed_types_', process); - return new Promise((resolve: () => void) => { this.messageBus.updateProcess(process.processId, { updated_at: new Date().toISOString(), ...newData }, [], null).then((processUpdated: any) => { const newStateId: string = processUpdated.diffs[0]?.state_id; this.messageBus.notifyUpdate(process.processId, newStateId).then(() => { this.messageBus.validateState(process.processId, newStateId).then(() => { + // Update cache + this.setItem('_deed_types_', processUpdated); + resolve(); }).catch(() => console.error('Failed to validate state')); }).catch(() => console.error('Failed to notify update')); diff --git a/src/common/Api/LeCoffreApi/sdk/ImportData.ts b/src/common/Api/LeCoffreApi/sdk/ImportData.ts index 3e4e13c3..890663dc 100644 --- a/src/common/Api/LeCoffreApi/sdk/ImportData.ts +++ b/src/common/Api/LeCoffreApi/sdk/ImportData.ts @@ -1,3 +1,8 @@ +import { v4 as uuidv4 } from 'uuid'; + +import User from 'src/sdk/User'; +import MessageBus from 'src/sdk/MessageBus'; + import RuleService from './RuleService'; import RuleGroupService from './RuleGroupService'; import RoleService from './RoleService'; @@ -19,7 +24,9 @@ export interface ProgressInfo { export default class ImportData { - public static async import(office: any, onProgress?: (info: ProgressInfo) => void): Promise { + protected static readonly messageBus: MessageBus = MessageBus.getInstance(); + + public static async import(office: any, validatorId: string, onProgress?: (info: ProgressInfo) => void): Promise { // Définir les étapes d'importation dynamiquement const importSteps = [ { @@ -81,6 +88,86 @@ export default class ImportData { results.push(result); } } + + if (!await this.isDone()) { + await this.done(validatorId); + } + } + + public static async isDone(): Promise { + return await this.messageBus.getProcessesDecoded((publicValues: any) => + publicValues['uid'] && + publicValues['utype'] && + publicValues['utype'] === 'importData' && + publicValues['isDeleted'] && publicValues['isDeleted'] === 'false' + ).then(async (processes: any[]) => processes.length > 0); + } + + private static async done(validatorId: string): Promise { + const ownerId = User.getInstance().getPairingId()!; + + const processData: any = { + uid: uuidv4(), + utype: 'importData', + isDeleted: 'false', + created_at: new Date().toISOString(), + updated_at: new Date().toISOString() + }; + + const privateFields: string[] = Object.keys(processData); + privateFields.splice(privateFields.indexOf('uid'), 1); + privateFields.splice(privateFields.indexOf('utype'), 1); + privateFields.splice(privateFields.indexOf('isDeleted'), 1); + + const roles: any = { + demiurge: { + members: [...[ownerId], validatorId], + validation_rules: [], + storages: [] + }, + owner: { + members: [ownerId], + validation_rules: [ + { + quorum: 0.5, + fields: [...privateFields, 'roles', 'uid', 'utype'], + min_sig_member: 1, + }, + ], + storages: [] + }, + validator: { + members: [validatorId], + validation_rules: [ + { + quorum: 0.5, + fields: ['idCertified', 'roles'], + min_sig_member: 1, + }, + { + quorum: 0.0, + fields: [...privateFields], + min_sig_member: 0, + }, + ], + storages: [] + }, + apophis: { + members: [ownerId], + validation_rules: [], + storages: [] + } + }; + + return new Promise((resolve: () => void, reject: (error: string) => void) => { + this.messageBus.createProcess(processData, privateFields, roles).then((processCreated: any) => { + this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id).then(() => { + this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id).then(() => { + resolve(); + }).catch(reject); + }).catch(reject); + }).catch(reject); + }); } private static async importRules(onProgress?: (progress: number, description?: string) => void): Promise { diff --git a/src/front/Components/Layouts/LoginCallback/index.tsx b/src/front/Components/Layouts/LoginCallback/index.tsx index c63c67e1..5bf98e20 100644 --- a/src/front/Components/Layouts/LoginCallback/index.tsx +++ b/src/front/Components/Layouts/LoginCallback/index.tsx @@ -63,6 +63,7 @@ export default function LoginCallBack() { office_status: 'ACTIVATED' }; const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0'; + OfficeService.createOffice(officeData, validatorId).then((process: any) => { if (process) { const office: any = process.processData; @@ -74,13 +75,50 @@ export default function LoginCallBack() { }); }; - const getCollaborator = async (collaboratorData: any) => { + const getCollaborator = async (idNotUser: any) => { return await new Promise(async (resolve: (role: any) => void) => { const processFound: any | null = await CollaboratorService.getCollaboratorBy({ idNot: idNotUser.idNot }); if (processFound) { resolve(processFound.processData); } else { const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0'; + const office: any = await getOffice(idNotUser); + + if (!await ImportData.isDone()) { + LoaderService.getInstance().hide(); + setShowProgress(true); + + await ImportData.import(office, validatorId, (info: ProgressInfo) => { + setProgressInfo(info); + }); + + setShowProgress(false); + LoaderService.getInstance().show(); + } + + const role: any = (await RoleService.getRoles()) + .map((process: any) => process.processData) + .find((role: any) => role.name === idNotUser.role.name); + + const officeRole: any = (await OfficeRoleService.getOfficeRoles()) + .map((process: any) => process.processData) + .filter((officeRole: any) => officeRole.office.uid === office.uid) + .find((officeRole: any) => officeRole.name === idNotUser.office_role.name); + + const collaboratorData: any = { + idNot: idNotUser.idNot, + contact: idNotUser.contact, + office: { + uid: office.uid + }, + role: { + uid: role.uid + }, + office_role: { + uid: officeRole.uid + } + }; + CollaboratorService.createCollaborator(collaboratorData, validatorId).then((process: any) => { if (process) { const collaborator: any = process.processData; @@ -235,40 +273,7 @@ export default function LoginCallBack() { LoaderService.getInstance().show(); MessageBus.getInstance().initMessageListener(); MessageBus.getInstance().isReady().then(async () => { - const office: any = await getOffice(idNotUser); - LoaderService.getInstance().hide(); - - setShowProgress(true); - await ImportData.import(office, (info: ProgressInfo) => { - setProgressInfo(info); - }); - setShowProgress(false); - - LoaderService.getInstance().show(); - - const role: any = (await RoleService.getRoles()) - .map((process: any) => process.processData) - .find((role: any) => role.name === idNotUser.role.name); - - const officeRole: any = (await OfficeRoleService.getOfficeRoles()) - .map((process: any) => process.processData) - .filter((officeRole: any) => officeRole.office.uid === office.uid) - .find((officeRole: any) => officeRole.name === idNotUser.office_role.name); - - const collaboratorData: any = { - idNot: idNotUser.idNot, - contact: idNotUser.contact, - office: { - uid: office.uid - }, - role: { - uid: role.uid - }, - office_role: { - uid: officeRole.uid - } - }; - const collaborator: any = await getCollaborator(collaboratorData); + const collaborator: any = await getCollaborator(idNotUser); UserStore.instance.connect(collaborator); MessageBus.getInstance().destroyMessageListener();