lecoffre-front/src/common/Api/LeCoffreApi/sdk/OfficeRoleService.ts

176 lines
5.9 KiB
TypeScript

import { v4 as uuidv4 } from 'uuid';
import User from 'src/sdk/User';
import AbstractService from './AbstractService';
import OfficeService from './OfficeService';
import RuleService from './RuleService';
import { DEFAULT_STORAGE_URLS } from '@Front/Config/AppConstants';
export default class OfficeRoleService extends AbstractService {
private constructor() {
super();
}
public static createOfficeRole(roleData: any, validatorId: string): Promise<any> {
const ownerId: string = User.getInstance().getPairingId()!;
const processData: any = {
uid: uuidv4(),
utype: 'officeRole',
isDeleted: 'false',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
...roleData
};
const privateFields: string[] = Object.keys(processData);
privateFields.splice(privateFields.indexOf('uid'), 1);
privateFields.splice(privateFields.indexOf('utype'), 1);
privateFields.splice(privateFields.indexOf('isDeleted'), 1);
const roles: any = {
demiurge: {
members: [ownerId],
validation_rules: [],
storages: []
},
owner: {
members: [ownerId],
validation_rules: [
{
quorum: 0.01,
fields: [...privateFields, 'roles', 'uid', 'utype', 'isDeleted'],
min_sig_member: 0.01,
},
],
storages: [...DEFAULT_STORAGE_URLS]
},
validator: {
members: [validatorId],
validation_rules: [
{
quorum: 1,
fields: [...privateFields, 'roles', 'uid', 'utype', 'isDeleted'],
min_sig_member: 1,
},
],
storages: [...DEFAULT_STORAGE_URLS]
},
apophis: {
members: [ownerId, validatorId],
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) => {
this.getOfficeRoleByUid(processCreated.processData.uid).then(resolve).catch(reject);
}).catch(reject);
}).catch(reject);
}).catch(reject);
});
}
public static getOfficeRoles(): Promise<any[]> {
// Check if we have valid cache
const items: any[] = this.getItems('_office_roles_');
return this.messageBus.getProcessesDecoded((publicValues: any) =>
publicValues['uid'] &&
publicValues['utype'] &&
publicValues['utype'] === 'officeRole' &&
publicValues['isDeleted'] &&
publicValues['isDeleted'] === 'false' &&
!items.map((item: any) => item.processData.uid).includes(publicValues['uid'])
).then(async (processes: any[]) => {
if (processes.length === 0) {
return items;
} else {
for (let process of processes) {
process = await this.completeOfficeRole(process);
// Update cache
this.setItem('_office_roles_', process);
items.push(process);
}
return items;
}
});
}
public static getOfficeRoleByUid(uid: string): Promise<any> {
// Check if we have valid cache
const item: any = this.getItem('_office_roles_', 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'] === 'officeRole' &&
publicValues['isDeleted'] &&
publicValues['isDeleted'] === 'false'
).then(async (processes: any[]) => {
if (processes.length === 0) {
resolve(null);
} else {
let process: any = processes[0];
process = await this.completeOfficeRole(process);
// Update cache
this.setItem('_office_roles_', process);
resolve(process);
}
}).catch(reject);
});
}
public static updateOfficeRole(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) => {
const roleUid: string = process.processData.uid;
this.removeItem('_office_roles_', roleUid);
this.getOfficeRoleByUid(roleUid).then(resolve).catch(reject);
}).catch(reject);
}).catch(reject);
}).catch(reject);
});
}
private static async completeOfficeRole(process: any): Promise<any> {
if (process.processData.office) {
process.processData.office = await new Promise<any>(async (resolve: (office: any) => void) => {
const office: any = (await OfficeService.getOfficeByUid(process.processData.office.uid)).processData;
resolve(office);
});
}
if (process.processData.rules && process.processData.rules.length > 0) {
process.processData.rules = await new Promise<any[]>(async (resolve: (rules: any[]) => void) => {
const rules: any[] = [];
for (const rule of process.processData.rules) {
rules.push((await RuleService.getRuleByUid(rule.uid)).processData);
}
resolve(rules);
});
}
return process;
}
}