From 4540ac726a19a315cbed944e67bbf6684072e321 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Thu, 11 Sep 2025 14:10:23 +0200 Subject: [PATCH] Update FolderService --- .../Api/LeCoffreApi/sdk/FolderService.ts | 320 +++++------------- 1 file changed, 86 insertions(+), 234 deletions(-) diff --git a/src/common/Api/LeCoffreApi/sdk/FolderService.ts b/src/common/Api/LeCoffreApi/sdk/FolderService.ts index 9413cc73..41cb6d11 100644 --- a/src/common/Api/LeCoffreApi/sdk/FolderService.ts +++ b/src/common/Api/LeCoffreApi/sdk/FolderService.ts @@ -3,13 +3,7 @@ import { v4 as uuidv4 } from 'uuid'; import User from 'src/sdk/User'; import AbstractService from './AbstractService'; -import CustomerService from './CustomerService'; -import CollaboratorService from './CollaboratorService'; -import DeedTypeService from './DeedTypeService'; -import DocumentTypeService from './DocumentTypeService'; -import DocumentService from './DocumentService'; -import FileService from './FileService'; -import NoteService from './NoteService'; +import { DEFAULT_STORAGE_URLS, DEFAULT_VALIDATOR_ID } from '@Front/Config/AppConstants'; export default class FolderService extends AbstractService { @@ -17,7 +11,7 @@ export default class FolderService extends AbstractService { super(); } - public static createFolder(folderData: any, stakeholdersId: string[], customersId: string[]): Promise { + public static async createFolder(folderData: any, customersId: string[]): Promise<{ processId: string, processData: any }> { const ownerId: string = User.getInstance().getPairingId()!; const processData: any = { @@ -30,9 +24,7 @@ export default class FolderService extends AbstractService { }; 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 allFields: string[] = [...privateFields, 'roles']; const roles: any = { demiurge: { @@ -40,135 +32,101 @@ export default class FolderService extends AbstractService { validation_rules: [], storages: [] }, - owner: { - members: [ownerId], + owners: { + members: [ownerId, DEFAULT_VALIDATOR_ID], validation_rules: [ { - quorum: 0.5, - fields: [...privateFields, 'roles', 'uid', 'utype'], - min_sig_member: 1, + quorum: 0.01, + fields: allFields, + min_sig_member: 0.01, }, ], - storages: [] + storages: [...DEFAULT_STORAGE_URLS] }, - stakeholders: { - members: stakeholdersId, - validation_rules: [ - { - quorum: 0.5, - fields: ['documents', 'motes'], - min_sig_member: 1, - }, - ], - storages: [] + guests: { + members: [], + validation_rules: [{ + quorum: 0.01, + fields: allFields, + min_sig_member: 0.01, + }], + storages: [...DEFAULT_STORAGE_URLS] }, customers: { members: customersId, validation_rules: [ { quorum: 0.0, - fields: privateFields, + fields: allFields, min_sig_member: 0.0, }, ], - storages: [] + storages: [...DEFAULT_STORAGE_URLS] }, apophis: { - members: [ownerId], + members: [ownerId, DEFAULT_VALIDATOR_ID], validation_rules: [], storages: [] } }; - return new Promise((resolve: (processCreated: any) => 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((_stateValidated: any) => { - this.getFolderByUid(processCreated.processData.uid).then(resolve).catch(reject); - }).catch(reject); - }).catch(reject); - }).catch(reject); - }); + try { + const processCreated = await this.messageBus.createProcess(processData, privateFields, roles); + await this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id); + await this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id); + const finalProcessData = await this.messageBus.getProcessData(processCreated.processId); + + return { processId: processCreated.processId, processData: finalProcessData }; + } catch (error) { + throw error; + } } - public static getFolders(callback: (processes: any[]) => void, waitForAll: boolean = false): void { + public static getFolders(callback: (processes: Record) => void): void { // Check if we have valid cache - const items: any[] = this.getItems('_folders_'); - if (items.length > 0 && !waitForAll) { - setTimeout(() => callback([...items]), 0); + const items: Record = this.getItems('_folders_'); + if (Object.keys(items).length > 0) { + setTimeout(() => callback(items), 0); } - this.messageBus.getProcessesDecoded((publicValues: any) => - publicValues['uid'] && - publicValues['utype'] && - publicValues['utype'] === 'folder' && - publicValues['isDeleted'] && - publicValues['isDeleted'] === 'false' && - !items.map((item: any) => item.processData.uid).includes(publicValues['uid']) - ).then(async (processes: any[]) => { - if (processes.length === 0) { - if (waitForAll) { - callback([...items]); - } + this.messageBus.getProcessesDecoded((_processId: string, values: any) => { + return values['utype'] + && values['utype'] === 'folder' + && values['isDeleted'] + && values['isDeleted'] === 'false'; + }).then(async (processes: Record) => { + if (Object.keys(processes).length === 0) { + callback(items); return; } - const updatedItems: any[] = [...items]; + console.log('[FolderService/getFolders] processes', processes); - for (let processIndex = 0; processIndex < processes.length; processIndex++) { - let process = processes[processIndex]; - - if (!waitForAll) { - process = await this.completeFolder(process, (processInProgress: any) => { - const currentItems: any[] = [...updatedItems]; - - const existingIndex: number = currentItems.findIndex(item => item.processData?.uid === processInProgress.processData?.uid); - if (existingIndex >= 0) { - currentItems[existingIndex] = processInProgress; - } else { - currentItems.push(processInProgress); - } - - callback(currentItems); - }); - } else { - process = await this.completeFolder(process); - } + const updatedItems: Record = { ...items }; + for (const [processId, process] of Object.entries(processes)) { // Update cache - this.setItem('_folders_', process); + this.setItem('_folders_', processId, process); - const existingIndex: number = updatedItems.findIndex(item => item.processData?.uid === process.processData?.uid); - if (existingIndex >= 0) { - updatedItems[existingIndex] = process; - } else { - updatedItems.push(process); - } - - if (!waitForAll) { - callback([...updatedItems]); - } + updatedItems[processId] = process; } - if (waitForAll) { - callback([...updatedItems]); - } + callback(updatedItems); }); } - public static getFoldersBy(whereClause: { [path: string]: any }): Promise { - return new Promise((resolve: (folders: any[]) => void) => { - this.getFolders((processes: any[]) => { - if (processes.length === 0) { - resolve([]); + public static getFoldersBy(whereClause: { [path: string]: any }): Promise> { + return new Promise>((resolve: (folders: Record) => void) => { + this.getFolders((processes: Record) => { + if (Object.keys(processes).length === 0) { + resolve({}); } else { - resolve(processes.filter((process: any) => { - const folder: any = process.processData; - + const filteredEntries = Object.entries(processes).filter(([processId, process]) => { for (const path in whereClause) { const paths: string[] = path.split('.'); - let value: any = folder; + let value: any = process; + value['processId'] = processId; for (let i = 0; i < paths.length; i++) { const currentPath = paths[i]; if (!currentPath || value === undefined || value === null) { @@ -183,146 +141,40 @@ export default class FolderService extends AbstractService { } return true; - })); + }); + + // Convert filtered entries back to a Record + const filteredProcesses: Record = {}; + filteredEntries.forEach(([processId, process]) => { + filteredProcesses[processId] = process; + }); + + resolve(filteredProcesses); } - }, true); - }); - } - - public static getFolderByUid(uid: string): Promise { - // Check if we have valid cache - const item: any = this.getItem('_folders_', uid); - if (item) { - return Promise.resolve(item); - } - - return new Promise((resolve: (process: any) => void, reject: (error: string) => void) => { - this.messageBus.getProcessesDecoded((publicValues: any) => - publicValues['uid'] && - publicValues['uid'] === uid && - publicValues['utype'] && - publicValues['utype'] === 'folder' && - publicValues['isDeleted'] && - publicValues['isDeleted'] === 'false' - ).then(async (processes: any[]) => { - if (processes.length === 0) { - resolve(null); - } else { - let process: any = processes[0]; - process = await this.completeFolder(process); - - // Update cache - this.setItem('_folders_', process); - - resolve(process); - } - }).catch(reject); - }); - } - - public static updateFolder(process: any, newData: any): Promise { - return new Promise((resolve: () => void, reject: (error: string) => 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((_stateValidated) => { - const folderUid: string = process.processData.uid; - this.removeItem('_folders_', folderUid); - - this.getFolderByUid(folderUid).then(resolve).catch(reject); - }).catch(reject); - }).catch(reject); - }).catch(reject); - }); - } - - public static refreshFolderByUid(uid: string): Promise { - return new Promise((resolve: () => void, reject: (error: string) => void) => { - this.getFolderByUid(uid).then((process: any) => { - this.updateFolder(process, {}).then(resolve).catch(reject); - }).catch(reject); - }); - } - - private static async completeFolder(process: any, progressCallback?: (processInProgress: any) => void): Promise { - const progressiveProcess: any = JSON.parse(JSON.stringify(process)); - - if (process.processData.customers && process.processData.customers.length > 0) { - process.processData.customers = await new Promise(async (resolve: (customers: any[]) => void) => { - const customers: any[] = []; - for (const customer of process.processData.customers) { - customers.push((await CustomerService.getCustomerByUid(customer.uid)).processData); - } - resolve(customers); }); + }); + } - if (process.processData.customers && process.processData.customers.length > 0) { - const documents: any[] = (await DocumentService.getDocuments()).map((process: any) => process.processData); - for (const customer of process.processData.customers) { - customer.documents = documents.filter((document: any) => (document.depositor && document.depositor.uid === customer.uid) || (document.customer && document.customer.uid === customer.uid)); + public static async updateFolder(processId: string, newData: any): Promise { + try { + const processUpdated = await this.messageBus.updateProcess( + processId, + { updated_at: new Date().toISOString(), ...newData }, + [], + null + ); + + const newStateId: string = processUpdated.diffs[0]?.state_id; + await this.messageBus.notifyUpdate(processId, newStateId); + await this.messageBus.validateState(processId, newStateId); - for (const document of customer.documents) { - if (document.document_type) { - document.document_type = (await DocumentTypeService.getDocumentTypeByUid(document.document_type.uid)).processData; - } - - if (document.files && document.files.length > 0) { - const files: any[] = []; - for (const file of document.files) { - files.push({ uid: file.uid, file_name: (await FileService.getFileByUid(file.uid)).processData.file_name }); - } - document.files = files; - } - } - } - } - - if (progressCallback) { - progressiveProcess.processData.customers = process.processData.customers; - progressCallback(JSON.parse(JSON.stringify(progressiveProcess))); - } + const processData = await this.messageBus.getProcessData(processId); + + // Update cache + this.setItem('_folders_', processId, processData); + } catch (error) { + console.error('Failed to update folder:', error); + throw error; // Re-throw to allow caller to handle } - - if (process.processData.stakeholders && process.processData.stakeholders.length > 0) { - process.processData.stakeholders = await new Promise(async (resolve: (stakeholders: any[]) => void) => { - const stakeholders: any[] = []; - for (const stakeholder of process.processData.stakeholders) { - stakeholders.push((await CollaboratorService.getCollaboratorByUid(stakeholder.uid)).processData); - } - resolve(stakeholders); - }); - - if (progressCallback) { - progressiveProcess.processData.stakeholders = process.processData.stakeholders; - progressCallback(JSON.parse(JSON.stringify(progressiveProcess))); - } - } - - if (process.processData.deed && process.processData.deed.deed_type) { - const deed_type: any = (await DeedTypeService.getDeedTypeByUid(process.processData.deed.deed_type.uid)).processData; - process.processData.deed.deed_type = deed_type; - - if (deed_type.document_types && deed_type.document_types.length > 0) { - // Remove duplicates - process.processData.deed.document_types = deed_type.document_types.filter((item: any, index: number) => deed_type.document_types.findIndex((t: any) => t.uid === item.uid) === index); - } - - if (progressCallback) { - progressiveProcess.processData.deed = process.processData.deed; - progressCallback(JSON.parse(JSON.stringify(progressiveProcess))); - } - } - - const notes: any[] = (await NoteService.getNotes()).map((process: any) => process.processData); - if (notes.length > 0) { - process.processData.notes = notes.filter((note: any) => note.folder.uid === process.processData.uid); - - if (progressCallback) { - progressiveProcess.processData.notes = process.processData.notes; - progressCallback(JSON.parse(JSON.stringify(progressiveProcess))); - } - } - - return process; } }