UNPKG

node-red-contrib-agilite

Version:

Node-RED nodes to integrate with Agilit-e cloud or Agilit-e on-prem

380 lines (351 loc) 15.1 kB
const Agilite = require('agilite').default const TypeDetect = require('agilite-utils/dist/type-detect').default const EnumsTypeDetect = require('agilite-utils/dist/enums-type-detect').default const AgiliteUtils = require('agilite-utils').default module.exports = function (RED) { function BPM(config) { RED.nodes.createNode(this, config) const node = this const field = config.field || 'payload' const fieldType = config.fieldType || 'msg' node.status({ fill: 'blue', text: 'ready', shape: 'ring' }) this.on('input', async msg => { const serverConfig = RED.nodes.getNode(config.server) const history = config.excludeHistory const stepOptions = config.excludeStepOptions const visibleObjects = config.excludeVisibleObjects const comments = null const apiServerUrl = serverConfig.server const failFlow = config.failFlow const includeKeywords = config.includeKeywords const includeData = config.includeData let agilite = null let apiKey = null let logProfileKey = null let profileKey = config.profileKey let currentUser = config.currentUser let currentStep = config.currentStep let bpmRecordId = config.bpmRecordId let optionSelected = config.optionSelected let bpmRecordIds = config.bpmRecordIds let responsibleUsers = config.responsibleUsers let stepNames = config.stepNames let roleNames = config.roleNames let relevantUsers = config.relevantUsers let relevantRoles = config.relevantRoles let eventStamps = config.eventStamps let eventStartDate = config.eventStartDate let eventEndDate = config.eventEndDate let profileKeys = config.profileKeys let isoLanguage = config.isoLanguage let page = config.page let pageLimit = config.pageLimit let sort = config.sort let data = msg.payload let errorMessage = null let result = null msg.agilite = msg.agilite || {} const submitResponse = response => { switch (fieldType) { case 'msg': RED.util.setMessageProperty(msg, field, response.data) break case 'flow': node.context().flow.set(field, response.data) break case 'global': node.context().global.set(field, response.data) break } node.status({ fill: 'green', text: 'Success', shape: 'ring' }) msg.agilite.success = true msg.agilite.errorMessage = '' node.send(msg) } const submitError = error => { errorMessage = null if (error.response && error.response.data.errorMessage) { errorMessage = error.response.data.errorMessage } else if (error.message) { errorMessage = error.message } else { errorMessage = error } node.status({ fill: 'red', text: 'Error', shape: 'ring' }) msg.agilite.success = false msg.agilite.errorMessage = errorMessage if (failFlow) { node.error(errorMessage, msg) } else { node.send(msg) } } try { // Check if there's an Agilit-e object in MSG if (TypeDetect(msg.agilite) !== EnumsTypeDetect.OBJECT) msg.agilite = {} // Set Log Profile Key and API Key logProfileKey = msg.agilite.logProfileKey || '' apiKey = msg.agilite.apiKey || serverConfig.credentials.apiKey // Validate Payload if (TypeDetect(data) !== EnumsTypeDetect.OBJECT) data = {} // Handlebars if (profileKey) { profileKey = AgiliteUtils.compileTemplate(profileKey, msg) if (TypeDetect(profileKey) !== 'string' || profileKey === '[object Object]') errorMessage = 'Invalid Profile Key type provided. Expected a string' } if (currentUser) { currentUser = AgiliteUtils.compileTemplate(currentUser, msg) if (TypeDetect(currentUser) !== 'string' || currentUser === '[object Object]') errorMessage = 'Invalid Current User type provided. Expected a string' } if (currentStep) { currentStep = AgiliteUtils.compileTemplate(currentStep, msg) if (TypeDetect(currentStep) !== 'string' || currentStep === '[object Object]') errorMessage = 'Invalid Current Step type provided. Expected a string' } if (bpmRecordId) { bpmRecordId = AgiliteUtils.compileTemplate(bpmRecordId, msg) if (TypeDetect(bpmRecordId) !== 'string' || bpmRecordId === '[object Object]') errorMessage = 'Invalid BPM Record Id type provided. Expected a string' } if (optionSelected) { optionSelected = AgiliteUtils.compileTemplate(optionSelected, msg) if (TypeDetect(optionSelected) !== 'string' || optionSelected === '[object Object]') errorMessage = 'Invalid Option Selected type provided. Expected a string' } if (bpmRecordIds) { bpmRecordIds = AgiliteUtils.compileTemplate(bpmRecordIds, msg) if (TypeDetect(bpmRecordIds) !== 'string' || bpmRecordIds === '[object Object]') errorMessage = 'Invalid BPM Record Ids type provided. Expected a string' } if (responsibleUsers) { responsibleUsers = AgiliteUtils.compileTemplate(responsibleUsers, msg) if (TypeDetect(responsibleUsers) !== 'string' || responsibleUsers === '[object Object]') errorMessage = 'Invalid Responsible Users type provided. Expected a string' } if (stepNames) { stepNames = AgiliteUtils.compileTemplate(stepNames, msg) if (TypeDetect(stepNames) !== 'string' || stepNames === '[object Object]') errorMessage = 'Invalid Step Names type provided. Expected a string' } if (roleNames) { roleNames = AgiliteUtils.compileTemplate(roleNames, msg) if (TypeDetect(roleNames) !== 'string' || roleNames === '[object Object]') errorMessage = 'Invalid Role Names type provided. Expected a string' } if (relevantUsers) { relevantUsers = AgiliteUtils.compileTemplate(relevantUsers, msg) if (TypeDetect(relevantUsers) !== 'string' || relevantUsers === '[object Object]') errorMessage = 'Invalid Relevant Users type provided. Expected a string' } if (profileKeys) { profileKeys = AgiliteUtils.compileTemplate(profileKeys, msg) if (TypeDetect(profileKeys) !== 'string' || profileKeys === '[object Object]') errorMessage = 'Invalid Profile Keys type provided. Expected a string' } if (isoLanguage) { isoLanguage = AgiliteUtils.compileTemplate(isoLanguage, msg) if (TypeDetect(isoLanguage) !== 'string' || isoLanguage === '[object Object]') errorMessage = 'Invalid ISO Language type provided. Expected a string' } if (page) { page = AgiliteUtils.compileTemplate(page, msg) if (TypeDetect(page) !== 'string' || page === '[object Object]') errorMessage = 'Invalid Page type provided. Expected a string or a number' } if (pageLimit) { pageLimit = AgiliteUtils.compileTemplate(pageLimit, msg) if (TypeDetect(pageLimit) !== 'string' || pageLimit === '[object Object]') errorMessage = 'Invalid Page Limit type provided. Expected a string or a number' } if (relevantRoles) { relevantRoles = AgiliteUtils.compileTemplate(relevantRoles, msg) if (TypeDetect(relevantRoles) !== 'string' || relevantRoles === '[object Object]') errorMessage = 'Invalid Relevant Roles type provided. Expected a string' } if (eventStartDate) { eventStartDate = AgiliteUtils.compileTemplate(eventStartDate, msg) if (TypeDetect(eventStartDate) !== 'string' || eventStartDate === '[object Object]') errorMessage = 'Invalid Event Start Date type provided. Expected a string' } if (eventEndDate) { eventEndDate = AgiliteUtils.compileTemplate(eventEndDate, msg) if (TypeDetect(eventEndDate) !== 'string' || eventEndDate === '[object Object]') errorMessage = 'Invalid Event End Date type provided. Expected a string' } if (eventStamps) { eventStamps = AgiliteUtils.compileTemplate(eventStamps, msg) if (TypeDetect(eventStamps) !== 'string' || eventStamps === '[object Object]') errorMessage = 'Invalid Event Stamps type provided. Expected a string' } if (sort) { sort = AgiliteUtils.compileTemplate(sort, msg) if (TypeDetect(sort) !== 'string' || sort === '[object Object]') errorMessage = 'Invalid Sort type provided. Expected a string' } // We need a apiKey, key and data to proceed if (!apiKey) { errorMessage = 'No valid API Key Provided. Please authenticate with Agilit-e first' } else if (!apiServerUrl) { errorMessage = 'No Server URL Provided' } else { switch (config.actionType) { case '1': // Register BPM Record if (!profileKey) errorMessage = 'No Profile Key found' if (!currentUser) errorMessage = 'No Current User found' break case '2': // Execute if (!profileKey) errorMessage = 'No Profile Key found' if (!bpmRecordId) errorMessage = 'No BPM Record Id found' if (!optionSelected) errorMessage = 'No Option Selected found' if (!currentUser) errorMessage = 'No Current User found' if (!currentStep) errorMessage = 'No Current Step found' break case '3': // Get Record State if (!profileKeys) errorMessage = 'No Profile Keys found' break case '4': // Get By Profile Key if (!profileKey) errorMessage = 'No Profile Key found' break case '5': // Get Active Steps case '6': // Get Active Users if (!profileKey) errorMessage = 'No Profile Key found' break case '7': // Assign Role if (!profileKey) { errorMessage = 'No Profile Key found' } else if (!bpmRecordId) { errorMessage = 'No BPM Record Id found' } else if (!roleNames) { errorMessage = 'No Role Name(s) found' } else if (!currentUser) { errorMessage = 'No Current User found' } else if (!responsibleUsers) { errorMessage = 'No Responsible User(s) found' } break case '8': // Get Assigned Roles if (!profileKey) { errorMessage = 'No Profile Key found' } else if (!bpmRecordId) { errorMessage = 'No BPM Record Id found' } else if (!roleNames) { errorMessage = 'No Role Name(s) found' } break } } if (errorMessage) return submitError(errorMessage) agilite = new Agilite({ apiServerUrl, apiKey }) // Finalize array properties profileKeys ? (profileKeys = profileKeys.split(',')) : (profileKeys = []) bpmRecordIds ? (bpmRecordIds = bpmRecordIds.split(',')) : (bpmRecordIds = []) stepNames ? (stepNames = stepNames.split(',')) : (stepNames = []) roleNames ? (roleNames = roleNames.split(',')) : (roleNames = []) responsibleUsers ? (responsibleUsers = responsibleUsers.split(',')) : (responsibleUsers = []) relevantUsers ? (relevantUsers = relevantUsers.split(',')) : (relevantUsers = []) relevantRoles ? (relevantRoles = relevantRoles.split(',')) : (relevantRoles = []) eventStamps ? (eventStamps = eventStamps.split(',')) : (eventStamps = []) node.status({ fill: 'yellow', text: 'Running', shape: 'ring' }) switch (config.actionType) { case '1': // Register BPM Record result = await agilite.BPM.registerBPMRecord( profileKey, currentUser, history, stepOptions, visibleObjects, includeKeywords, includeData, isoLanguage, logProfileKey ) break case '2': // Execute result = await agilite.BPM.execute( profileKey, bpmRecordId, optionSelected, currentUser, currentStep, comments, data, history, stepOptions, visibleObjects, includeKeywords, includeData, isoLanguage, logProfileKey ) break case '3': // Get Record State result = await agilite.BPM.getRecordState( profileKeys, bpmRecordIds, stepNames, responsibleUsers, relevantUsers, relevantRoles, eventStamps, eventStartDate, eventEndDate, history, stepOptions, visibleObjects, includeKeywords, includeData, page, pageLimit, sort, isoLanguage, logProfileKey ) break case '4': // Get By Profile Key result = await agilite.BPM.getByProfileKey(profileKey, logProfileKey) break case '5': // Get Active Steps result = await agilite.BPM.getActiveSteps(profileKey, isoLanguage, logProfileKey) break case '6': // Get Active Users result = await agilite.BPM.getActiveUsers(profileKey, logProfileKey) break case '7': // Assign Role result = await agilite.BPM.assignRole(profileKey, bpmRecordId, roleNames[0], currentUser, responsibleUsers, logProfileKey) break case '8': // Get Assigned Roles result = await agilite.BPM.getAssignedRoles(profileKey, bpmRecordId, roleNames, logProfileKey) break case '9': // Lock Record result = await agilite.BPM.lockRecord(bpmRecordId, logProfileKey) break case '10': // Unlock Record result = await agilite.BPM.unlockRecord(bpmRecordId, logProfileKey) break default: throw new Error('No valid Action Type specified') } submitResponse(result) } catch (e) { submitError(e) } }) } RED.nodes.registerType('bpm', BPM) }