96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
import MessageBus from 'src/sdk/MessageBus';
|
|
import User from 'src/sdk/User';
|
|
|
|
export default class CustomerService {
|
|
|
|
private static readonly messageBus: MessageBus = MessageBus.getInstance();
|
|
|
|
private constructor() { }
|
|
|
|
public static createCustomer(customerData: any, validatorId: string): Promise<any> {
|
|
const ownerId = User.getInstance().getPairingId()!;
|
|
|
|
const processData: any = {
|
|
uid: uuidv4(),
|
|
utype: 'customer',
|
|
isDeleted: 'false',
|
|
created_at: new Date().toISOString(),
|
|
updated_at: new Date().toISOString(),
|
|
...customerData,
|
|
};
|
|
|
|
const privateFields: string[] = Object.keys(processData);
|
|
privateFields.splice(privateFields.indexOf('uid'), 1);
|
|
privateFields.splice(privateFields.indexOf('utype'), 1);
|
|
|
|
const roles: any = {
|
|
demiurge: {
|
|
members: [...[ownerId], validatorId],
|
|
validation_rules: [],
|
|
storages: []
|
|
},
|
|
owner: {
|
|
members: [ownerId],
|
|
validation_rules: [
|
|
{
|
|
quorum: 0.5,
|
|
fields: [...privateFields, 'roles', 'uid', 'utype'],
|
|
min_sig_member: 1,
|
|
},
|
|
],
|
|
storages: []
|
|
},
|
|
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: {
|
|
members: [ownerId],
|
|
validation_rules: [],
|
|
storages: []
|
|
}
|
|
};
|
|
|
|
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) => {
|
|
resolve(processCreated);
|
|
}).catch(reject);
|
|
}).catch(reject);
|
|
}).catch(reject);
|
|
});
|
|
}
|
|
|
|
public static getCustomers(): Promise<any[]> {
|
|
return this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['utype'] && publicValues['utype'] === 'customer');
|
|
}
|
|
|
|
public static getCustomerByUid(uid: string): Promise<any> {
|
|
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').then((profiles: any[]) => {
|
|
if (profiles.length === 0) {
|
|
resolve(null);
|
|
} else {
|
|
resolve(profiles[0]);
|
|
}
|
|
}).catch(reject);
|
|
});
|
|
}
|
|
}
|