Browse Source

Updating block definitions to use pure JSON schema so that it can be used for easy validation.

gh-pages
mike12345567 6 years ago
parent
commit
4625b7a97e
  1. 369
      packages/server/src/api/controllers/workflow/blockDefinitions.js
  2. 34
      packages/server/src/workflows/actions.js
  3. 8
      packages/server/src/workflows/logic.js

369
packages/server/src/api/controllers/workflow/blockDefinitions.js

@ -8,33 +8,44 @@ const ACTION = {
icon: "ri-save-3-fill", icon: "ri-save-3-fill",
description: "Save a record to your database", description: "Save a record to your database",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
record: { schema: {
type: "record", inputs: {
label: "The record to be written", properties: {
default: {}, record: {
}, type: "object",
model: { customType: "record",
type: "model", title: "The record to be written",
label: "Table", default: {},
}, },
}, model: {
outputs: { type: "object",
response: { customType: "model",
type: "object", title: "Table",
label: "The response from the table", },
}, },
success: { required: ["record", "model"],
type: "boolean", },
label: "Whether the action was successful", outputs: {
}, properties: {
id: { response: {
type: "string", type: "object",
label: "The identifier of the new record", description: "The response from the table",
}, },
revision: { success: {
type: "string", type: "boolean",
label: "The revision of the new record", description: "Whether the action was successful",
},
id: {
type: "string",
description: "The identifier of the new record",
},
revision: {
type: "string",
description: "The revision of the new record",
},
},
required: ["success", "id", "revision"],
}, },
}, },
}, },
@ -44,24 +55,33 @@ const ACTION = {
name: "Delete Record", name: "Delete Record",
tagline: "Delete a {{inputs.record.model.name}} record", tagline: "Delete a {{inputs.record.model.name}} record",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
id: { schema: {
type: "string", inputs: {
label: "Record ID", properties: {
}, id: {
revision: { type: "string",
type: "string", title: "Record ID",
label: "Record Revision", },
}, revision: {
}, type: "string",
outputs: { title: "Record Revision",
response: { },
type: "object", },
label: "The response from the table", required: ["id", "revision"],
}, },
success: { outputs: {
type: "boolean", properties: {
label: "Whether the action was successful", response: {
type: "object",
description: "The response from the table",
},
success: {
type: "boolean",
description: "Whether the action was successful",
},
},
required: ["success"],
}, },
}, },
}, },
@ -71,38 +91,48 @@ const ACTION = {
icon: "ri-user-add-fill", icon: "ri-user-add-fill",
name: "Create User", name: "Create User",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
username: { schema: {
type: "string", inputs: {
label: "Username", properties: {
}, username: {
password: { type: "string",
type: "password", title: "Username",
label: "Password", },
}, password: {
accessLevelId: { type: "string",
type: "string", customType: "password",
label: "Access Level", title: "Password",
default: accessLevels.POWERUSER_LEVEL_ID, },
options: accessLevels.ACCESS_LEVELS, accessLevelId: {
}, type: "string",
}, title: "Access Level",
outputs: { default: accessLevels.POWERUSER_LEVEL_ID,
id: { enum: accessLevels.ACCESS_LEVELS,
type: "string", },
label: "The identifier of the new user", },
}, required: ["username", "password", "accessLevelId"],
revision: { },
type: "string", outputs: {
label: "The revision of the new user", properties: {
}, id: {
response: { type: "string",
type: "object", description: "The identifier of the new user",
label: "The response from the user table", },
}, revision: {
success: { type: "string",
type: "boolean", description: "The revision of the new user",
label: "Whether the action was successful", },
response: {
type: "object",
description: "The response from the user table",
},
success: {
type: "boolean",
description: "Whether the action was successful",
},
},
required: ["id", "revision", "success"],
}, },
}, },
}, },
@ -112,32 +142,42 @@ const ACTION = {
icon: "ri-mail-open-fill", icon: "ri-mail-open-fill",
name: "Send Email", name: "Send Email",
type: "ACTION", type: "ACTION",
inputs: { inputs: {},
to: { schema: {
type: "string", inputs: {
label: "Send To", properties: {
}, to: {
from: { type: "string",
type: "string", title: "Send To",
label: "Send From", },
}, from: {
subject: { type: "string",
type: "string", title: "Send From",
label: "Email Subject", },
}, subject: {
contents: { type: "string",
type: "string", title: "Email Subject",
label: "Email Contents", },
}, contents: {
}, type: "string",
outputs: { title: "Email Contents",
success: { },
type: "boolean", },
label: "Whether the email was sent", required: ["to", "from", "subject", "contents"],
}, },
response: { outputs: {
type: "object", properties: {
label: "A response from the email client, this may be an error", success: {
type: "boolean",
description: "Whether the email was sent",
},
response: {
type: "object",
description:
"A response from the email client, this may be an error",
},
},
required: ["success"],
}, },
}, },
}, },
@ -150,26 +190,33 @@ const LOGIC = {
icon: "ri-git-branch-line", 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",
type: "LOGIC", type: "LOGIC",
inputs: { inputs: {},
filter: { schema: {
type: "string", inputs: {
label: "Reference Value", filter: {
}, type: "string",
condition: { title: "Reference Value",
type: "string", },
label: "Condition", condition: {
options: conditions, type: "string",
default: "equals", title: "Condition",
}, enum: conditions,
value: { default: "equals",
type: "string", },
label: "Comparison Value", value: {
}, type: "string",
}, title: "Comparison Value",
outputs: { },
success: { required: ["filter", "condition", "value"],
type: "boolean", },
label: "Whether the logic block passed", outputs: {
properties: {
success: {
type: "boolean",
description: "Whether the logic block passed",
},
},
required: ["success"],
}, },
}, },
}, },
@ -178,10 +225,16 @@ const LOGIC = {
icon: "ri-time-fill", icon: "ri-time-fill",
tagline: "Delay for {{inputs.time}} milliseconds", tagline: "Delay for {{inputs.time}} milliseconds",
description: "Delay the workflow until an amount of time has passed", description: "Delay the workflow until an amount of time has passed",
inputs: { inputs: {},
time: { schema: {
type: "number", inputs: {
label: "Delay in milliseconds", properties: {
time: {
type: "number",
title: "Delay in milliseconds",
},
},
required: ["time"],
}, },
}, },
type: "LOGIC", type: "LOGIC",
@ -195,16 +248,27 @@ const TRIGGER = {
icon: "ri-save-line", icon: "ri-save-line",
tagline: "Record is added to {{inputs.model.name}}", tagline: "Record is added to {{inputs.model.name}}",
description: "Fired when a record is saved to your database", description: "Fired when a record is saved to your database",
inputs: { inputs: {},
model: { schema: {
type: "model", inputs: {
label: "Table", properties: {
}, model: {
}, type: "object",
outputs: { customType: "model",
record: { title: "Table",
type: "record", },
label: "The new record that was saved", },
required: ["model"],
},
outputs: {
properties: {
record: {
type: "object",
customType: "record",
description: "The new record that was saved",
},
},
required: ["record"],
}, },
}, },
type: "TRIGGER", type: "TRIGGER",
@ -215,16 +279,27 @@ const TRIGGER = {
icon: "ri-delete-bin-line", icon: "ri-delete-bin-line",
tagline: "Record is deleted from {{inputs.model.name}}", tagline: "Record is deleted from {{inputs.model.name}}",
description: "Fired when a record is deleted from your database", description: "Fired when a record is deleted from your database",
inputs: { inputs: {},
model: { schema: {
type: "model", inputs: {
label: "Table", properties: {
}, model: {
}, type: "object",
outputs: { customType: "model",
record: { title: "Table",
type: "record", },
label: "The record that was deleted", },
required: ["model"],
},
outputs: {
properties: {
record: {
type: "object",
customType: "record",
description: "The record that was deleted",
},
},
required: ["record"],
}, },
}, },
type: "TRIGGER", type: "TRIGGER",

34
packages/server/src/workflows/actions.js

@ -5,11 +5,11 @@ const sgMail = require("@sendgrid/mail")
sgMail.setApiKey(process.env.SENDGRID_API_KEY) sgMail.setApiKey(process.env.SENDGRID_API_KEY)
let BUILTIN_ACTIONS = { let BUILTIN_ACTIONS = {
CREATE_USER: async function({ args, context }) { CREATE_USER: async function(inputs) {
const { username, password, accessLevelId } = args const { username, password, accessLevelId } = inputs
const ctx = { const ctx = {
user: { user: {
instanceId: context.instanceId, instanceId: inputs.instanceId,
}, },
request: { request: {
body: { username, password, accessLevelId }, body: { username, password, accessLevelId },
@ -32,18 +32,16 @@ let BUILTIN_ACTIONS = {
} }
} }
}, },
SAVE_RECORD: async function({ args, context }) { SAVE_RECORD: async function(inputs) {
const { model, ...record } = args.record
const ctx = { const ctx = {
params: { params: {
instanceId: context.instanceId, instanceId: inputs.instanceId,
modelId: model._id, modelId: inputs.model._id,
}, },
request: { request: {
body: record, body: inputs.record,
}, },
user: { instanceId: context.instanceId }, user: { instanceId: inputs.instanceId },
} }
try { try {
@ -62,12 +60,12 @@ let BUILTIN_ACTIONS = {
} }
} }
}, },
SEND_EMAIL: async function({ args }) { SEND_EMAIL: async function(inputs) {
const msg = { const msg = {
to: args.to, to: inputs.to,
from: args.from, from: inputs.from,
subject: args.subject, subject: inputs.subject,
text: args.text, text: inputs.text,
} }
try { try {
@ -83,8 +81,8 @@ let BUILTIN_ACTIONS = {
} }
} }
}, },
DELETE_RECORD: async function({ args, context }) { DELETE_RECORD: async function(inputs) {
const { model, ...record } = args.record const { model, ...record } = inputs.record
// TODO: better logging of when actions are missed due to missing parameters // TODO: better logging of when actions are missed due to missing parameters
if (record.recordId == null || record.revId == null) { if (record.recordId == null || record.revId == null) {
return return
@ -95,7 +93,7 @@ let BUILTIN_ACTIONS = {
recordId: record.recordId, recordId: record.recordId,
revId: record.revId, revId: record.revId,
}, },
user: { instanceId: context.instanceId }, user: { instanceId: inputs.instanceId },
} }
try { try {

8
packages/server/src/workflows/logic.js

@ -1,12 +1,12 @@
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)) const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let LOGIC = { let LOGIC = {
DELAY: async function delay({ args }) { DELAY: async function delay(inputs) {
await wait(args.time) await wait(inputs.time)
}, },
FILTER: async function filter({ args }) { FILTER: async function filter(inputs) {
const { field, condition, value } = args const { field, condition, value } = inputs
switch (condition) { switch (condition) {
case "equals": case "equals":
if (field !== value) return if (field !== value) return

Loading…
Cancel
Save