2020-05-28 21:20:03 +02:00
|
|
|
const EventEmitter = require("events").EventEmitter
|
2020-05-31 18:12:52 +02:00
|
|
|
const CouchDB = require("../db");
|
2020-05-24 23:54:08 +02:00
|
|
|
|
2020-05-31 18:12:52 +02:00
|
|
|
const emitter = new EventEmitter()
|
|
|
|
|
|
|
|
function determineWorkflowsToTrigger(instanceId, event) {
|
|
|
|
const db = new CouchDB(instanceId);
|
|
|
|
const workflowsToTrigger = await db.query("database/by_workflow_trigger", {
|
|
|
|
key: [event]
|
|
|
|
})
|
|
|
|
|
|
|
|
return workflowsToTrigger.rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
emitter.on("record:save", async function(event) {
|
|
|
|
const workflowsToTrigger = await determineWorkflowsToTrigger(instanceId, "record:save")
|
|
|
|
|
|
|
|
for (let workflow of workflowsToTrigger) {
|
|
|
|
// SERVER SIDE STUFF!!
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
emitter.on("record:delete", function(event) {
|
|
|
|
const workflowsToTrigger = await determineWorkflowsToTrigger(instanceId, "record:delete")
|
|
|
|
|
|
|
|
for (let workflow of workflowsToTrigger) {
|
|
|
|
// SERVER SIDE STUFF!!
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = emitter
|