2020-05-20 18:02:46 +02:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const controller = require("../controllers/workflow")
|
2020-05-27 18:23:01 +02:00
|
|
|
const authorized = require("../../middleware/authorized")
|
2020-09-10 18:46:34 +02:00
|
|
|
const joiValidator = require("../../middleware/joi-validator")
|
2020-06-01 22:26:32 +02:00
|
|
|
const { BUILDER } = require("../../utilities/accessLevels")
|
2020-09-10 18:46:34 +02:00
|
|
|
const Joi = require("joi")
|
2020-05-20 18:02:46 +02:00
|
|
|
|
|
|
|
const router = Router()
|
|
|
|
|
2020-09-10 18:46:34 +02:00
|
|
|
// prettier-ignore
|
|
|
|
function generateStepSchema(allowStepTypes) {
|
|
|
|
return Joi.object({
|
|
|
|
stepId: Joi.string().required(),
|
|
|
|
id: Joi.string().required(),
|
|
|
|
description: Joi.string().required(),
|
|
|
|
name: Joi.string().required(),
|
|
|
|
tagline: Joi.string().required(),
|
|
|
|
icon: Joi.string().required(),
|
|
|
|
params: Joi.object(),
|
|
|
|
// TODO: validate args a bit more deeply
|
|
|
|
args: Joi.object(),
|
|
|
|
type: Joi.string().required().valid(...allowStepTypes),
|
|
|
|
}).unknown(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// prettier-ignore
|
|
|
|
const workflowValidator = joiValidator.body(Joi.object({
|
|
|
|
live: Joi.bool(),
|
|
|
|
id: Joi.string().required(),
|
|
|
|
rev: Joi.string().required(),
|
|
|
|
name: Joi.string().required(),
|
|
|
|
type: Joi.string().valid("workflow").required(),
|
|
|
|
definition: Joi.object({
|
|
|
|
steps: Joi.array().required().items(generateStepSchema(["ACTION", "LOGIC"])),
|
|
|
|
trigger: generateStepSchema(["TRIGGER"]).required(),
|
|
|
|
}).required().unknown(true),
|
|
|
|
}).unknown(true))
|
|
|
|
|
2020-05-20 18:02:46 +02:00
|
|
|
router
|
2020-09-10 12:06:13 +02:00
|
|
|
.get("/api/workflows/trigger/list", authorized(BUILDER), controller.getTriggerList)
|
|
|
|
.get("/api/workflows/action/list", authorized(BUILDER), controller.getActionList)
|
|
|
|
.get("/api/workflows/logic/list", authorized(BUILDER), controller.getLogicList)
|
2020-06-18 17:59:31 +02:00
|
|
|
.get("/api/workflows", authorized(BUILDER), controller.fetch)
|
2020-06-01 22:26:32 +02:00
|
|
|
.get("/api/workflows/:id", authorized(BUILDER), controller.find)
|
2020-09-10 18:46:34 +02:00
|
|
|
.put("/api/workflows", authorized(BUILDER), workflowValidator, controller.update)
|
|
|
|
.post("/api/workflows", authorized(BUILDER), workflowValidator, controller.create)
|
2020-09-10 16:00:21 +02:00
|
|
|
.post("/api/workflows/:id/trigger", controller.trigger)
|
2020-06-18 17:59:31 +02:00
|
|
|
.delete("/api/workflows/:id/:rev", authorized(BUILDER), controller.destroy)
|
2020-05-20 18:02:46 +02:00
|
|
|
|
|
|
|
module.exports = router
|