2020-06-01 22:26:32 +02:00
|
|
|
const CouchDB = require("../../../db")
|
|
|
|
const newid = require("../../../db/newid")
|
2020-09-10 12:06:13 +02:00
|
|
|
const blockDefinitions = require("./blockDefinitions")
|
2020-09-10 16:00:21 +02:00
|
|
|
const triggers = require("../../../workflows/triggers")
|
2020-09-10 12:06:13 +02:00
|
|
|
|
|
|
|
/*************************
|
|
|
|
* *
|
|
|
|
* BUILDER FUNCTIONS *
|
|
|
|
* *
|
|
|
|
*************************/
|
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-05-20 18:02:46 +02:00
|
|
|
const workflow = ctx.request.body
|
|
|
|
|
|
|
|
workflow._id = newid()
|
|
|
|
|
|
|
|
workflow.type = "workflow"
|
|
|
|
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-05-28 21:20:03 +02:00
|
|
|
const workflow = ctx.request.body
|
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) {
|
|
|
|
ctx.body = blockDefinitions.ACTION
|
2020-05-26 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 12:06:13 +02:00
|
|
|
exports.getTriggerList = async function(ctx) {
|
|
|
|
ctx.body = blockDefinitions.TRIGGER
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getLogicList = async function(ctx) {
|
2020-09-10 16:00:21 +02:00
|
|
|
ctx.body = blockDefinitions.LOGIC
|
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-05-20 18:02:46 +02:00
|
|
|
}
|