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
const items: any[] = this.getItems('_document_types_');
if (items.length > 0) {
setTimeout(() => callback([...items]), 0);
const items: Record<string, any> = 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<string, any>) => {
if (Object.keys(processes).length === 0) {
callback(items);
return;
}
const updatedItems: any[] = [...items];
for (let processIndex = 0; processIndex < processes.length; processIndex++) {
const process = processes[processIndex];
const updatedItems: Record<string, any> = { ...items };
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<any> {
// 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<void> {
try {
const processUpdated = await this.messageBus.updateProcess(
processId,
{ updated_at: new Date().toISOString(), ...newData },
[],
null
);
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);
await this.messageBus.notifyUpdate(processId, newStateId);
await this.messageBus.validateState(processId, newStateId);
this.getDocumentTypeByProcessId(process.processId).then(resolve).catch(reject);
}).catch(reject);
}).catch(reject);
}).catch(reject);
});
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
}
}
}