diff --git a/packages/server/src/environment.js b/packages/server/src/environment.js index b3043fbfe0..b64ddfe465 100644 --- a/packages/server/src/environment.js +++ b/packages/server/src/environment.js @@ -8,4 +8,5 @@ module.exports = { SALT_ROUNDS: process.env.SALT_ROUNDS, LOGGER: process.env.LOGGER, BUDIBASE_ENVIRONMENT: process.env.BUDIBASE_ENVIRONMENT, + SENDGRID_API_KEY: process.env.SENDGRID_API_KEY, } diff --git a/packages/server/src/workflows/steps/sendEmail.js b/packages/server/src/workflows/steps/sendEmail.js index cb2ec007e3..b384b026fb 100644 --- a/packages/server/src/workflows/steps/sendEmail.js +++ b/packages/server/src/workflows/steps/sendEmail.js @@ -1,5 +1,6 @@ +const environment = require("../../environment") const sgMail = require("@sendgrid/mail") -sgMail.setApiKey(process.env.SENDGRID_API_KEY) +sgMail.setApiKey(environment.SENDGRID_API_KEY) module.exports.definition = { description: "Send an email", @@ -52,7 +53,7 @@ module.exports.run = async function({ inputs }) { to: inputs.to, from: inputs.from, subject: inputs.subject, - text: inputs.text, + text: inputs.text ? inputs.text : "Empty", } try { diff --git a/packages/server/src/workflows/triggers.js b/packages/server/src/workflows/triggers.js index e393be0e8c..7cc7a42c09 100644 --- a/packages/server/src/workflows/triggers.js +++ b/packages/server/src/workflows/triggers.js @@ -76,7 +76,7 @@ const BUILTIN_DEFINITIONS = { }, } -async function queueRelevantWorkflows(event, eventType) { +async function queueRelevantRecordWorkflows(event, eventType) { if (event.instanceId == null) { throw `No instanceId specified for ${eventType} - check event emitters.` } @@ -88,7 +88,13 @@ async function queueRelevantWorkflows(event, eventType) { const workflows = workflowsToTrigger.rows.map(wf => wf.doc) for (let workflow of workflows) { - if (!workflow.live) { + let workflowDef = workflow.definition + let workflowTrigger = workflowDef ? workflowDef.trigger : {} + if ( + !workflow.live || + !workflowTrigger.inputs || + workflowTrigger.inputs.modelId !== event.record.modelId + ) { continue } workflowQueue.add({ workflow, event }) @@ -96,11 +102,17 @@ async function queueRelevantWorkflows(event, eventType) { } emitter.on("record:save", async function(event) { - await queueRelevantWorkflows(event, "record:save") + if (!event || !event.record || !event.record.modelId) { + return + } + await queueRelevantRecordWorkflows(event, "record:save") }) emitter.on("record:delete", async function(event) { - await queueRelevantWorkflows(event, "record:delete") + if (!event || !event.record || !event.record.modelId) { + return + } + await queueRelevantRecordWorkflows(event, "record:delete") }) async function fillRecordOutput(workflow, params) {