2020-09-16 15:00:04 +02:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const newid = require("../../db/newid")
|
|
|
|
const actions = require("../../workflows/actions")
|
|
|
|
const logic = require("../../workflows/logic")
|
|
|
|
const triggers = require("../../workflows/triggers")
|
2020-09-10 12:06:13 +02:00
|
|
|
|
|
|
|
/*************************
|
|
|
|
* *
|
|
|
|
* BUILDER FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*************************/
|
2020-05-20 18:02:46 +02:00
|
|
|
|
2020-09-18 15:34:14 +02:00
|
|
|
function cleanWorkflowInputs(workflow) {
|
|
|
|
if (workflow == null) {
|
|
|
|
return workflow
|
|
|
|
}
|
|
|
|
let steps = workflow.definition.steps
|
|
|
|
let trigger = workflow.definition.trigger
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return workflow
|
|
|
|
}
|
|
|
|
|
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-18 15:34:14 +02:00
|
|
|
let workflow = ctx.request.body
|
2020-05-20 18:02:46 +02:00
|
|
|
|
|
|
|
workflow._id = newid()
|
|
|
|
|
|
|
|
workflow.type = "workflow"
|
2020-09-18 15:34:14 +02:00
|
|
|
workflow = cleanWorkflowInputs(workflow)
|
2020-05-20 18:02:46 +02:00
|
|
|
const response = await db.post(workflow)
|
|
|
|
workflow._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message: "Workflow created successfully",
|
|
|
|
workflow: {
|
|
|
|
...workflow,
|
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-18 15:34:14 +02:00
|
|
|
let workflow = ctx.request.body
|
2020-05-22 17:32:23 +02:00
|
|
|
|
2020-09-18 15:34:14 +02:00
|
|
|
workflow = cleanWorkflowInputs(workflow)
|
2020-05-22 17:32:23 +02:00
|
|
|
const response = await db.put(workflow)
|
|
|
|
workflow._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message: `Workflow ${workflow._id} updated successfully.`,
|
|
|
|
workflow: {
|
|
|
|
...workflow,
|
|
|
|
_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-05-27 22:51:32 +02:00
|
|
|
key: ["workflow"],
|
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-16 15:00:04 +02:00
|
|
|
ctx.body = actions.BUILTIN_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,
|
|
|
|
action: actions.BUILTIN_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)
|
|
|
|
let workflow = await db.get(ctx.params.id)
|
2020-09-14 10:12:17 +02:00
|
|
|
await triggers.externalTrigger(workflow, {
|
|
|
|
...ctx.request.body,
|
|
|
|
instanceId: ctx.user.instanceId,
|
|
|
|
})
|
2020-09-14 11:30:35 +02:00
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message: `Workflow ${workflow._id} has been triggered.`,
|
|
|
|
workflow,
|
|
|
|
}
|
2020-05-20 18:02:46 +02:00
|
|
|
}
|