Refactor DeedTypeService
* async/await pattern where possible * remove unused getDeedTypeByUid()
This commit is contained in:
parent
e2cd9a46b0
commit
caff36edcc
@ -12,7 +12,7 @@ export default class DeedTypeService extends AbstractService {
|
||||
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 processData: any = {
|
||||
@ -51,60 +51,38 @@ export default class DeedTypeService extends AbstractService {
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise<any>((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.getDeedTypeByUid(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 getDeedTypes(callback: (processes: any[]) => void, waitForAll: boolean = false): void {
|
||||
public static getDeedTypes(callback: (processes: any[]) => void): void {
|
||||
// Check if we have valid cache
|
||||
const items: any[] = this.getItems('_deed_types_');
|
||||
if (items.length > 0 && !waitForAll) {
|
||||
if (items.length > 0) {
|
||||
setTimeout(() => callback([...items]), 0);
|
||||
}
|
||||
|
||||
this.messageBus.getProcessesDecoded((publicValues: any) =>
|
||||
publicValues['uid'] &&
|
||||
publicValues['utype'] &&
|
||||
publicValues['utype'] === 'deedType' &&
|
||||
publicValues['isDeleted'] &&
|
||||
publicValues['isDeleted'] === 'false' &&
|
||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
||||
).then(async (processes: any[]) => {
|
||||
this.messageBus.getProcessesDecoded((values: any) => {
|
||||
return values['utype'] === 'deedType'
|
||||
&& values['isDeleted'] === 'false';
|
||||
}).then(async (processes: any) => {
|
||||
if (processes.length === 0) {
|
||||
if (waitForAll) {
|
||||
callback([...items]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedItems: any[] = [...items];
|
||||
|
||||
for (let processIndex = 0; processIndex < processes.length; processIndex++) {
|
||||
let 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);
|
||||
}
|
||||
const process = processes[processIndex];
|
||||
|
||||
// Update cache
|
||||
this.setItem('_deed_types_', process);
|
||||
@ -115,90 +93,31 @@ export default class DeedTypeService extends AbstractService {
|
||||
} else {
|
||||
updatedItems.push(process);
|
||||
}
|
||||
|
||||
if (!waitForAll) {
|
||||
callback([...updatedItems]);
|
||||
}
|
||||
}
|
||||
|
||||
if (waitForAll) {
|
||||
callback([...updatedItems]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getDeedTypeByUid(uid: string): Promise<any> {
|
||||
// Check if we have valid cache
|
||||
const item: any = this.getItem('_deed_types_', uid);
|
||||
if (item) {
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
public static async updateDeedType(process: any, newData: any): Promise<void> {
|
||||
try {
|
||||
const processUpdated = await this.messageBus.updateProcess(
|
||||
process.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) =>
|
||||
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(() => {
|
||||
await this.messageBus.notifyUpdate(process.processId, newStateId);
|
||||
await this.messageBus.validateState(process.processId, newStateId);
|
||||
|
||||
// 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)));
|
||||
} catch (error) {
|
||||
console.error('Failed to update deed type:', error);
|
||||
throw error; // Re-throw to allow caller to handle
|
||||
}
|
||||
}
|
||||
|
||||
process.processData.document_types = progressiveProcess.processData.document_types;
|
||||
}
|
||||
|
||||
return process;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user