1
0
Fork 0
mirror of synced 2024-06-27 18:40:42 +12:00
budibase/packages/server/src/api/controllers/automation.js

184 lines
4.5 KiB
JavaScript
Raw Normal View History

const CouchDB = require("../../db")
const actions = require("../../automations/actions")
const logic = require("../../automations/logic")
const triggers = require("../../automations/triggers")
const { getAutomationParams, generateAutomationID } = require("../../db/utils")
const {
checkForWebhooks,
updateTestHistory,
} = require("../../automations/utils")
const { setTestFlag, clearTestFlag } = require("../../utilities/redis")
2021-05-19 03:37:54 +12:00
/*************************
* *
* BUILDER FUNCTIONS *
* *
*************************/
2020-05-21 04:02:46 +12:00
function cleanAutomationInputs(automation) {
if (automation == null) {
return automation
}
let steps = automation.definition.steps
let trigger = automation.definition.trigger
let allSteps = [...steps, trigger]
// live is not a property used anymore
if (automation.live != null) {
delete automation.live
}
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]
}
}
}
return automation
}
2021-05-03 19:31:09 +12:00
exports.create = async function (ctx) {
const db = new CouchDB(ctx.appId)
let automation = ctx.request.body
automation.appId = ctx.appId
2020-05-21 04:02:46 +12:00
// call through to update if already exists
if (automation._id && automation._rev) {
return exports.update(ctx)
}
automation._id = generateAutomationID()
2020-05-21 04:02:46 +12:00
automation.type = "automation"
automation = cleanAutomationInputs(automation)
automation = await checkForWebhooks({
appId: ctx.appId,
newAuto: automation,
})
const response = await db.put(automation)
automation._rev = response.rev
2020-05-21 04:02:46 +12:00
ctx.status = 200
ctx.body = {
message: "Automation created successfully",
automation: {
...automation,
2020-05-27 23:51:19 +12:00
...response,
},
}
2020-05-21 04:02:46 +12:00
}
2021-05-03 19:31:09 +12:00
exports.update = async function (ctx) {
const db = new CouchDB(ctx.appId)
let automation = ctx.request.body
automation.appId = ctx.appId
const oldAutomation = await db.get(automation._id)
automation = cleanAutomationInputs(automation)
automation = await checkForWebhooks({
appId: ctx.appId,
oldAuto: oldAutomation,
newAuto: automation,
})
const response = await db.put(automation)
automation._rev = response.rev
2020-05-23 03:32:23 +12:00
ctx.status = 200
ctx.body = {
message: `Automation ${automation._id} updated successfully.`,
automation: {
...automation,
2020-05-23 03:32:23 +12:00
_rev: response.rev,
_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) {
const db = new CouchDB(ctx.appId)
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) {
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) {
const db = new CouchDB(ctx.appId)
const oldAutomation = await db.get(ctx.params.id)
await checkForWebhooks({
appId: ctx.appId,
2021-05-19 08:03:26 +12:00
oldAuto: oldAutomation,
})
ctx.body = await db.remove(ctx.params.id, ctx.params.rev)
}
2021-05-03 19:31:09 +12:00
exports.getActionList = async function (ctx) {
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) {
ctx.body = triggers.TRIGGER_DEFINITIONS
}
2021-05-03 19:31:09 +12:00
exports.getLogicList = async function (ctx) {
ctx.body = logic.LOGIC_DEFINITIONS
}
2021-05-03 19:31:09 +12:00
module.exports.getDefinitionList = async function (ctx) {
ctx.body = {
logic: logic.LOGIC_DEFINITIONS,
trigger: triggers.TRIGGER_DEFINITIONS,
action: actions.ACTION_DEFINITIONS,
}
}
/*********************
* *
* API FUNCTIONS *
* *
*********************/
2021-05-03 19:31:09 +12:00
exports.trigger = async function (ctx) {
const appId = ctx.appId
const db = new CouchDB(appId)
let automation = await db.get(ctx.params.id)
await triggers.externalTrigger(automation, {
...ctx.request.body,
appId,
})
ctx.body = {
message: `Automation ${automation._id} has been triggered.`,
automation,
}
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) {
const appId = ctx.appId
const db = new CouchDB(appId)
let automation = await db.get(ctx.params.id)
await setTestFlag(automation._id)
const response = await triggers.externalTrigger(
automation,
{
...ctx.request.body,
appId,
},
{ getResponses: true }
)
// save a test history run
await updateTestHistory(ctx.appId, automation, {
...ctx.request.body,
occurredAt: new Date().toISOString(),
})
await clearTestFlag(automation._id)
ctx.body = response
2021-09-08 00:59:58 +12:00
}