1
0
Fork 0
mirror of synced 2024-06-29 11:31:06 +12:00
budibase/packages/server/src/automations/triggers.js

111 lines
3.3 KiB
JavaScript
Raw Normal View History

const CouchDB = require("../db")
const emitter = require("../events/index")
const env = require("../environment")
2021-05-06 04:49:53 +12:00
const Queue = env.isTest()
? require("../utilities/queue/inMemoryQueue")
: require("bull")
const { getAutomationParams } = require("../db/utils")
const { coerce } = require("../utilities/rowProcessor")
const { utils } = require("@budibase/auth/redis")
2021-05-19 03:37:54 +12:00
const { JobQueues } = require("../constants")
const { definitions } = require("./triggerInfo")
2021-09-08 00:58:53 +12:00
const { isDevAppID } = require("../db/utils")
// need this to call directly, so we can get a response
const { processEvent } = require("./utils")
const { opts } = utils.getRedisOptions()
2021-05-19 03:37:54 +12:00
let automationQueue = new Queue(JobQueues.AUTOMATIONS, { redis: opts })
2021-03-27 03:56:34 +13:00
const TRIGGER_DEFINITIONS = definitions
async function queueRelevantRowAutomations(event, eventType) {
if (event.appId == null) {
throw `No appId specified for ${eventType} - check event emitters.`
}
2021-09-08 00:58:53 +12:00
// don't queue events which are for dev apps, only way to test automations is
// running tests on them
if (isDevAppID(event.appId)) {
return
}
const db = new CouchDB(event.appId)
let automations = await db.allDocs(
getAutomationParams(null, { include_docs: true })
)
// filter down to the correct event type
automations = automations.rows
2021-05-04 22:32:22 +12:00
.map(automation => automation.doc)
.filter(automation => {
const trigger = automation.definition.trigger
return trigger && trigger.event === eventType
})
for (let automation of automations) {
let automationDef = automation.definition
let automationTrigger = automationDef ? automationDef.trigger : {}
if (
!automation.live ||
!automationTrigger.inputs ||
automationTrigger.inputs.tableId !== event.row.tableId
) {
continue
}
await automationQueue.add({ automation, event })
}
}
2021-05-03 19:31:09 +12:00
emitter.on("row:save", async function (event) {
/* istanbul ignore next */
if (!event || !event.row || !event.row.tableId) {
return
}
await queueRelevantRowAutomations(event, "row:save")
})
2021-05-03 19:31:09 +12:00
emitter.on("row:update", async function (event) {
/* istanbul ignore next */
if (!event || !event.row || !event.row.tableId) {
return
}
await queueRelevantRowAutomations(event, "row:update")
})
2021-05-03 19:31:09 +12:00
emitter.on("row:delete", async function (event) {
/* istanbul ignore next */
if (!event || !event.row || !event.row.tableId) {
return
}
await queueRelevantRowAutomations(event, "row:delete")
})
exports.externalTrigger = async function (
automation,
params,
{ getResponses } = {}
) {
if (automation.definition != null && automation.definition.trigger != null) {
if (automation.definition.trigger.stepId === "APP") {
// values are likely to be submitted as strings, so we shall convert to correct type
const coercedFields = {}
const fields = automation.definition.trigger.inputs.fields
for (let key of Object.keys(fields)) {
coercedFields[key] = coerce(params.fields[key], fields[key])
}
params.fields = coercedFields
}
}
const data = { automation, event: params }
if (getResponses) {
return processEvent({ data })
} else {
return automationQueue.add(data)
}
}
exports.getQueues = () => {
2021-05-08 00:55:57 +12:00
return [automationQueue]
}
exports.automationQueue = automationQueue
exports.TRIGGER_DEFINITIONS = TRIGGER_DEFINITIONS