From b304d1d6226b2136b37e5c6c233a260725896d5f Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Mon, 14 Oct 2024 09:30:05 +0100 Subject: [PATCH 1/2] changes to support automation conditions on both sides of evaluation --- .../tests/scenarios/branching.spec.ts | 98 ++++++++++++++++++- .../tests/utilities/AutomationTestBuilder.ts | 6 +- packages/server/src/threads/automation.ts | 21 +++- 3 files changed, 114 insertions(+), 11 deletions(-) diff --git a/packages/server/src/automations/tests/scenarios/branching.spec.ts b/packages/server/src/automations/tests/scenarios/branching.spec.ts index 032b729e44..c05ec2f663 100644 --- a/packages/server/src/automations/tests/scenarios/branching.spec.ts +++ b/packages/server/src/automations/tests/scenarios/branching.spec.ts @@ -48,7 +48,9 @@ describe("Branching automations", () => { { stepId: branch1LogId } ), condition: { - equal: { [`{{ steps.${firstLogId}.success }}`]: true }, + equal: { + [`{{ literal steps.${firstLogId}.success }}`]: true, + }, }, }, branch2: { @@ -58,19 +60,21 @@ describe("Branching automations", () => { { stepId: branch2LogId } ), condition: { - equal: { [`{{ steps.${firstLogId}.success }}`]: false }, + equal: { + [`{{ literal steps.${firstLogId}.success }}`]: false, + }, }, }, }), condition: { - equal: { [`{{ steps.${firstLogId}.success }}`]: true }, + equal: { [`{{ literal steps.${firstLogId}.success }}`]: true }, }, }, topLevelBranch2: { steps: stepBuilder => stepBuilder.serverLog({ text: "Branch 2" }, { stepId: branch2Id }), condition: { - equal: { [`{{ steps.${firstLogId}.success }}`]: false }, + equal: { [`{{ literal steps.${firstLogId}.success }}`]: false }, }, }, }) @@ -217,4 +221,90 @@ describe("Branching automations", () => { ) expect(results.steps[2]).toBeUndefined() }) + + it("evaluate multiple conditions", async () => { + const builder = createAutomationBuilder({ + name: "evaluate multiple conditions", + }) + + const results = await builder + .appAction({ fields: { test_trigger: true } }) + .serverLog({ text: "Starting automation" }, { stepId: "aN6znRYHG" }) + .branch({ + specialBranch: { + steps: stepBuilder => stepBuilder.serverLog({ text: "Special user" }), + condition: { + $or: { + conditions: [ + { + equal: { + '{{ js "cmV0dXJuICQoInRyaWdnZXIuZmllbGRzLnRlc3RfdHJpZ2dlciIp" }}': + "{{ literal trigger.fields.test_trigger}}", + }, + }, + ], + }, + }, + }, + regularBranch: { + steps: stepBuilder => stepBuilder.serverLog({ text: "Regular user" }), + condition: { + $and: { + conditions: [ + { + equal: { "{{ literal trigger.fields.test_trigger}}": "blah" }, + }, + { + equal: { "{{ literal trigger.fields.test_trigger}}": "123" }, + }, + ], + }, + }, + }, + }) + .run() + + expect(results.steps[2].outputs.message).toContain("Special user") + }) + + it("evaluate multiple conditions with interpolated text", async () => { + const builder = createAutomationBuilder({ + name: "evaluate multiple conditions", + }) + + const results = await builder + .appAction({ fields: { test_trigger: true } }) + .serverLog({ text: "Starting automation" }, { stepId: "aN6znRYHG" }) + .branch({ + specialBranch: { + steps: stepBuilder => stepBuilder.serverLog({ text: "Special user" }), + condition: { + $or: { + conditions: [ + { + equal: { + "{{ trigger.fields.test_trigger }} 5": + "{{ trigger.fields.test_trigger }} 5", + }, + }, + ], + }, + }, + }, + regularBranch: { + steps: stepBuilder => stepBuilder.serverLog({ text: "Regular user" }), + condition: { + $and: { + conditions: [ + { equal: { "{{ trigger.fields.test_trigger }}": "blah" } }, + { equal: { "{{ trigger.fields.test_trigger }}": "123" } }, + ], + }, + }, + }, + }) + .run() + + expect(results.steps[2].outputs.message).toContain("Special user") + }) }) diff --git a/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts b/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts index 0c1e8cef4b..981932947a 100644 --- a/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts +++ b/packages/server/src/automations/tests/utilities/AutomationTestBuilder.ts @@ -88,13 +88,13 @@ class BaseStepBuilder { Object.entries(branchConfig).forEach(([key, branch]) => { const stepBuilder = new StepBuilder() branch.steps(stepBuilder) - + let branchId = uuidv4() branchStepInputs.branches.push({ name: key, condition: branch.condition, - id: uuidv4(), + id: branchId, }) - branchStepInputs.children![key] = stepBuilder.build() + branchStepInputs.children![branchId] = stepBuilder.build() }) const branchStep: AutomationStep = { ...definition, diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index 567b71d863..2d7769c3ea 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -30,7 +30,12 @@ import { import { AutomationContext, TriggerOutput } from "../definitions/automations" import { WorkerCallback } from "./definitions" import { context, logging } from "@budibase/backend-core" -import { processObject, processStringSync } from "@budibase/string-templates" +import { + findHBSBlocks, + isJSBinding, + processObject, + processStringSync, +} from "@budibase/string-templates" import { cloneDeep } from "lodash/fp" import { performance } from "perf_hooks" import * as sdkUtils from "../sdk/utils" @@ -535,13 +540,21 @@ class Orchestrator { conditions, filter => { Object.entries(filter).forEach(([_, value]) => { - Object.entries(value).forEach(([field, _]) => { - const updatedField = field.replace("{{", "{{ literal ") + Object.entries(value).forEach(([field, val]) => { const fromContext = processStringSync( - updatedField, + field, this.processContext(this.context) ) toFilter[field] = fromContext + + if (typeof val === "string" && findHBSBlocks(val).length > 0) { + const processedVal = processStringSync( + val, + this.processContext(this.context) + ) + + value[field] = processedVal + } }) }) return filter From 030022adadca71e293fa76522a3399f7561e94f5 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Mon, 14 Oct 2024 09:37:30 +0100 Subject: [PATCH 2/2] lint --- packages/server/src/threads/automation.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index 2d7769c3ea..4a8b18a733 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -32,7 +32,6 @@ import { WorkerCallback } from "./definitions" import { context, logging } from "@budibase/backend-core" import { findHBSBlocks, - isJSBinding, processObject, processStringSync, } from "@budibase/string-templates"