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.

This commit is contained in:
mike12345567 2020-09-17 15:14:08 +01:00
parent 99b1bbebbb
commit f0ae4ad7cc
2 changed files with 49 additions and 1 deletions

View File

@ -119,6 +119,7 @@ describe("/workflows", () => {
let saveAction = ACTION_DEFINITIONS["SAVE_RECORD"] let saveAction = ACTION_DEFINITIONS["SAVE_RECORD"]
saveAction.inputs.record = { saveAction.inputs.record = {
name: "{{trigger.name}}", name: "{{trigger.name}}",
description: "{{trigger.description}}"
} }
saveAction.id = "awde444wk" saveAction.id = "awde444wk"
@ -160,7 +161,7 @@ describe("/workflows", () => {
await createWorkflow() await createWorkflow()
const res = await request const res = await request
.post(`/api/workflows/${workflow._id}/trigger`) .post(`/api/workflows/${workflow._id}/trigger`)
.send({ name: "Test", description: "Test" }) .send({ name: "Test" })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -171,6 +172,7 @@ describe("/workflows", () => {
let elements = await getAllFromModel(request, app._id, instance._id, model._id) let elements = await getAllFromModel(request, app._id, instance._id, model._id)
expect(elements.length).toEqual(1) expect(elements.length).toEqual(1)
expect(elements[0].name).toEqual("Test") expect(elements[0].name).toEqual("Test")
expect(elements[0].description).toEqual("TEST")
}) })
}) })

View File

@ -4,6 +4,11 @@ const InMemoryQueue = require("./queue/inMemoryQueue")
let workflowQueue = new 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 = { const BUILTIN_DEFINITIONS = {
RECORD_SAVED: { RECORD_SAVED: {
name: "Record Saved", name: "Record Saved",
@ -98,7 +103,48 @@ emitter.on("record:delete", async function(event) {
await queueRelevantWorkflows(event, "record:delete") 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) { 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 }) workflowQueue.add({ workflow, event: params })
} }