Update DocumentTypeService

This commit is contained in:
Sosthene 2025-09-11 14:09:44 +02:00
parent 02f6a10b9d
commit 9a3685ec4f

View File

@ -63,106 +63,55 @@ export default class DocumentTypeService extends AbstractService {
}); });
} }
public static getDocumentTypes(callback: (processes: any[]) => void): void { public static getDocumentTypes(callback: (processes: Record<string, any>) => void): void {
// Check if we have valid cache // Check if we have valid cache
const items: any[] = this.getItems('_document_types_'); const items: Record<string, any> = this.getItems('_document_types_');
if (items.length > 0) { if (Object.keys(items).length > 0) {
setTimeout(() => callback([...items]), 0); setTimeout(() => callback(items), 0);
} }
this.messageBus.getProcessesDecoded((values: any) => { this.messageBus.getProcessesDecoded((_processId: string, values: any) => {
return values['utype'] === 'documentType' return values['utype'] === 'documentType'
&& values['isDeleted'] === 'false'; && values['isDeleted'] === 'false';
}).then(async (processes: any) => { }).then(async (processes: Record<string, any>) => {
if (processes.length === 0) { if (Object.keys(processes).length === 0) {
callback([...items]); callback(items);
return; return;
} }
const updatedItems: any[] = [...items]; const updatedItems: Record<string, any> = { ...items };
for (let processIndex = 0; processIndex < processes.length; processIndex++) {
const process = processes[processIndex];
for (const [processId, process] of Object.entries(processes)) {
// Update cache // Update cache
this.setItem('_document_types_', process); this.setItem('_document_types_', processId, process);
const existingIndex: number = updatedItems.findIndex(item => item.processId === process.processId); updatedItems[processId] = process;
if (existingIndex >= 0) {
updatedItems[existingIndex] = process;
} else {
updatedItems.push(process);
}
} }
callback([...updatedItems]); callback(updatedItems);
}); });
} }
public static getDocumentTypeByProcessId(processId: string): Promise<any> { public static async updateDocumentType(processId: string, newData: any): Promise<void> {
// Check if we have valid cache try {
const item: any = this.getItem('_document_types_', processId); const processUpdated = await this.messageBus.updateProcess(
if (item) { processId,
return Promise.resolve(item); { 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<any>((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<any> {
// Check if we have valid cache
const item: any = this.getItem('_document_types_', uid);
if (item) {
return Promise.resolve(item);
}
return new Promise<any>((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<void> {
return new Promise<void>((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);
});
} }
} }