2020-09-11 19:47:22 +02:00
|
|
|
const mustache = require("mustache")
|
|
|
|
const actions = require("./actions")
|
|
|
|
const logic = require("./logic")
|
|
|
|
|
2020-09-17 17:16:05 +02:00
|
|
|
const FILTER_STEP_ID = logic.BUILTIN_DEFINITIONS.FILTER.stepId
|
|
|
|
|
2020-09-17 14:36:19 +02:00
|
|
|
function cleanMustache(string) {
|
|
|
|
let charToReplace = {
|
|
|
|
"[": ".",
|
|
|
|
"]": "",
|
|
|
|
}
|
|
|
|
let regex = new RegExp(/{{[^}}]*}}/g)
|
2020-09-22 11:41:56 +02:00
|
|
|
let matches = string.match(regex)
|
|
|
|
if (matches == null) {
|
|
|
|
return string
|
|
|
|
}
|
|
|
|
for (let match of matches) {
|
2020-09-17 14:36:19 +02:00
|
|
|
let baseIdx = string.indexOf(match)
|
|
|
|
for (let key of Object.keys(charToReplace)) {
|
2020-09-22 11:33:25 +02:00
|
|
|
let idxChar = match.indexOf(key)
|
2020-09-17 14:36:19 +02:00
|
|
|
if (idxChar !== -1) {
|
|
|
|
string =
|
|
|
|
string.slice(baseIdx, baseIdx + idxChar) +
|
|
|
|
charToReplace[key] +
|
|
|
|
string.slice(baseIdx + idxChar + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string
|
|
|
|
}
|
|
|
|
|
2020-09-23 12:54:15 +02:00
|
|
|
// looks for inputs that need cleanup to the correct type
|
|
|
|
function cleanInputValue(inputs, schema) {
|
|
|
|
if (schema == null) {
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
for (let inputKey of Object.keys(inputs)) {
|
|
|
|
let input = inputs[inputKey]
|
|
|
|
if (typeof input !== "string") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let propSchema = schema.properties[inputKey]
|
|
|
|
if (propSchema.type === "boolean") {
|
|
|
|
let lcInput = input.toLowerCase()
|
|
|
|
if (lcInput === "true") {
|
|
|
|
inputs[inputKey] = true
|
|
|
|
}
|
|
|
|
if (lcInput === "false") {
|
|
|
|
inputs[inputKey] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (propSchema.type === "number") {
|
|
|
|
let floatInput = parseFloat(input)
|
|
|
|
if (!isNaN(floatInput)) {
|
|
|
|
inputs[inputKey] = floatInput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
function recurseMustache(inputs, context) {
|
2020-09-17 14:36:19 +02:00
|
|
|
for (let key of Object.keys(inputs)) {
|
2020-09-16 15:00:04 +02:00
|
|
|
let val = inputs[key]
|
|
|
|
if (typeof val === "string") {
|
2020-09-17 14:36:19 +02:00
|
|
|
val = cleanMustache(inputs[key])
|
|
|
|
inputs[key] = mustache.render(val, context)
|
2020-09-16 15:00:04 +02:00
|
|
|
}
|
|
|
|
// this covers objects and arrays
|
|
|
|
else if (typeof val === "object") {
|
|
|
|
inputs[key] = recurseMustache(inputs[key], context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:47:22 +02:00
|
|
|
/**
|
2020-09-21 14:49:34 +02:00
|
|
|
* The automation orchestrator is a class responsible for executing automations.
|
|
|
|
* It handles the context of the automation and makes sure each step gets the correct
|
2020-09-11 19:47:22 +02:00
|
|
|
* inputs and handles any outputs.
|
|
|
|
*/
|
|
|
|
class Orchestrator {
|
2020-09-21 14:49:34 +02:00
|
|
|
constructor(automation, triggerOutput) {
|
2020-09-16 15:00:04 +02:00
|
|
|
this._instanceId = triggerOutput.instanceId
|
2020-09-17 17:16:05 +02:00
|
|
|
// remove from context
|
|
|
|
delete triggerOutput.instanceId
|
2020-09-17 14:36:19 +02:00
|
|
|
// step zero is never used as the mustache is zero indexed for customer facing
|
|
|
|
this._context = { steps: [{}], trigger: triggerOutput }
|
2020-09-21 14:49:34 +02:00
|
|
|
this._automation = automation
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
async getStepFunctionality(type, stepId) {
|
2020-09-11 19:47:22 +02:00
|
|
|
let step = null
|
|
|
|
if (type === "ACTION") {
|
|
|
|
step = await actions.getAction(stepId)
|
|
|
|
} else if (type === "LOGIC") {
|
|
|
|
step = logic.getLogic(stepId)
|
|
|
|
}
|
|
|
|
if (step == null) {
|
2020-09-21 14:49:34 +02:00
|
|
|
throw `Cannot find automation step by name ${stepId}`
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
async execute() {
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = this._automation
|
|
|
|
for (let step of automation.definition.steps) {
|
2020-09-17 14:36:19 +02:00
|
|
|
let stepFn = await this.getStepFunctionality(step.type, step.stepId)
|
|
|
|
step.inputs = recurseMustache(step.inputs, this._context)
|
2020-09-23 12:54:15 +02:00
|
|
|
step.inputs = cleanInputValue(step.inputs, step.schema.inputs)
|
2020-09-16 15:00:04 +02:00
|
|
|
// instanceId is always passed
|
2020-09-23 13:29:20 +02:00
|
|
|
try {
|
|
|
|
const outputs = await stepFn({
|
|
|
|
inputs: step.inputs,
|
|
|
|
instanceId: this._instanceId,
|
|
|
|
})
|
|
|
|
if (step.stepId === FILTER_STEP_ID && !outputs.success) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
this._context.steps.push(outputs)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Automation error - ${step.stepId} - ${err}`)
|
2020-09-17 17:16:05 +02:00
|
|
|
}
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:30:35 +02:00
|
|
|
// callback is required for worker-farm to state that the worker thread has completed
|
2020-09-11 19:47:22 +02:00
|
|
|
module.exports = async (job, cb = null) => {
|
|
|
|
try {
|
2020-09-21 14:49:34 +02:00
|
|
|
const automationOrchestrator = new Orchestrator(
|
|
|
|
job.data.automation,
|
2020-09-16 15:00:04 +02:00
|
|
|
job.data.event
|
|
|
|
)
|
2020-09-21 14:49:34 +02:00
|
|
|
await automationOrchestrator.execute()
|
2020-09-11 19:47:22 +02:00
|
|
|
if (cb) {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (cb) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|