2020-09-16 15:00:04 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-09-21 14:49:34 +02:00
|
|
|
const actions = require("../../automations/actions")
|
|
|
|
const logic = require("../../automations/logic")
|
|
|
|
const triggers = require("../../automations/triggers")
|
2021-09-08 20:29:28 +02:00
|
|
|
const { getAutomationParams, generateAutomationID } = require("../../db/utils")
|
2021-09-10 14:52:41 +02:00
|
|
|
const {
|
|
|
|
checkForWebhooks,
|
|
|
|
updateTestHistory,
|
|
|
|
} = require("../../automations/utils")
|
2021-09-10 15:37:34 +02:00
|
|
|
const { setTestFlag, clearTestFlag } = require("../../utilities/redis")
|
2021-05-18 17:37:54 +02:00
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
/*************************
|
|
|
|
* *
|
|
|
|
* BUILDER FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*************************/
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2020-09-21 14:49:34 +02:00
|
|
|
function cleanAutomationInputs(automation) {
|
|
|
|
if (automation == null) {
|
|
|
|
return automation
|
2020-09-18 15:34:14 +02:00
|
|
|
}
|
2020-09-21 14:49:34 +02:00
|
|
|
let steps = automation.definition.steps
|
|
|
|
let trigger = automation.definition.trigger
|
2020-09-18 15:34:14 +02:00
|
|
|
let allSteps = [...steps, trigger]
|
2021-09-08 20:29:28 +02:00
|
|
|
// live is not a property used anymore
|
|
|
|
if (automation.live != null) {
|
|
|
|
delete automation.live
|
|
|
|
}
|
2020-09-18 15:34:14 +02:00
|
|
|
for (let step of allSteps) {
|
|
|
|
if (step == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for (let inputName of Object.keys(step.inputs)) {
|
|
|
|
if (!step.inputs[inputName] || step.inputs[inputName] === "") {
|
|
|
|
delete step.inputs[inputName]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 14:49:34 +02:00
|
|
|
return automation
|
2020-09-18 15:34:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.create = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = ctx.request.body
|
2021-03-29 18:32:05 +02:00
|
|
|
automation.appId = ctx.appId
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2021-03-10 12:47:39 +01:00
|
|
|
// call through to update if already exists
|
|
|
|
if (automation._id && automation._rev) {
|
|
|
|
return exports.update(ctx)
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:37:46 +02:00
|
|
|
automation._id = generateAutomationID()
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2020-09-21 14:49:34 +02:00
|
|
|
automation.type = "automation"
|
|
|
|
automation = cleanAutomationInputs(automation)
|
2021-03-29 18:32:05 +02:00
|
|
|
automation = await checkForWebhooks({
|
|
|
|
appId: ctx.appId,
|
|
|
|
newAuto: automation,
|
|
|
|
})
|
2020-10-22 18:48:32 +02:00
|
|
|
const response = await db.put(automation)
|
2020-09-21 14:49:34 +02:00
|
|
|
automation._rev = response.rev
|
2020-05-20 18:02:46 +02:00
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
2020-09-21 14:49:34 +02:00
|
|
|
message: "Automation created successfully",
|
|
|
|
automation: {
|
|
|
|
...automation,
|
2020-05-27 13:51:19 +02:00
|
|
|
...response,
|
2020-05-28 21:20:03 +02:00
|
|
|
},
|
|
|
|
}
|
2020-05-20 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.update = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = ctx.request.body
|
2021-03-29 18:32:05 +02:00
|
|
|
automation.appId = ctx.appId
|
2020-10-22 18:48:32 +02:00
|
|
|
const oldAutomation = await db.get(automation._id)
|
2020-09-21 14:49:34 +02:00
|
|
|
automation = cleanAutomationInputs(automation)
|
2020-10-22 18:48:32 +02:00
|
|
|
automation = await checkForWebhooks({
|
2021-03-29 18:32:05 +02:00
|
|
|
appId: ctx.appId,
|
2020-10-22 18:48:32 +02:00
|
|
|
oldAuto: oldAutomation,
|
|
|
|
newAuto: automation,
|
|
|
|
})
|
2020-09-21 14:49:34 +02:00
|
|
|
const response = await db.put(automation)
|
|
|
|
automation._rev = response.rev
|
2020-05-22 17:32:23 +02:00
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
2020-09-21 14:49:34 +02:00
|
|
|
message: `Automation ${automation._id} updated successfully.`,
|
|
|
|
automation: {
|
|
|
|
...automation,
|
2020-05-22 17:32:23 +02:00
|
|
|
_rev: response.rev,
|
2020-05-28 21:20:03 +02:00
|
|
|
_id: response.id,
|
2020-05-22 17:32:23 +02:00
|
|
|
},
|
|
|
|
}
|
2020-05-20 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-10-01 18:22:08 +02:00
|
|
|
const response = await db.allDocs(
|
|
|
|
getAutomationParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
2020-05-20 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.find = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-05-20 18:02:46 +02:00
|
|
|
ctx.body = await db.get(ctx.params.id)
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.destroy = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-10-22 18:48:32 +02:00
|
|
|
const oldAutomation = await db.get(ctx.params.id)
|
2021-03-29 18:32:05 +02:00
|
|
|
await checkForWebhooks({
|
|
|
|
appId: ctx.appId,
|
2021-05-18 22:03:26 +02:00
|
|
|
oldAuto: oldAutomation,
|
|
|
|
})
|
2020-09-10 12:06:13 +02:00
|
|
|
ctx.body = await db.remove(ctx.params.id, ctx.params.rev)
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.getActionList = async function (ctx) {
|
2021-09-06 18:53:02 +02:00
|
|
|
ctx.body = actions.ACTION_DEFINITIONS
|
2020-05-26 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.getTriggerList = async function (ctx) {
|
2021-09-06 18:53:02 +02:00
|
|
|
ctx.body = triggers.TRIGGER_DEFINITIONS
|
2020-09-10 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.getLogicList = async function (ctx) {
|
2021-09-06 18:53:02 +02:00
|
|
|
ctx.body = logic.LOGIC_DEFINITIONS
|
2020-09-10 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
module.exports.getDefinitionList = async function (ctx) {
|
2020-09-14 16:34:09 +02:00
|
|
|
ctx.body = {
|
2021-09-06 18:53:02 +02:00
|
|
|
logic: logic.LOGIC_DEFINITIONS,
|
|
|
|
trigger: triggers.TRIGGER_DEFINITIONS,
|
|
|
|
action: actions.ACTION_DEFINITIONS,
|
2020-09-14 16:34:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
/*********************
|
|
|
|
* *
|
|
|
|
* API FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*********************/
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.trigger = async function (ctx) {
|
2021-09-08 15:08:22 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = await db.get(ctx.params.id)
|
|
|
|
await triggers.externalTrigger(automation, {
|
2020-09-14 10:12:17 +02:00
|
|
|
...ctx.request.body,
|
2021-09-08 15:08:22 +02:00
|
|
|
appId,
|
2020-09-14 10:12:17 +02:00
|
|
|
})
|
2020-09-14 11:30:35 +02:00
|
|
|
ctx.body = {
|
2020-09-21 14:49:34 +02:00
|
|
|
message: `Automation ${automation._id} has been triggered.`,
|
|
|
|
automation,
|
2020-09-14 11:30:35 +02:00
|
|
|
}
|
2020-05-20 18:02:46 +02:00
|
|
|
}
|
2021-09-07 14:58:53 +02:00
|
|
|
|
2021-09-07 14:59:58 +02:00
|
|
|
exports.test = async function (ctx) {
|
2021-09-08 15:08:22 +02:00
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
2021-09-07 20:06:20 +02:00
|
|
|
let automation = await db.get(ctx.params.id)
|
2021-09-10 15:37:34 +02:00
|
|
|
await setTestFlag(automation._id)
|
2021-09-08 20:29:28 +02:00
|
|
|
const response = await triggers.externalTrigger(
|
2021-09-07 20:06:20 +02:00
|
|
|
automation,
|
2021-09-08 15:08:22 +02:00
|
|
|
{
|
|
|
|
...ctx.request.body,
|
|
|
|
appId,
|
|
|
|
},
|
|
|
|
{ getResponses: true }
|
|
|
|
)
|
2021-09-08 20:29:28 +02:00
|
|
|
// save a test history run
|
2021-09-10 14:52:41 +02:00
|
|
|
await updateTestHistory(ctx.appId, automation, {
|
|
|
|
...ctx.request.body,
|
|
|
|
occurredAt: new Date().toISOString(),
|
|
|
|
})
|
2021-09-10 15:37:34 +02:00
|
|
|
await clearTestFlag(automation._id)
|
2021-09-08 20:29:28 +02:00
|
|
|
ctx.body = response
|
2021-09-07 14:59:58 +02:00
|
|
|
}
|