UNPKG

tuain-bpm-lib

Version:

Servicio de gestión de manejo de procesos de la plataforma Tuain

221 lines (188 loc) 5.06 kB
const { ObjectId, ISODate } = require('mongodb'); const VARIABLES = 'variables'; const USER_ROLES = 'users'; const GROUP_ROLES = 'groupRoles'; const SOURCES_STATUS = 'sources.status'; const DESTINATONS_STATUS = 'destinations.status'; /** * Funciones generales para realizar consultas sobre colecciones */ function findByParams(paramsObject) { if (!paramsObject) { return null; } let id = null; let paramsQuery = {}; if (typeof paramsObject === 'string') { // No es un objeto con parámetros sino una cadena con un id id = paramsObject; } else { id = paramsObject.id ?? null; delete paramsObject.id; const params = Object.keys(paramsObject) .reduce((acc, cur) => { if (paramsObject[cur]) { return Object.assign(acc, { [cur]: paramsObject[cur] }); } return acc; }, {}); paramsQuery = { ...params }; } if (id) { paramsQuery._id = ObjectId(id); } return paramsQuery; } function updateInstance(itemInfo, updateData) { return [ findByParams(itemInfo), { $set: updateData }, ]; } function updateProcSubobject(itemInfo, preffix, name, value) { return [ findByParams(itemInfo), { $set: { [`${preffix}.${name}`]: value } }, ]; } /** * Consultas para procesos */ function findProcess(procInfo) { return findByParams(procInfo); } function findProcessByUser(userType, userId) { return { [`users.${userType}`]: userId }; } function updateProc(procInfo, updateData) { return updateInstance(procInfo, updateData); } function updateProcVar(procInfo, varName, newValue) { return updateProcSubobject(procInfo, VARIABLES, varName, newValue); } function updateProcUser(procInfo, roleName, userId) { return updateProcSubobject(procInfo, USER_ROLES, roleName, userId); } function updateProcGroup(procInfo, roleName, groupId) { return updateProcSubobject(procInfo, GROUP_ROLES, roleName, groupId); } /** * Tareas */ function findTask(taskInfo) { return findByParams(taskInfo); } function findGroupsTasks(groupIdArray) { return { groupAssigned: { $in: groupIdArray } }; } function updateTask(taskInfo, updateData) { return updateInstance(taskInfo, updateData); } function updateTaskVar(taskInfo, varName, newValue) { return updateProcSubobject(taskInfo, VARIABLES, varName, newValue); } /** * Eventos */ function findEvent(eventInfo) { return findByParams(eventInfo); } function updateEvent(eventInfo, updateData) { return updateInstance(eventInfo, updateData); } function updateEventVar(eventInfo, varName, newValue) { return updateProcSubobject(eventInfo, VARIABLES, varName, newValue); } function findTaskInstance(taskId, procInstance, taskName, status) { let query = {}; if (procInstance && taskName) { query = { procId: procInstance, name: taskName }; (status) && Object.assign(query, { status }); return query; } query = { _id: ObjectId(taskId) }; (status) && Object.assign(query, { status }); return query; } /** * Gateways */ function findGateway(paramsObject) { return findByParams(paramsObject); } function updateGateway(gatewayInfo, updateData) { return updateInstance(gatewayInfo, updateData); } function updateGatewaySource(gatewayInfo, sourceName, newValue) { return updateProcSubobject(gatewayInfo, SOURCES_STATUS, sourceName, newValue); } function updateGatewayDestination(gatewayInfo, destName, newValue) { return updateProcSubobject(gatewayInfo, DESTINATONS_STATUS, destName, newValue); } function updateGatewayDestinationNode(gatewayId, gatewayDestinations) { const updateData = { destinations: {} }; const destNames = Object.keys(gatewayDestinations); for (let index = 0; index < destNames.length; index++) { const name = destNames[index]; const { nodeId } = gatewayDestinations[name]; updateData.destinations[`${name}.nextNode.id`] = nodeId; } return [ findByParams(gatewayId), { $set: updateData }, ]; } function updateGatewaySourceStatus(gatewayId, origin, status, gatewayData) { const updateData = { ...gatewayData }; updateData[`sources.${origin.name}.status`] = status; Object.entries(origin).forEach(([key, value]) => { updateData[`sources.${origin.name}.${key}`] = value; }); return [ findByParams(gatewayId), { $set: updateData }, ]; } function findTimers(status, beforeDate) { return { status, creation: { $lt: ISODate(beforeDate), }, }; } function changeTimerStatus(timerId, status) { return [ findByParams(timerId), { $set: { status } }, ]; } module.exports = { // General findProcess, findProcessByUser, updateProc, // Vars & Roles updateProcVar, updateProcUser, updateProcGroup, // Tasks findTask, findTaskInstance, findGroupsTasks, updateTask, updateTaskVar, // Events findEvent, updateEvent, updateEventVar, findTimers, changeTimerStatus, // Gateways findGateway, updateGateway, updateGatewaySource, updateGatewayDestination, updateGatewaySourceStatus, updateGatewayDestinationNode, };