UNPKG

node-red-contrib-agilite

Version:

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

222 lines (194 loc) 7.73 kB
const Agilite = require('agilite').default const AgiliteUtils = require('agilite-utils').default const TypeDetect = require('agilite-utils/dist/type-detect').default const EnumsTypeDetect = require('agilite-utils/dist/enums-type-detect').default module.exports = function (RED) { function BatchLogging(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 apiServerUrl = serverConfig.server const failFlow = config.failFlow let data = msg.payload let agilite = null let apiKey = null let profileKey = config.profileKey let logProfileKey = config.logProfileKey let category = config.category let subCategory = config.subCategory let code = config.code let message = config.message let qry = config.qry let fieldsToReturn = config.fieldsToReturn let qryOptions = config.qryOptions let page = config.page let pageLimit = config.pageLimit let errorMessage = null let result = null let contentType = 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 if (!logProfileKey) logProfileKey = msg.agilite.logProfileKey || '' apiKey = msg.agilite.apiKey || serverConfig.credentials.apiKey // Validate Payload if (config.actionType !== '2') { if (TypeDetect(data) !== EnumsTypeDetect.OBJECT) data = {} } else { // Create Log Entry data validation switch (TypeDetect(data)) { case EnumsTypeDetect.STRING: contentType = 'text/plain' break case EnumsTypeDetect.OBJECT: contentType = 'application/json' break default: contentType = 'application/json' data = { data } break } } // 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 (logProfileKey) { logProfileKey = AgiliteUtils.compileTemplate(logProfileKey, msg) if (TypeDetect(logProfileKey) !== 'string' || logProfileKey === '[object Object]') errorMessage = 'Invalid Log Profile Key type provided. Expected a string' } if (category) { category = AgiliteUtils.compileTemplate(category, msg) if (TypeDetect(category) !== 'string' || category === '[object Object]') errorMessage = 'Invalid Category type provided. Expected a string' } if (subCategory) { subCategory = AgiliteUtils.compileTemplate(subCategory, msg) if (TypeDetect(subCategory) !== 'string' || subCategory === '[object Object]') errorMessage = 'Invalid Sub Category type provided. Expected a string' } if (code) { code = AgiliteUtils.compileTemplate(code, msg) if (TypeDetect(code) !== 'string' || code === '[object Object]') errorMessage = 'Invalid Code type provided. Expected a string' } if (message) { message = AgiliteUtils.compileTemplate(message, msg) if (TypeDetect(message) !== 'string' || message === '[object Object]') errorMessage = 'Invalid Message type provided. Expected a string' } if (fieldsToReturn) { fieldsToReturn = AgiliteUtils.compileTemplate(fieldsToReturn, msg) if (TypeDetect(fieldsToReturn) !== 'string' || fieldsToReturn === '[object Object]') errorMessage = 'Invalid Fields To Return 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' } qry = AgiliteUtils.compileTemplate(qry, msg) qryOptions = AgiliteUtils.compileTemplate(qryOptions, msg) // We need a token, keys 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': // Get By Profile Key case '2': // Create Log Entry case '3': // Get Logs case '4': // Reset Logs if (!profileKey) errorMessage = 'No Profile Key found' break } } if (errorMessage) return submitError(errorMessage) agilite = new Agilite({ apiServerUrl, apiKey }) node.status({ fill: 'yellow', text: 'Running', shape: 'ring' }) switch (config.actionType) { case '1': result = await agilite.BatchLogging.getByProfileKey(profileKey, logProfileKey) break case '2': result = await agilite.BatchLogging.createLogEntry(profileKey, category, subCategory, code, message, contentType, data) break case '3': result = await agilite.BatchLogging.getLogs(profileKey, qry, fieldsToReturn, qryOptions, page, pageLimit) break case '4': result = await agilite.BatchLogging.resetLogs(profileKey, logProfileKey) break default: throw new Error('No valid Action Type specified') } submitResponse(result) } catch (e) { submitError(e) } }) } RED.nodes.registerType('batch-logging', BatchLogging) }