Update CustomerService
This commit is contained in:
parent
6a7918d081
commit
2450d674e2
@ -3,6 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
|
|||||||
import User from 'src/sdk/User';
|
import User from 'src/sdk/User';
|
||||||
|
|
||||||
import AbstractService from './AbstractService';
|
import AbstractService from './AbstractService';
|
||||||
|
import { DEFAULT_STORAGE_URLS } from '@Front/Config/AppConstants';
|
||||||
|
|
||||||
export default class CustomerService extends AbstractService {
|
export default class CustomerService extends AbstractService {
|
||||||
|
|
||||||
@ -10,7 +11,7 @@ export default class CustomerService extends AbstractService {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static createCustomer(customerData: any, validatorId: string): Promise<any> {
|
public static async createCustomer(customerData: 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 = {
|
||||||
@ -23,114 +24,72 @@ export default class CustomerService extends AbstractService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const privateFields: string[] = Object.keys(processData);
|
const privateFields: string[] = Object.keys(processData);
|
||||||
privateFields.splice(privateFields.indexOf('uid'), 1);
|
const allFields: string[] = [...privateFields, 'roles'];
|
||||||
privateFields.splice(privateFields.indexOf('utype'), 1);
|
|
||||||
privateFields.splice(privateFields.indexOf('isDeleted'), 1);
|
|
||||||
|
|
||||||
const roles: any = {
|
const roles: any = {
|
||||||
demiurge: {
|
demiurge: {
|
||||||
members: [...[ownerId], validatorId],
|
members: [ownerId, validatorId],
|
||||||
validation_rules: [],
|
validation_rules: [],
|
||||||
storages: []
|
storages: []
|
||||||
},
|
},
|
||||||
owner: {
|
owner: {
|
||||||
members: [ownerId],
|
members: [ownerId, validatorId],
|
||||||
validation_rules: [
|
validation_rules: [
|
||||||
{
|
{
|
||||||
quorum: 0.5,
|
quorum: 1,
|
||||||
fields: [...privateFields, 'roles', 'uid', 'utype'],
|
fields: allFields,
|
||||||
min_sig_member: 1,
|
min_sig_member: 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
storages: []
|
storages: [...DEFAULT_STORAGE_URLS]
|
||||||
},
|
|
||||||
validator: {
|
|
||||||
members: [validatorId],
|
|
||||||
validation_rules: [
|
|
||||||
{
|
|
||||||
quorum: 0.5,
|
|
||||||
fields: ['idCertified', 'roles'],
|
|
||||||
min_sig_member: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
quorum: 0.0,
|
|
||||||
fields: [...privateFields],
|
|
||||||
min_sig_member: 0,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
storages: []
|
|
||||||
},
|
},
|
||||||
apophis: {
|
apophis: {
|
||||||
members: [ownerId],
|
members: [ownerId, validatorId],
|
||||||
validation_rules: [],
|
validation_rules: [],
|
||||||
storages: []
|
storages: []
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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.getCustomerByUid(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[processCreated.processId] };
|
||||||
}).catch(reject);
|
} catch (error) {
|
||||||
});
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getCustomers(): Promise<any[]> {
|
public static getCustomers(callback: (processes: Record<string, any>) => void): void {
|
||||||
// Check if we have valid cache
|
// Check if we have valid cache
|
||||||
const items: any[] = this.getItems('_customers_');
|
const items: Record<string, any> = this.getItems('_customers_');
|
||||||
|
if (Object.keys(items).length > 0) {
|
||||||
|
setTimeout(() => callback(items), 0);
|
||||||
|
}
|
||||||
|
|
||||||
return this.messageBus.getProcessesDecoded((publicValues: any) =>
|
this.messageBus.getProcessesDecoded((_processId: string, values: any) => {
|
||||||
publicValues['uid'] &&
|
return values['utype']
|
||||||
publicValues['utype'] &&
|
&& values['utype'] === 'customer'
|
||||||
publicValues['utype'] === 'customer' &&
|
&& values['isDeleted']
|
||||||
publicValues['isDeleted'] &&
|
&& values['isDeleted'] === 'false';
|
||||||
publicValues['isDeleted'] === 'false' &&
|
}).then(async (processes: Record<string, any>) => {
|
||||||
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
|
if (Object.keys(processes).length === 0) {
|
||||||
).then((processes: any[]) => {
|
callback(items);
|
||||||
if (processes.length === 0) {
|
return;
|
||||||
return items;
|
}
|
||||||
} else {
|
|
||||||
for (const process of processes) {
|
const updatedItems: Record<string, any> = { ...items };
|
||||||
|
|
||||||
|
for (const [processId, process] of Object.entries(processes)) {
|
||||||
// Update cache
|
// Update cache
|
||||||
this.setItem('_customers_', process);
|
this.setItem('_customers_', processId, process);
|
||||||
|
|
||||||
items.push(process);
|
updatedItems[processId] = process;
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getCustomerByUid(uid: string): Promise<any> {
|
callback(updatedItems);
|
||||||
// Check if we have valid cache
|
|
||||||
const item: any = this.getItem('_customers_', 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'] === 'customer' &&
|
|
||||||
publicValues['isDeleted'] &&
|
|
||||||
publicValues['isDeleted'] === 'false'
|
|
||||||
).then((processes: any[]) => {
|
|
||||||
if (processes.length === 0) {
|
|
||||||
resolve(null);
|
|
||||||
} else {
|
|
||||||
const process: any = processes[0];
|
|
||||||
|
|
||||||
// Update cache
|
|
||||||
this.setItem('_customers_', process);
|
|
||||||
|
|
||||||
resolve(process);
|
|
||||||
}
|
|
||||||
}).catch(reject);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +102,7 @@ export default class CustomerService extends AbstractService {
|
|||||||
const customerUid: string = process.processData.uid;
|
const customerUid: string = process.processData.uid;
|
||||||
this.removeItem('_customers_', customerUid);
|
this.removeItem('_customers_', customerUid);
|
||||||
|
|
||||||
this.getCustomerByUid(customerUid).then(resolve).catch(reject);
|
resolve();
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user