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

109 lines
2.6 KiB
JavaScript
Raw Normal View History

const CouchDB = require("../../db")
const newid = require("../../db/newid")
const actions = require("../../workflows/actions")
const logic = require("../../workflows/logic")
const triggers = require("../../workflows/triggers")
/*************************
* *
* BUILDER FUNCTIONS *
* *
*************************/
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-05-21 04:02:46 +12: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 23:51:19 +12:00
...response,
},
}
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)
const workflow = ctx.request.body
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,
_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`, {
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)
}
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 = actions.BUILTIN_DEFINITIONS
2020-05-27 08:34:01 +12:00
}
exports.getTriggerList = async function(ctx) {
ctx.body = triggers.BUILTIN_DEFINITIONS
}
exports.getLogicList = async function(ctx) {
ctx.body = logic.BUILTIN_DEFINITIONS
}
module.exports.getDefinitionList = async function(ctx) {
ctx.body = {
logic: logic.BUILTIN_DEFINITIONS,
trigger: triggers.BUILTIN_DEFINITIONS,
action: actions.BUILTIN_DEFINITIONS,
}
}
/*********************
* *
* API FUNCTIONS *
* *
*********************/
exports.trigger = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId)
let workflow = await db.get(ctx.params.id)
await triggers.externalTrigger(workflow, {
...ctx.request.body,
instanceId: ctx.user.instanceId,
})
ctx.status = 200
ctx.body = {
message: `Workflow ${workflow._id} has been triggered.`,
workflow,
}
2020-05-21 04:02:46 +12:00
}