2020-09-16 15:00:04 +02:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const newid = require("../../db/newid")
|
2020-09-21 14:49:34 +02:00
|
|
|
const actions = require("../../automations/actions")
|
|
|
|
const logic = require("../../automations/logic")
|
|
|
|
const triggers = require("../../automations/triggers")
|
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]
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-20 18:02:46 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-06-18 17:59:31 +02:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = ctx.request.body
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2020-09-21 14:49:34 +02:00
|
|
|
automation._id = newid()
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2020-09-21 14:49:34 +02:00
|
|
|
automation.type = "automation"
|
|
|
|
automation = cleanAutomationInputs(automation)
|
|
|
|
const response = await db.post(automation)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
exports.update = async function(ctx) {
|
2020-06-18 17:59:31 +02:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = ctx.request.body
|
2020-05-22 17:32:23 +02:00
|
|
|
|
2020-09-21 14:49:34 +02:00
|
|
|
automation = cleanAutomationInputs(automation)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = async function(ctx) {
|
2020-06-18 17:59:31 +02:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-20 18:02:46 +02:00
|
|
|
const response = await db.query(`database/by_type`, {
|
2020-09-21 14:49:34 +02:00
|
|
|
key: ["automation"],
|
2020-05-20 18:02:46 +02:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.find = async function(ctx) {
|
2020-06-01 22:26:32 +02:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-20 18:02:46 +02:00
|
|
|
ctx.body = await db.get(ctx.params.id)
|
|
|
|
}
|
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
exports.destroy = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
|
|
|
ctx.body = await db.remove(ctx.params.id, ctx.params.rev)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getActionList = async function(ctx) {
|
2020-09-18 17:50:52 +02:00
|
|
|
ctx.body = actions.DEFINITIONS
|
2020-05-26 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
exports.getTriggerList = async function(ctx) {
|
2020-09-16 15:00:04 +02:00
|
|
|
ctx.body = triggers.BUILTIN_DEFINITIONS
|
2020-09-10 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getLogicList = async function(ctx) {
|
2020-09-16 15:00:04 +02:00
|
|
|
ctx.body = logic.BUILTIN_DEFINITIONS
|
2020-09-10 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
2020-09-14 16:34:09 +02:00
|
|
|
module.exports.getDefinitionList = async function(ctx) {
|
|
|
|
ctx.body = {
|
2020-09-16 15:00:04 +02:00
|
|
|
logic: logic.BUILTIN_DEFINITIONS,
|
|
|
|
trigger: triggers.BUILTIN_DEFINITIONS,
|
2020-09-18 17:50:52 +02:00
|
|
|
action: actions.DEFINITIONS,
|
2020-09-14 16:34:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
/*********************
|
|
|
|
* *
|
|
|
|
* API FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*********************/
|
|
|
|
|
|
|
|
exports.trigger = async function(ctx) {
|
2020-09-10 16:00:21 +02:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
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,
|
|
|
|
instanceId: ctx.user.instanceId,
|
|
|
|
})
|
2020-09-14 11:30:35 +02:00
|
|
|
ctx.status = 200
|
|
|
|
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
|
|
|
}
|