diff --git a/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts b/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts index 6bab2586..40995ba6 100644 --- a/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts @@ -63,106 +63,55 @@ export default class DocumentTypeService extends AbstractService { }); } - public static getDocumentTypes(callback: (processes: any[]) => void): void { + public static getDocumentTypes(callback: (processes: Record) => void): void { // Check if we have valid cache - const items: any[] = this.getItems('_document_types_'); - if (items.length > 0) { - setTimeout(() => callback([...items]), 0); + const items: Record = this.getItems('_document_types_'); + if (Object.keys(items).length > 0) { + setTimeout(() => callback(items), 0); } - this.messageBus.getProcessesDecoded((values: any) => { + this.messageBus.getProcessesDecoded((_processId: string, values: any) => { return values['utype'] === 'documentType' && values['isDeleted'] === 'false'; - }).then(async (processes: any) => { - if (processes.length === 0) { - callback([...items]); + }).then(async (processes: Record) => { + if (Object.keys(processes).length === 0) { + callback(items); return; } - const updatedItems: any[] = [...items]; + const updatedItems: Record = { ...items }; - for (let processIndex = 0; processIndex < processes.length; processIndex++) { - const process = processes[processIndex]; - + for (const [processId, process] of Object.entries(processes)) { // Update cache - this.setItem('_document_types_', process); + this.setItem('_document_types_', processId, process); - const existingIndex: number = updatedItems.findIndex(item => item.processId === process.processId); - if (existingIndex >= 0) { - updatedItems[existingIndex] = process; - } else { - updatedItems.push(process); - } + updatedItems[processId] = process; } - callback([...updatedItems]); + callback(updatedItems); }); } - public static getDocumentTypeByProcessId(processId: string): Promise { - // Check if we have valid cache - const item: any = this.getItem('_document_types_', processId); - if (item) { - return Promise.resolve(item); + public static async updateDocumentType(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); + + const processData = await this.messageBus.getProcessData(processId); + + // Update cache + this.setItem('_document_types_', processId, processData); + } catch (error) { + console.error('Failed to update deed type:', error); + throw error; // Re-throw to allow caller to handle } - - return new Promise((resolve: (process: any) => void, reject: (error: string) => void) => { - this.messageBus.getProcessesDecoded((publicValues: any) => { - return publicValues['utype'] === 'documentType' && publicValues['isDeleted'] === 'false'; - }).then((processes: any[]) => { - // Find the process with matching processId - const process = processes.find((p: any) => p.processId === processId); - - if (!process) { - resolve(null); - return; - } - - // Update cache - this.setItem('_document_types_', process); - - resolve(process); - }).catch(reject); - }); - } - - // Keep the old method for backward compatibility but mark as deprecated - /** @deprecated Use getDocumentTypeByProcessId instead */ - public static getDocumentTypeByUid(uid: string): Promise { - // Check if we have valid cache - const item: any = this.getItem('_document_types_', 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'] === 'documentType' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false').then((processes: any[]) => { - if (processes.length === 0) { - resolve(null); - } else { - const process: any = processes[0]; - - // Update cache - this.setItem('_document_types_', process); - - resolve(process); - } - }).catch(reject); - }); - } - - public static updateDocumentType(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) => { - this.removeItem('_document_types_', process.processId); - - this.getDocumentTypeByProcessId(process.processId).then(resolve).catch(reject); - }).catch(reject); - }).catch(reject); - }).catch(reject); - }); } }