2020-10-27 14:19:38 +01:00
|
|
|
const handlebars = require("handlebars")
|
2020-09-11 19:47:22 +02:00
|
|
|
const actions = require("./actions")
|
|
|
|
const logic = require("./logic")
|
2020-09-23 14:34:11 +02:00
|
|
|
const automationUtils = require("./automationUtils")
|
2020-09-11 19:47:22 +02:00
|
|
|
|
2020-10-27 14:19:38 +01:00
|
|
|
handlebars.registerHelper("object", value => {
|
|
|
|
return new handlebars.SafeString(JSON.stringify(value))
|
|
|
|
})
|
|
|
|
|
2020-09-17 17:16:05 +02:00
|
|
|
const FILTER_STEP_ID = logic.BUILTIN_DEFINITIONS.FILTER.stepId
|
|
|
|
|
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-23 14:34:11 +02:00
|
|
|
val = automationUtils.cleanMustache(inputs[key])
|
2020-10-27 14:19:38 +01:00
|
|
|
const template = handlebars.compile(val)
|
|
|
|
inputs[key] = template(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 14:34:11 +02:00
|
|
|
step.inputs = automationUtils.cleanInputValues(
|
|
|
|
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,
|
2020-10-08 18:34:41 +02:00
|
|
|
apiKey: automation.apiKey,
|
2020-09-23 13:29:20 +02:00
|
|
|
})
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|