UNPKG

node-red-contrib-agilite

Version:

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

168 lines (140 loc) 5.08 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 TierStructures(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 includeValues = config.includeValues const includeMetaData = config.includeMetaData const includeTierEntries = config.includeTierEntries const apiServerUrl = serverConfig.server const failFlow = config.failFlow let agilite = null let apiKey = null let logProfileKey = null let tierKeys = config.tierKeys let sortValues = config.sortValues let valuesOutputFormat = config.valuesOutputFormat 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 (tierKeys) { tierKeys = AgiliteUtils.compileTemplate(tierKeys, msg) if (TypeDetect(tierKeys) !== 'string' || tierKeys === '[object Object]') errorMessage = 'Invalid Tier Keys type provided. Expected a string' } if (sortValues) { sortValues = AgiliteUtils.compileTemplate(sortValues, msg) if (TypeDetect(sortValues) !== 'string' || sortValues === '[object Object]') errorMessage = 'Invalid Sort Values type provided. Expected a string' } if (valuesOutputFormat) { valuesOutputFormat = AgiliteUtils.compileTemplate(valuesOutputFormat, msg) if (TypeDetect(valuesOutputFormat) !== 'string' || valuesOutputFormat === '[object Object]') errorMessage = 'Invalid Values Output Format type provided. Expected a string' } // Finalize array properties tierKeys = tierKeys.split(',') // 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': // getTierByKey if (!tierKeys) errorMessage = 'No Tier Keys Provided' break } } if (errorMessage) return submitError(errorMessage) agilite = new Agilite({ apiServerUrl, apiKey }) node.status({ fill: 'yellow', text: 'Running', shape: 'ring' }) switch (config.actionType) { case '1': // getTierByKey result = await agilite.TierStructures.getTierByKey( tierKeys, includeValues, includeMetaData, includeTierEntries, sortValues, valuesOutputFormat, logProfileKey ) break } submitResponse(result) } catch (e) { submitError(e) } }) } RED.nodes.registerType('tier-structures', TierStructures) }