From e2791d832b500f14438d1704a5d02a000a652605 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 15 Sep 2020 14:27:23 +0100 Subject: [PATCH] This is a breaking change, it updates the block definitions to work with the new structure of inputs and outputs. --- packages/server/src/api/controllers/record.js | 1 - packages/server/src/api/controllers/user.js | 1 + .../controllers/workflow/blockDefinitions.js | 190 ++++++++++++++++-- packages/server/src/utilities/accessLevels.js | 6 + packages/server/src/workflows/actions.js | 30 ++- packages/server/src/workflows/logic.js | 7 + 6 files changed, 203 insertions(+), 32 deletions(-) diff --git a/packages/server/src/api/controllers/record.js b/packages/server/src/api/controllers/record.js index f9cea706f5..8fc041c6af 100644 --- a/packages/server/src/api/controllers/record.js +++ b/packages/server/src/api/controllers/record.js @@ -53,7 +53,6 @@ exports.patch = async function(ctx) { ctx.body = record ctx.status = 200 ctx.message = `${model.name} updated successfully.` - return } exports.save = async function(ctx) { diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index d4525974f9..22cf2d2a3c 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -58,6 +58,7 @@ exports.create = async function(ctx) { ctx.message = "User created successfully." ctx.body = { _rev: response.rev, + _id: user._id, username, name, } diff --git a/packages/server/src/api/controllers/workflow/blockDefinitions.js b/packages/server/src/api/controllers/workflow/blockDefinitions.js index f713823245..7cd0f8ab05 100644 --- a/packages/server/src/api/controllers/workflow/blockDefinitions.js +++ b/packages/server/src/api/controllers/workflow/blockDefinitions.js @@ -1,28 +1,72 @@ +let accessLevels = require("../../../utilities/accessLevels") +let conditions = require("../../../workflows/logic").LogicConditions + const ACTION = { SAVE_RECORD: { name: "Save Record", tagline: "Save a {{record.model.name}} record", icon: "ri-save-3-fill", - description: "Save a record to your database.", - params: { - record: "record", - }, - args: { - record: {}, - }, + description: "Save a record to your database", type: "ACTION", + input: { + record: { + type: "record", + label: "The record to be written", + default: {}, + }, + model: { + type: "model", + label: "The table to save a record to", + }, + }, + output: { + response: { + type: "object", + label: "The response from the table", + }, + success: { + type: "boolean", + label: "Whether the action was successful", + }, + id: { + type: "string", + label: "The identifier of the new record", + }, + revision: { + type: "string", + label: "The revision of the new record", + }, + }, }, DELETE_RECORD: { - description: "Delete a record from your database.", + description: "Delete a record from your database", icon: "ri-delete-bin-line", name: "Delete Record", tagline: "Delete a {{record.model.name}} record", - params: {}, - args: {}, type: "ACTION", + input: { + id: { + type: "string", + label: "The identifier of the record to be deleted", + }, + revision: { + type: "string", + label: "The revision of the record to be deleted", + }, + }, + output: { + response: { + type: "object", + label: "The response from the table", + }, + success: { + type: "boolean", + label: "Whether the action was successful", + }, + }, }, CREATE_USER: { - description: "Create a new user.", + description: "Create a new user", tagline: "Create user {{username}}", icon: "ri-user-add-fill", name: "Create User", @@ -35,6 +79,40 @@ const ACTION = { accessLevelId: "POWER_USER", }, type: "ACTION", + input: { + username: { + type: "string", + label: "The username of the new user to create", + }, + password: { + type: "password", + label: "The password of the new user to create", + }, + accessLevelId: { + type: "string", + label: "The level of access to the system the new user will have", + default: accessLevels.POWERUSER_LEVEL_ID, + options: accessLevels.ACCESS_LEVELS, + }, + }, + output: { + id: { + type: "string", + label: "The identifier of the new user", + }, + revision: { + type: "string", + label: "The revision of the new user", + }, + response: { + type: "object", + label: "The response from the user table", + }, + success: { + type: "boolean", + label: "Whether the action was successful", + }, + }, }, SEND_EMAIL: { description: "Send an email.", @@ -48,6 +126,34 @@ const ACTION = { text: "longText", }, type: "ACTION", + input: { + to: { + type: "string", + label: "Email address to send email to", + }, + from: { + type: "string", + label: "Email address to send email from", + }, + subject: { + type: "string", + label: "The subject of the email", + }, + contents: { + type: "string", + label: "The contents of the email", + }, + }, + output: { + success: { + type: "boolean", + label: "Whether the email was sent", + }, + response: { + type: "object", + label: "A response from the email client, this may be an error", + }, + }, }, } @@ -56,7 +162,7 @@ const LOGIC = { name: "Filter", tagline: "{{filter}} {{condition}} {{value}}", icon: "ri-git-branch-line", - description: "Filter any workflows which do not meet certain conditions.", + description: "Filter any workflows which do not meet certain conditions", params: { filter: "string", condition: ["equals"], @@ -66,14 +172,38 @@ const LOGIC = { condition: "equals", }, type: "LOGIC", + input: { + field: { + type: "string", + label: "The input to filter on", + }, + condition: { + type: "string", + label: "The condition to use for filtering", + options: conditions, + }, + value: { + type: "string", + label: "The value to compare against", + }, + }, + output: { + success: { + type: "boolean", + label: "Whether the logic block passed", + }, + }, }, DELAY: { name: "Delay", icon: "ri-time-fill", tagline: "Delay for {{time}} milliseconds", - description: "Delay the workflow until an amount of time has passed.", - params: { - time: "number", + description: "Delay the workflow until an amount of time has passed", + input: { + time: { + type: "number", + label: "The duration of the delay in milliseconds", + }, }, type: "LOGIC", }, @@ -85,9 +215,18 @@ const TRIGGER = { event: "record:save", icon: "ri-save-line", tagline: "Record is added to {{model.name}}", - description: "Fired when a record is saved to your database.", - params: { - model: "model", + description: "Fired when a record is saved to your database", + input: { + model: { + type: "model", + label: "The table to trigger on when a new record is saved", + }, + }, + output: { + record: { + type: "record", + label: "The new record that was saved", + }, }, type: "TRIGGER", }, @@ -96,9 +235,18 @@ const TRIGGER = { event: "record:delete", icon: "ri-delete-bin-line", tagline: "Record is deleted from {{model.name}}", - description: "Fired when a record is deleted from your database.", - params: { - model: "model", + description: "Fired when a record is deleted from your database", + input: { + model: { + type: "model", + label: "The table to trigger on when a record is deleted", + }, + }, + output: { + record: { + type: "record", + label: "The record that was deleted", + }, }, type: "TRIGGER", }, diff --git a/packages/server/src/utilities/accessLevels.js b/packages/server/src/utilities/accessLevels.js index 50ae559d07..bc8ae4cb77 100644 --- a/packages/server/src/utilities/accessLevels.js +++ b/packages/server/src/utilities/accessLevels.js @@ -87,6 +87,12 @@ module.exports = { POWERUSER_LEVEL_ID, BUILDER_LEVEL_ID, ANON_LEVEL_ID, + ACCESS_LEVELS: [ + ADMIN_LEVEL_ID, + POWERUSER_LEVEL_ID, + BUILDER_LEVEL_ID, + ANON_LEVEL_ID, + ], READ_MODEL, WRITE_MODEL, READ_VIEW, diff --git a/packages/server/src/workflows/actions.js b/packages/server/src/workflows/actions.js index 1dd3aa694d..626a0af922 100644 --- a/packages/server/src/workflows/actions.js +++ b/packages/server/src/workflows/actions.js @@ -17,14 +17,18 @@ let BUILTIN_ACTIONS = { } try { - const response = await userController.create(ctx) + await userController.create(ctx) return { - user: response, + response: ctx.body, + id: ctx.body._id, + revision: ctx.body._rev, + success: ctx.status === 200, } } catch (err) { console.error(err) return { - user: null, + success: false, + response: err, } } }, @@ -45,13 +49,16 @@ let BUILTIN_ACTIONS = { try { await recordController.save(ctx) return { - record: ctx.body, + response: ctx.body, + id: ctx.body._id, + revision: ctx.body._rev, + success: ctx.status === 200, } } catch (err) { console.error(err) return { - record: null, - error: err.message, + success: false, + response: err, } } }, @@ -67,13 +74,12 @@ let BUILTIN_ACTIONS = { await sgMail.send(msg) return { success: true, - ...args, } } catch (err) { console.error(err) return { success: false, - error: err.message, + response: err, } } }, @@ -94,11 +100,15 @@ let BUILTIN_ACTIONS = { try { await recordController.destroy(ctx) + return { + response: ctx.body, + success: ctx.status === 200, + } } catch (err) { console.error(err) return { - record: null, - error: err.message, + success: false, + response: err, } } }, diff --git a/packages/server/src/workflows/logic.js b/packages/server/src/workflows/logic.js index a1d71bc0f2..376a712a4d 100644 --- a/packages/server/src/workflows/logic.js +++ b/packages/server/src/workflows/logic.js @@ -22,3 +22,10 @@ module.exports.getLogic = function(logicName) { return LOGIC[logicName] } } + +module.exports.LogicConditions = [ + "equals", + "notEquals", + "greaterThan", + "lessThan", +]