node-red-contrib-agilite
Version:
Node-RED nodes to integrate with Agilit-e cloud or Agilit-e on-prem
256 lines (228 loc) • 9.64 kB
JavaScript
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 Keywords(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 outputFormat = config.outputFormat
const outputFormat2 = config.outputFormat2
const sortBy = config.sortBy
const sortBy2 = config.sortBy2
const apiServerUrl = serverConfig.server
const failFlow = config.failFlow
let agilite = null
let apiKey = null
let logProfileKey = null
let profileKey = config.profileKey
let recordId = config.recordId
let groupName = config.groupName
let labelKey = config.labelKey
let valueKey = config.valueKey
let profileKeys = config.profileKeys
let recordIds = config.recordIds
let result = null
let data = msg.payload
let errorMessage = 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 && TypeDetect(data) !== EnumsTypeDetect.ARRAY) data = {}
// Handlebars
if (recordId) {
recordId = AgiliteUtils.compileTemplate(recordId, msg)
if (TypeDetect(recordId) !== 'string' || recordId === '[object Object]') errorMessage = 'Invalid Record Id type provided. Expected a string'
}
if (profileKey) {
profileKey = AgiliteUtils.compileTemplate(profileKey, msg)
if (TypeDetect(profileKey) !== 'string' || profileKey === '[object Object]')
errorMessage = 'Invalid Profile Key type provided. Expected a string'
}
if (groupName) {
groupName = AgiliteUtils.compileTemplate(groupName, msg)
if (TypeDetect(groupName) !== 'string' || groupName === '[object Object]')
errorMessage = 'Invalid Group Name type provided. Expected a string'
}
if (labelKey) {
labelKey = AgiliteUtils.compileTemplate(labelKey, msg)
if (TypeDetect(labelKey) !== 'string' || labelKey === '[object Object]') errorMessage = 'Invalid Label Key type provided. Expected a string'
}
if (valueKey) {
valueKey = AgiliteUtils.compileTemplate(valueKey, msg)
if (TypeDetect(valueKey) !== 'string' || valueKey === '[object Object]') errorMessage = 'Invalid Value Key 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 (recordIds) {
recordIds = AgiliteUtils.compileTemplate(recordIds, msg)
if (TypeDetect(recordIds) !== 'string' || recordIds === '[object Object]')
errorMessage = 'Invalid Record Ids 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': // Get Keywords By Profile Key
if (!profileKey) errorMessage = 'Please provide a Profile Key'
break
case '2': // Get Profile Keys By Group
if (!groupName) errorMessage = 'Please provide a Group Name'
break
case '3': // Get Keyword Value by Label
if (!profileKey) {
errorMessage = 'Please provide a Profile Key'
} else if (!labelKey) {
errorMessage = 'Please provide a Label Key'
}
break
case '4': // Get Keyword Label by Value
if (!profileKey) {
errorMessage = 'Please provide a Profile Key'
} else if (!valueKey) {
errorMessage = 'Please provide a Value Key'
}
break
case '5': // Create Keyword
if (!data.data) errorMessage = 'No valid data object found in msg.payload'
if (data.data) {
if (!data.data.isActive) errorMessage = 'Please provide an "isActive" property'
if (!data.data.key) errorMessage = 'Please provide a "key" property'
}
break
case '6': // Update Keyword Record
case '7': // Delete Keyword Record
if (!recordId) errorMessage = 'Please provide a Record Id'
break
case '8': // Set Values By Profille Key
if (!profileKey) errorMessage = 'Please provide a Profile Key'
break
case '9': // Set Value By Label
case '10': // Set Value By Label
if (!profileKey) {
errorMessage = 'Please provide a Record Id'
} else if (!valueKey) {
errorMessage = 'Please provide a Value Key'
} else if (!labelKey) {
errorMessage = 'Please provide a Label Key'
}
break
case '11': // Get Data
break
}
}
if (errorMessage) return submitError(errorMessage)
agilite = new Agilite({ apiServerUrl, apiKey })
node.status({
fill: 'yellow',
text: 'Running',
shape: 'ring'
})
switch (config.actionType) {
case '1': // Get Values By Profile Key
result = await agilite.Keywords.getValuesByProfileKey(profileKey, sortBy, outputFormat, logProfileKey)
break
case '2': // Get Profile Keys By Group
result = await agilite.Keywords.getProfileKeysByGroup(groupName, sortBy2, logProfileKey)
break
case '3': // Get Keyword Value by Label
result = await agilite.Keywords.getValueByLabel(profileKey, labelKey, outputFormat2, logProfileKey)
break
case '4': // Get Keyword Label by Value
result = await agilite.Keywords.getLabelByValue(profileKey, valueKey, outputFormat2, logProfileKey)
break
case '5': // Create Keyword Record
result = await agilite.Keywords.postData(data, logProfileKey)
break
case '6': // Update Keyword Record
result = await agilite.Keywords.putData(recordId, data, logProfileKey)
break
case '7': // Delete Keyword Record
result = await agilite.Keywords.deleteData(recordId, logProfileKey)
break
case '8': // Set Values By Profile Key
result = await agilite.Keywords.setValuesByProfileKey(profileKey, data, logProfileKey)
break
case '9': // Set Value By Label
result = await agilite.Keywords.setValueByLabel(profileKey, labelKey, valueKey, logProfileKey)
break
case '10': // Set Label By Value
result = await agilite.Keywords.setLabelByValue(profileKey, valueKey, labelKey, logProfileKey)
break
case '11': // Get Data
result = await agilite.Keywords.getData(profileKeys.split(','), recordIds.split(','), false, logProfileKey)
break
default:
throw new Error('No valid Action Type specified')
}
submitResponse(result)
} catch (e) {
submitError(e)
}
})
}
RED.nodes.registerType('keywords', Keywords)
}