From f0ae4ad7cc03556ec3979a5a80641d44a4b788c9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 17 Sep 2020 15:14:08 +0100 Subject: [PATCH] Making the trigger endpoint detect if the trigger should have a model input and faking any properties that don't exist if required - this behaviour overridden by inputs from external client. --- .../src/api/routes/tests/workflow.spec.js | 4 +- packages/server/src/workflows/triggers.js | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/workflow.spec.js b/packages/server/src/api/routes/tests/workflow.spec.js index ce82cf8b13..8e0f79a9a6 100644 --- a/packages/server/src/api/routes/tests/workflow.spec.js +++ b/packages/server/src/api/routes/tests/workflow.spec.js @@ -119,6 +119,7 @@ describe("/workflows", () => { let saveAction = ACTION_DEFINITIONS["SAVE_RECORD"] saveAction.inputs.record = { name: "{{trigger.name}}", + description: "{{trigger.description}}" } saveAction.id = "awde444wk" @@ -160,7 +161,7 @@ describe("/workflows", () => { await createWorkflow() const res = await request .post(`/api/workflows/${workflow._id}/trigger`) - .send({ name: "Test", description: "Test" }) + .send({ name: "Test" }) .set(defaultHeaders(app._id, instance._id)) .expect('Content-Type', /json/) .expect(200) @@ -171,6 +172,7 @@ describe("/workflows", () => { let elements = await getAllFromModel(request, app._id, instance._id, model._id) expect(elements.length).toEqual(1) expect(elements[0].name).toEqual("Test") + expect(elements[0].description).toEqual("TEST") }) }) diff --git a/packages/server/src/workflows/triggers.js b/packages/server/src/workflows/triggers.js index a0f74fd756..e393be0e8c 100644 --- a/packages/server/src/workflows/triggers.js +++ b/packages/server/src/workflows/triggers.js @@ -4,6 +4,11 @@ const InMemoryQueue = require("./queue/inMemoryQueue") let workflowQueue = new InMemoryQueue() +const FAKE_STRING = "TEST" +const FAKE_BOOL = false +const FAKE_NUMBER = 1 +const FAKE_DATETIME = "1970-01-01T00:00:00.000Z" + const BUILTIN_DEFINITIONS = { RECORD_SAVED: { name: "Record Saved", @@ -98,7 +103,48 @@ emitter.on("record:delete", async function(event) { await queueRelevantWorkflows(event, "record:delete") }) +async function fillRecordOutput(workflow, params) { + let triggerSchema = workflow.definition.trigger + let modelId = triggerSchema.inputs.modelId + const db = new CouchDB(params.instanceId) + try { + let model = await db.get(modelId) + for (let schemaKey of Object.keys(model.schema)) { + if (params[schemaKey] != null) { + continue + } + let propSchema = model.schema[schemaKey] + switch (propSchema.constraints.type) { + case "string": + params[schemaKey] = FAKE_STRING + break + case "boolean": + params[schemaKey] = FAKE_BOOL + break + case "number": + params[schemaKey] = FAKE_NUMBER + break + case "datetime": + params[schemaKey] = FAKE_DATETIME + break + } + } + } catch (err) { + throw "Failed to find model for trigger" + } + return params +} + module.exports.externalTrigger = async function(workflow, params) { + // TODO: replace this with allowing user in builder to input values in future + if ( + workflow.definition != null && + workflow.definition.trigger != null && + workflow.definition.trigger.inputs.modelId != null + ) { + params = await fillRecordOutput(workflow, params) + } + workflowQueue.add({ workflow, event: params }) }