Refactor DeedTypeService

* async/await pattern where possible
* remove unused getDeedTypeByUid()
This commit is contained in:
Sosthene 2025-09-10 13:21:20 +02:00
parent e2cd9a46b0
commit caff36edcc

View File

@ -12,7 +12,7 @@ export default class DeedTypeService extends AbstractService {
super(); super();
} }
public static createDeedType(deedTypeData: any, validatorId: string): Promise<{ processId: string, processData: any }> { public static async createDeedType(deedTypeData: any, validatorId: string): Promise<{ processId: string, processData: any }> {
const ownerId: string = User.getInstance().getPairingId()!; const ownerId: string = User.getInstance().getPairingId()!;
const processData: any = { const processData: any = {
@ -51,61 +51,39 @@ export default class DeedTypeService extends AbstractService {
} }
}; };
return new Promise<any>((resolve: (processCreated: any) => void, reject: (error: string) => void) => { try {
this.messageBus.createProcess(processData, privateFields, roles).then((processCreated: any) => { const processCreated = await this.messageBus.createProcess(processData, privateFields, roles);
this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id).then(() => { await this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id);
this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id).then((_stateValidated: any) => { await this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id);
this.getDeedTypeByUid(processCreated.processData.uid).then(resolve).catch(reject); const finalProcessData = await this.messageBus.getProcessData(processCreated.processId);
}).catch(reject);
}).catch(reject); return { processId: processCreated.processId, processData: finalProcessData };
}).catch(reject); } catch (error) {
}); throw error;
}
} }
public static getDeedTypes(callback: (processes: any[]) => void, waitForAll: boolean = false): void { public static getDeedTypes(callback: (processes: any[]) => void): void {
// Check if we have valid cache // Check if we have valid cache
const items: any[] = this.getItems('_deed_types_'); const items: any[] = this.getItems('_deed_types_');
if (items.length > 0 && !waitForAll) { if (items.length > 0) {
setTimeout(() => callback([...items]), 0); setTimeout(() => callback([...items]), 0);
} }
this.messageBus.getProcessesDecoded((publicValues: any) => this.messageBus.getProcessesDecoded((values: any) => {
publicValues['uid'] && return values['utype'] === 'deedType'
publicValues['utype'] && && values['isDeleted'] === 'false';
publicValues['utype'] === 'deedType' && }).then(async (processes: any) => {
publicValues['isDeleted'] &&
publicValues['isDeleted'] === 'false' &&
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
).then(async (processes: any[]) => {
if (processes.length === 0) { if (processes.length === 0) {
if (waitForAll) { callback([...items]);
callback([...items]);
}
return; return;
} }
const updatedItems: any[] = [...items]; const updatedItems: any[] = [...items];
for (let processIndex = 0; processIndex < processes.length; processIndex++) { for (let processIndex = 0; processIndex < processes.length; processIndex++) {
let process = processes[processIndex]; const process = processes[processIndex];
if (!waitForAll) {
process = await this.completeDeedType(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.completeDeedType(process);
}
// Update cache // Update cache
this.setItem('_deed_types_', process); this.setItem('_deed_types_', process);
@ -115,90 +93,31 @@ export default class DeedTypeService extends AbstractService {
} else { } else {
updatedItems.push(process); updatedItems.push(process);
} }
if (!waitForAll) {
callback([...updatedItems]);
}
} }
if (waitForAll) { callback([...updatedItems]);
callback([...updatedItems]);
}
}); });
} }
public static getDeedTypeByUid(uid: string): Promise<any> { public static async updateDeedType(process: any, newData: any): Promise<void> {
// Check if we have valid cache try {
const item: any = this.getItem('_deed_types_', uid); const processUpdated = await this.messageBus.updateProcess(
if (item) { process.processId,
return Promise.resolve(item); { updated_at: new Date().toISOString(), ...newData },
[],
null
);
const newStateId: string = processUpdated.diffs[0]?.state_id;
await this.messageBus.notifyUpdate(process.processId, newStateId);
await this.messageBus.validateState(process.processId, newStateId);
// Update cache
this.setItem('_deed_types_', process);
} 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) =>
publicValues['uid'] &&
publicValues['uid'] === uid &&
publicValues['utype'] &&
publicValues['utype'] === 'deedType' &&
publicValues['isDeleted'] &&
publicValues['isDeleted'] === 'false'
).then(async (processes: any[]) => {
if (processes.length === 0) {
resolve(null);
} else {
let process: any = processes[0];
process = await this.completeDeedType(process);
// Update cache
this.setItem('_deed_types_', process);
resolve(process);
}
}).catch(reject);
});
} }
public static updateDeedType(process: any, newData: any): Promise<void> {
return new Promise<void>((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_', process);
resolve();
}).catch((error) => console.error('Failed to validate state', error));
}).catch((error) => console.error('Failed to notify update', error));
}).catch((error) => console.error('Failed to update', error));
});
}
private static async completeDeedType(process: any, progressCallback?: (processInProgress: any) => void): Promise<any> {
const progressiveProcess: any = JSON.parse(JSON.stringify(process));
if (process.processData.document_types && process.processData.document_types.length > 0) {
progressiveProcess.processData.document_types = [];
if (progressCallback) {
progressCallback(progressiveProcess);
}
for (const document_type of process.processData.document_types) {
const documentTypeData = (await DocumentTypeService.getDocumentTypeByUid(document_type.uid)).processData;
progressiveProcess.processData.document_types.push(documentTypeData);
// Remove duplicates
progressiveProcess.processData.document_types = progressiveProcess.processData.document_types
.filter((item: any, index: number) => progressiveProcess.processData.document_types.findIndex((t: any) => t.uid === item.uid) === index);
if (progressCallback) {
progressCallback(JSON.parse(JSON.stringify(progressiveProcess)));
}
}
process.processData.document_types = progressiveProcess.processData.document_types;
}
return process;
}
} }