2020-09-10 16:00:21 +02:00
|
|
|
const CouchDB = require("../db")
|
|
|
|
const emitter = require("../events/index")
|
|
|
|
const InMemoryQueue = require("./queue/inMemoryQueue")
|
|
|
|
|
|
|
|
let workflowQueue = new InMemoryQueue()
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
const BUILTIN_DEFINITIONS = {
|
|
|
|
RECORD_SAVED: {
|
|
|
|
name: "Record Saved",
|
|
|
|
event: "record:save",
|
|
|
|
icon: "ri-save-line",
|
|
|
|
tagline: "Record is added to {{inputs.enriched.model.name}}",
|
|
|
|
description: "Fired when a record is saved to your database",
|
2020-09-16 20:25:52 +02:00
|
|
|
stepId: "RECORD_SAVED",
|
2020-09-16 15:00:04 +02:00
|
|
|
inputs: {},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
modelId: {
|
|
|
|
type: "string",
|
|
|
|
customType: "model",
|
|
|
|
title: "Table",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["modelId"],
|
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
|
|
|
record: {
|
|
|
|
type: "object",
|
|
|
|
customType: "record",
|
|
|
|
description: "The new record that was saved",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["record"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
type: "TRIGGER",
|
|
|
|
},
|
|
|
|
RECORD_DELETED: {
|
|
|
|
name: "Record Deleted",
|
|
|
|
event: "record:delete",
|
|
|
|
icon: "ri-delete-bin-line",
|
|
|
|
tagline: "Record is deleted from {{inputs.enriched.model.name}}",
|
|
|
|
description: "Fired when a record is deleted from your database",
|
2020-09-16 20:25:52 +02:00
|
|
|
stepId: "RECORD_DELETED",
|
2020-09-16 15:00:04 +02:00
|
|
|
inputs: {},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
modelId: {
|
|
|
|
type: "string",
|
|
|
|
customType: "model",
|
|
|
|
title: "Table",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["modelId"],
|
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
|
|
|
record: {
|
|
|
|
type: "object",
|
|
|
|
customType: "record",
|
|
|
|
description: "The record that was deleted",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["record"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
type: "TRIGGER",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-10 16:00:21 +02:00
|
|
|
async function queueRelevantWorkflows(event, eventType) {
|
2020-09-14 11:30:35 +02:00
|
|
|
if (event.instanceId == null) {
|
|
|
|
throw `No instanceId specified for ${eventType} - check event emitters.`
|
|
|
|
}
|
2020-09-10 16:00:21 +02:00
|
|
|
const db = new CouchDB(event.instanceId)
|
|
|
|
const workflowsToTrigger = await db.query("database/by_workflow_trigger", {
|
|
|
|
key: [eventType],
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
const workflows = workflowsToTrigger.rows.map(wf => wf.doc)
|
|
|
|
for (let workflow of workflows) {
|
2020-09-11 19:47:22 +02:00
|
|
|
if (!workflow.live) {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-10 16:00:21 +02:00
|
|
|
workflowQueue.add({ workflow, event })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emitter.on("record:save", async function(event) {
|
|
|
|
await queueRelevantWorkflows(event, "record:save")
|
|
|
|
})
|
|
|
|
|
|
|
|
emitter.on("record:delete", async function(event) {
|
|
|
|
await queueRelevantWorkflows(event, "record:delete")
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports.externalTrigger = async function(workflow, params) {
|
|
|
|
workflowQueue.add({ workflow, event: params })
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.workflowQueue = workflowQueue
|
2020-09-16 15:00:04 +02:00
|
|
|
|
|
|
|
module.exports.BUILTIN_DEFINITIONS = BUILTIN_DEFINITIONS
|