2020-09-21 14:49:34 +02:00
|
|
|
const actions = require("../../automations/actions")
|
|
|
|
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,
|
2021-12-14 18:59:02 +01:00
|
|
|
removeDeprecated,
|
2021-09-10 14:52:41 +02:00
|
|
|
} = require("../../automations/utils")
|
2021-09-13 19:03:09 +02:00
|
|
|
const { deleteEntityMetadata } = require("../../utilities")
|
|
|
|
const { MetadataTypes } = require("../../constants")
|
2021-09-10 15:37:34 +02:00
|
|
|
const { setTestFlag, clearTestFlag } = require("../../utilities/redis")
|
2022-01-28 01:05:39 +01:00
|
|
|
const { getAppDB } = require("@budibase/backend-core/context")
|
2022-04-06 14:54:57 +02:00
|
|
|
const { events } = require("@budibase/backend-core")
|
2021-05-18 17:37:54 +02:00
|
|
|
|
2021-12-14 18:59:02 +01:00
|
|
|
const ACTION_DEFS = removeDeprecated(actions.ACTION_DEFINITIONS)
|
|
|
|
const TRIGGER_DEFS = removeDeprecated(triggers.TRIGGER_DEFINITIONS)
|
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
/*************************
|
|
|
|
* *
|
|
|
|
* BUILDER FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*************************/
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2022-01-28 01:05:39 +01:00
|
|
|
async function cleanupAutomationMetadata(automationId) {
|
|
|
|
await deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId)
|
2021-09-13 19:03:09 +02:00
|
|
|
await deleteEntityMetadata(
|
|
|
|
MetadataTypes.AUTOMATION_TEST_HISTORY,
|
|
|
|
automationId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
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({
|
|
|
|
newAuto: automation,
|
|
|
|
})
|
2020-10-22 18:48:32 +02:00
|
|
|
const response = await db.put(automation)
|
2022-04-06 14:54:57 +02:00
|
|
|
events.automation.created()
|
|
|
|
for (let step of automation.definition.steps) {
|
|
|
|
events.automation.stepCreated(step)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-04-06 14:54:57 +02:00
|
|
|
const getNewSteps = (oldAutomation, automation) => {
|
|
|
|
const oldStepIds = oldAutomation.definition.steps.map(s => s.id)
|
|
|
|
return automation.definition.steps.filter(s => !oldStepIds.includes(s.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
const getDeletedSteps = (oldAutomation, automation) => {
|
|
|
|
const stepIds = automation.definition.steps.map(s => s.id)
|
|
|
|
return oldAutomation.definition.steps.filter(s => !stepIds.includes(s.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleStepEvents = (oldAutomation, automation) => {
|
|
|
|
// new steps
|
|
|
|
const newSteps = getNewSteps(oldAutomation, automation)
|
|
|
|
for (let step of newSteps) {
|
|
|
|
events.automation.stepCreated(step)
|
|
|
|
}
|
|
|
|
|
|
|
|
// old steps
|
|
|
|
const deletedSteps = getDeletedSteps(oldAutomation, automation)
|
|
|
|
for (let step of deletedSteps) {
|
|
|
|
events.automation.stepDeleted(step)
|
|
|
|
}
|
|
|
|
}
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.update = async function (ctx) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
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({
|
|
|
|
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
|
|
|
|
2021-09-13 19:03:09 +02:00
|
|
|
const oldAutoTrigger =
|
|
|
|
oldAutomation && oldAutomation.definition.trigger
|
|
|
|
? oldAutomation.definition.trigger
|
2022-04-06 14:54:57 +02:00
|
|
|
: undefined
|
2021-09-13 19:03:09 +02:00
|
|
|
const newAutoTrigger =
|
|
|
|
automation && automation.definition.trigger
|
|
|
|
? automation.definition.trigger
|
|
|
|
: {}
|
|
|
|
// trigger has been updated, remove the test inputs
|
2022-04-06 14:54:57 +02:00
|
|
|
if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger.id) {
|
|
|
|
events.automation.triggerUpdated()
|
2021-09-13 19:03:09 +02:00
|
|
|
await deleteEntityMetadata(
|
|
|
|
ctx.appId,
|
|
|
|
MetadataTypes.AUTOMATION_TEST_INPUT,
|
|
|
|
automation._id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:54:57 +02:00
|
|
|
handleStepEvents(oldAutomation, automation)
|
|
|
|
|
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) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
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) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
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) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
2021-09-13 19:03:09 +02:00
|
|
|
const automationId = ctx.params.id
|
|
|
|
const oldAutomation = await db.get(automationId)
|
2021-03-29 18:32:05 +02:00
|
|
|
await checkForWebhooks({
|
2021-05-18 22:03:26 +02:00
|
|
|
oldAuto: oldAutomation,
|
|
|
|
})
|
2021-09-13 19:03:09 +02:00
|
|
|
// delete metadata first
|
2022-01-28 01:05:39 +01:00
|
|
|
await cleanupAutomationMetadata(automationId)
|
2021-09-13 19:03:09 +02:00
|
|
|
ctx.body = await db.remove(automationId, ctx.params.rev)
|
2022-04-06 14:54:57 +02:00
|
|
|
events.automation.deleted()
|
2020-09-10 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.getActionList = async function (ctx) {
|
2021-12-14 18:59:02 +01:00
|
|
|
ctx.body = ACTION_DEFS
|
2020-05-26 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.getTriggerList = async function (ctx) {
|
2021-12-14 18:59:02 +01:00
|
|
|
ctx.body = TRIGGER_DEFS
|
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-12-14 18:59:02 +01:00
|
|
|
trigger: TRIGGER_DEFS,
|
|
|
|
action: ACTION_DEFS,
|
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) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
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,
|
2022-01-28 01:05:39 +01:00
|
|
|
appId: ctx.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-17 18:18:52 +02:00
|
|
|
function prepareTestInput(input) {
|
|
|
|
// prepare the test parameters
|
|
|
|
if (input.id && input.row) {
|
|
|
|
input.row._id = input.id
|
|
|
|
}
|
|
|
|
if (input.revision && input.row) {
|
|
|
|
input.row._rev = input.revision
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:59:58 +02:00
|
|
|
exports.test = async function (ctx) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
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-17 18:18:52 +02:00
|
|
|
const testInput = prepareTestInput(ctx.request.body)
|
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
|
|
|
{
|
2021-09-17 18:18:52 +02:00
|
|
|
...testInput,
|
2022-01-28 01:05:39 +01:00
|
|
|
appId: ctx.appId,
|
2021-09-08 15:08:22 +02:00
|
|
|
},
|
|
|
|
{ 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,
|
2021-09-14 17:54:42 +02:00
|
|
|
occurredAt: new Date().getTime(),
|
2021-09-10 14:52:41 +02:00
|
|
|
})
|
2021-09-10 15:37:34 +02:00
|
|
|
await clearTestFlag(automation._id)
|
2021-09-08 20:29:28 +02:00
|
|
|
ctx.body = response
|
2022-04-06 14:54:57 +02:00
|
|
|
events.automation.tested()
|
2021-09-07 14:59:58 +02:00
|
|
|
}
|