2020-09-17 01:00:04 +12: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 22:06:13 +12:00
|
|
|
|
|
|
|
/*************************
|
|
|
|
* *
|
|
|
|
* BUILDER FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*************************/
|
2020-05-21 04:02:46 +12:00
|
|
|
|
2020-09-19 01:34:14 +12: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-21 04:02:46 +12:00
|
|
|
exports.create = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-09-19 01:34:14 +12:00
|
|
|
let workflow = ctx.request.body
|
2020-05-21 04:02:46 +12:00
|
|
|
|
|
|
|
workflow._id = newid()
|
|
|
|
|
|
|
|
workflow.type = "workflow"
|
2020-09-19 01:34:14 +12:00
|
|
|
workflow = cleanWorkflowInputs(workflow)
|
2020-05-21 04:02:46 +12: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 23:51:19 +12:00
|
|
|
...response,
|
2020-05-29 07:20:03 +12:00
|
|
|
},
|
|
|
|
}
|
2020-05-21 04:02:46 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.update = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-09-19 01:34:14 +12:00
|
|
|
let workflow = ctx.request.body
|
2020-05-23 03:32:23 +12:00
|
|
|
|
2020-09-19 01:34:14 +12:00
|
|
|
workflow = cleanWorkflowInputs(workflow)
|
2020-05-23 03:32:23 +12: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-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
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = async function(ctx) {
|
2020-06-19 03:59:31 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-21 04:02:46 +12:00
|
|
|
const response = await db.query(`database/by_type`, {
|
2020-05-28 08:51:32 +12:00
|
|
|
key: ["workflow"],
|
2020-05-21 04:02:46 +12:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.find = async function(ctx) {
|
2020-06-02 08:26:32 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
2020-05-21 04:02:46 +12:00
|
|
|
ctx.body = await db.get(ctx.params.id)
|
|
|
|
}
|
|
|
|
|
2020-09-10 22:06:13 +12: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-17 01:00:04 +12:00
|
|
|
ctx.body = actions.BUILTIN_DEFINITIONS
|
2020-05-27 08:34:01 +12:00
|
|
|
}
|
|
|
|
|
2020-09-10 22:06:13 +12:00
|
|
|
exports.getTriggerList = async function(ctx) {
|
2020-09-17 01:00:04 +12:00
|
|
|
ctx.body = triggers.BUILTIN_DEFINITIONS
|
2020-09-10 22:06:13 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getLogicList = async function(ctx) {
|
2020-09-17 01:00:04 +12:00
|
|
|
ctx.body = logic.BUILTIN_DEFINITIONS
|
2020-09-10 22:06:13 +12:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:34:09 +12:00
|
|
|
module.exports.getDefinitionList = async function(ctx) {
|
|
|
|
ctx.body = {
|
2020-09-17 01:00:04 +12:00
|
|
|
logic: logic.BUILTIN_DEFINITIONS,
|
|
|
|
trigger: triggers.BUILTIN_DEFINITIONS,
|
|
|
|
action: actions.BUILTIN_DEFINITIONS,
|
2020-09-15 02:34:09 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 22:06:13 +12:00
|
|
|
/*********************
|
|
|
|
* *
|
|
|
|
* API FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*********************/
|
|
|
|
|
|
|
|
exports.trigger = async function(ctx) {
|
2020-09-11 02:00:21 +12:00
|
|
|
const db = new CouchDB(ctx.user.instanceId)
|
|
|
|
let workflow = await db.get(ctx.params.id)
|
2020-09-14 20:12:17 +12:00
|
|
|
await triggers.externalTrigger(workflow, {
|
|
|
|
...ctx.request.body,
|
|
|
|
instanceId: ctx.user.instanceId,
|
|
|
|
})
|
2020-09-14 21:30:35 +12:00
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message: `Workflow ${workflow._id} has been triggered.`,
|
|
|
|
workflow,
|
|
|
|
}
|
2020-05-21 04:02:46 +12:00
|
|
|
}
|