tuain-nano-bpm
Version:
Package to provide basic functionalities for Process Management on services for Tuain platform
210 lines (191 loc) • 8.16 kB
JavaScript
const ProcessControllerBase = require('./controller-base');
const {
methods: { saveNewInstance, newInstance, updateBasicInfoInstance, goToNode },
errors: generalErrors,
} = require('./general');
const {
methods: { setVar, setVars, getVar, getVars, assignUser, getUser, assignGroup, getGroup },
errors: varsRolesErrors,
} = require('./vars-and-roles');
const {
methods: {
getTaskInstance, createTaskInstance, updateTaskNextNode, updateTaskInstance, startNewTask,
assignTaskToUser, assignTaskToGroup, stopTask, completeTask, getProcessActiveTasks, getUserProcess,
getActiveTasks, getUserTasks, getGroupsTasks, getProcessTaskHistory, getTasksInProgress,
}, errors: taskErrors,
} = require('./task');
const { methods: { createEventInstance, triggerEvent, getProcessEventsHistory }, errors: eventErrors } = require('./proc-event');
const {
methods: {
refreshProcessTimers, updateTimers, saveTimerEvent, registerTimerEvent, triggerTimerEvent,
addScheduledTimer,
}, errors: eventTimersErrors,
} = require('./timers');
const {
methods: {
startNewGateway, getGatewayInstance, createStandardGateway, updateGatewayInstance, updateGatewayNextNode,
createParallelGateway, updateParallelGateway, getProcessGatewaysHistory, resolveGatewayDestination,
defineGatewayDestinationCallback, getGatewayValudationFunction,
}, errors: gatewayErrors,
} = require('./gateway');
const {
process: {
emmiterTypes: {
taskStart: TASKSTART, taskComplete: TASKCOMPLETE, taskClose: TASKCLOSE,
eventClose: EVENTCLOSE, eventComplete: EVENTCOMPLETE,
},
},
} = require('../../config');
/**
* Update mediante Observables:
*
* const { Observable } = require('rxjs');
class DataSource {
constructor() {
this.subject = new Observable(observer => {
this.observer = observer;
});
}
setData(data) {
this.observer.next(data);
}
}
class DataConsumer {
constructor(dataSource) {
this.dataSource = dataSource;
}
subscribeToData() {
this.dataSource.subject.subscribe(data => {
console.log(`Received data: ${data}`);
});
}
}
const dataSource = new DataSource();
const dataConsumer = new DataConsumer(dataSource);
dataConsumer.subscribeToData();
dataSource.setData('hello');
// Output: Received data: hello
*/
const modErrs = {
newProcessInstance: {
notStartEvent: ['01', 'No se encontró definición del evento de inicialización start'],
missingRequiredVariable: ['02', 'Variable requerida para iniciar el proceso no se encuentra'],
},
};
class ProcessController extends ProcessControllerBase {
constructor(ecosystem, procName, readOnlyDbPool, readWriteDbPool, logger, errMgr, definition = null) {
super();
this._ecosystem = ecosystem;
this.name = procName;
this.roPool = readOnlyDbPool;
this.rwPool = readWriteDbPool;
this.logger = logger;
this.errMgr = errMgr;
this.callbackSets = {
[TASKSTART]: {},
[TASKCOMPLETE]: {},
[TASKCLOSE]: {},
[EVENTCLOSE]: {},
[EVENTCOMPLETE]: {},
};
this.gatewayDestinationValidations = {};
this.refreshTimer = null;
this.currentTimers = [];
this.externalTaskSubscriptions = [];
this.scheduledTimers = [];
this.errMgr.addModuleSet('ProcessController', modErrs);
this.errMgr.addModuleSet('ProcessController-general', generalErrors);
this.errMgr.addModuleSet('ProcessController-basic', varsRolesErrors);
this.errMgr.addModuleSet('ProcessController-task', taskErrors);
this.errMgr.addModuleSet('ProcessController-event', eventErrors);
this.errMgr.addModuleSet('ProcessController-eventTimers', eventTimersErrors);
this.errMgr.addModuleSet('ProcessController-gateway', gatewayErrors);
// General
this.saveNewInstance = saveNewInstance.bind(this);
this.newInstance = newInstance.bind(this);
this.updateBasicInfoInstance = updateBasicInfoInstance.bind(this);
this.goToNode = goToNode.bind(this);
// Vars and roles
this.setVar = setVar.bind(this);
this.setVars = setVars.bind(this);
this.getVar = getVar.bind(this);
this.getVars = getVars.bind(this);
this.assignUser = assignUser.bind(this);
this.getUser = getUser.bind(this);
this.assignGroup = assignGroup.bind(this);
this.getGroup = getGroup.bind(this);
// Tasks
this.getTaskInstance = getTaskInstance.bind(this);
this.createTaskInstance = createTaskInstance.bind(this);
this.updateTaskNextNode = updateTaskNextNode.bind(this);
this.updateTaskInstance = updateTaskInstance.bind(this);
this.startNewTask = startNewTask.bind(this);
this.assignTaskToUser = assignTaskToUser.bind(this);
this.assignTaskToGroup = assignTaskToGroup.bind(this);
this.stopTask = stopTask.bind(this);
this.completeTask = completeTask.bind(this);
this.getProcessActiveTasks = getProcessActiveTasks.bind(this);
this.getUserProcess = getUserProcess.bind(this);
this.getActiveTasks = getActiveTasks.bind(this);
this.getUserTasks = getUserTasks.bind(this);
this.getGroupsTasks = getGroupsTasks.bind(this);
this.getProcessTaskHistory = getProcessTaskHistory.bind(this);
this.getTasksInProgress = getTasksInProgress.bind(this);
// Events
this.createEventInstance = createEventInstance.bind(this);
this.triggerEvent = triggerEvent.bind(this);
this.getProcessEventsHistory = getProcessEventsHistory.bind(this);
// Timers
this.refreshProcessTimers = refreshProcessTimers.bind(this);
this.updateTimers = updateTimers.bind(this);
this.saveTimerEvent = saveTimerEvent.bind(this);
this.registerTimerEvent = registerTimerEvent.bind(this);
this.triggerTimerEvent = triggerTimerEvent.bind(this);
this.addScheduledTimer = addScheduledTimer.bind(this);
// Gateway
this.startNewGateway = startNewGateway.bind(this);
this.getGatewayInstance = getGatewayInstance.bind(this);
this.createStandardGateway = createStandardGateway.bind(this);
this.updateGatewayInstance = updateGatewayInstance.bind(this);
this.updateGatewayNextNode = updateGatewayNextNode.bind(this);
this.createParallelGateway = createParallelGateway.bind(this);
this.updateParallelGateway = updateParallelGateway.bind(this);
this.getProcessGatewaysHistory = getProcessGatewaysHistory.bind(this);
this.resolveGatewayDestination = resolveGatewayDestination.bind(this);
// ...
this.defineGatewayDestinationCallback = defineGatewayDestinationCallback.bind(this);
this.getGatewayValudationFunction = getGatewayValudationFunction.bind(this);
// Manejadores de eventos
this.onTaskStart = (name, callback) => this.eventSubscribe(TASKSTART, name, callback);
this.onTaskComplete = (name, callback) => this.eventSubscribe(TASKCOMPLETE, name, callback);
this.onTaskClose = (name, callback) => this.eventSubscribe(TASKCLOSE, name, callback);
this.onEventClose = (name, callback) => this.eventSubscribe(EVENTCLOSE, name, callback);
this.onEventComplete = (name, callback) => this.eventSubscribe(EVENTCOMPLETE, name, callback);
if (definition) {
this.initialize(definition);
}
}
async initialize(inputProcDef) {
this.processDefinition = inputProcDef;
this.customizeBehaviour();
}
eventSubscribe(type, name, callback) {
const message = `Se registra subscripción evento ${type} para ${name}`;
this.logger.log({ level: 'silly', label: 'eventEmmiters', action: type, message });
this.callbackSets[type][name] = callback;
}
async execCustomCallback(type, name, event) {
const callbackFunction = this.callbackSets?.[type]?.[name] ?? null;
return (callbackFunction) ? callbackFunction(event) : null;
}
async getProcessHistory(procId) {
const [taskErr, tasks] = await this.getProcessTaskHistory(procId);
const [eventErr, events] = await this.getProcessEventsHistory(procId);
const [gatewayErr, gateways] = await this.getProcessGatewaysHistory(procId);
if (taskErr || eventErr || gatewayErr) {
return [taskErr ?? eventErr ?? gatewayErr, null];
}
return [null, { tasks, events, gateways }];
}
}
module.exports = ProcessController;