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