From 4b06c17dc00dc9ed50898573aee704b84dd181b3 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Mon, 18 Mar 2024 21:07:04 +0100 Subject: [PATCH] fix(workflows-sdk): Value resolver should resolve non StepResponse (#6726) --- .changeset/silly-weeks-melt.md | 5 +++ .../__fixtures__/workflow_1.ts | 5 +-- .../__fixtures__/workflow_2.ts | 38 +++++++++---------- .../integration-tests/__tests__/index.spec.ts | 12 ++++++ .../utils/composer/helpers/resolve-value.ts | 5 ++- 5 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 .changeset/silly-weeks-melt.md diff --git a/.changeset/silly-weeks-melt.md b/.changeset/silly-weeks-melt.md new file mode 100644 index 0000000000000..e2e9eb4ee3625 --- /dev/null +++ b/.changeset/silly-weeks-melt.md @@ -0,0 +1,5 @@ +--- +"@medusajs/workflows-sdk": patch +--- + +fix(workflows-sdk): Value resolver should resolve non StepResponse diff --git a/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_1.ts b/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_1.ts index cb0056466e910..a8a5932585979 100644 --- a/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_1.ts +++ b/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_1.ts @@ -1,7 +1,7 @@ import { - StepResponse, createStep, createWorkflow, + StepResponse, } from "@medusajs/workflows-sdk" const step_1 = createStep( @@ -15,7 +15,6 @@ const step_1 = createStep( return } - console.log("reverted", compensateInput.compensate) return new StepResponse({ reverted: true, }) @@ -25,8 +24,6 @@ const step_1 = createStep( const step_2 = createStep( "step_2", jest.fn((input, context) => { - console.log("triggered async request", context.metadata.idempotency_key) - if (input) { return new StepResponse({ notAsyncResponse: input.hey }) } diff --git a/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_2.ts b/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_2.ts index f15d51889fe3e..5fd7f65fdccd7 100644 --- a/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_2.ts +++ b/packages/workflow-engine-inmemory/integration-tests/__fixtures__/workflow_2.ts @@ -1,7 +1,7 @@ import { - StepResponse, createStep, createWorkflow, + StepResponse, } from "@medusajs/workflows-sdk" const step_1 = createStep( @@ -15,22 +15,20 @@ const step_1 = createStep( return } - console.log("reverted", compensateInput.compensate) return new StepResponse({ reverted: true, }) }) ) +export const workflow2Step2Invoke = jest.fn((input, context) => { + if (input) { + return new StepResponse({ notAsyncResponse: input.hey }) + } +}) const step_2 = createStep( "step_2", - jest.fn((input, context) => { - console.log("triggered async request", context.metadata.idempotency_key) - - if (input) { - return new StepResponse({ notAsyncResponse: input.hey }) - } - }), + workflow2Step2Invoke, jest.fn((_, context) => { return new StepResponse({ step: context.metadata.action, @@ -40,16 +38,14 @@ const step_2 = createStep( }) ) -const step_3 = createStep( - "step_3", - jest.fn((res) => { - return new StepResponse({ - done: { - inputFromSyncStep: res.notAsyncResponse, - }, - }) +export const workflow2Step3Invoke = jest.fn((res) => { + return new StepResponse({ + done: { + inputFromSyncStep: res.notAsyncResponse, + }, }) -) +}) +const step_3 = createStep("step_3", workflow2Step3Invoke) createWorkflow( { @@ -59,13 +55,13 @@ createWorkflow( function (input) { step_1(input) - const ret2 = step_2({ hey: "oh" }) + step_2({ hey: "oh" }) - step_2({ hey: "async hello" }).config({ + const ret2_async = step_2({ hey: "async hello" }).config({ name: "new_step_name", async: true, }) - return step_3(ret2) + return step_3(ret2_async) } ) diff --git a/packages/workflow-engine-inmemory/integration-tests/__tests__/index.spec.ts b/packages/workflow-engine-inmemory/integration-tests/__tests__/index.spec.ts index c4e6c0a209587..191a2ffde17f2 100644 --- a/packages/workflow-engine-inmemory/integration-tests/__tests__/index.spec.ts +++ b/packages/workflow-engine-inmemory/integration-tests/__tests__/index.spec.ts @@ -6,6 +6,7 @@ import { knex } from "knex" import { setTimeout } from "timers/promises" import "../__fixtures__" import { DB_URL, TestDatabase } from "../utils" +import { workflow2Step2Invoke, workflow2Step3Invoke } from "../__fixtures__" const sharedPgConnection = knex({ client: "pg", @@ -124,6 +125,17 @@ describe("Workflow Orchestrator module", function () { stepResponse: { uhuuuu: "yeaah!" }, }) + expect(workflow2Step2Invoke).toBeCalledTimes(2) + expect(workflow2Step2Invoke.mock.calls[0][0]).toEqual({ hey: "oh" }) + expect(workflow2Step2Invoke.mock.calls[1][0]).toEqual({ + hey: "async hello", + }) + + expect(workflow2Step3Invoke).toBeCalledTimes(1) + expect(workflow2Step3Invoke.mock.calls[0][0]).toEqual({ + uhuuuu: "yeaah!", + }) + executionsList = await query({ workflow_executions: { fields: ["id"], diff --git a/packages/workflows-sdk/src/utils/composer/helpers/resolve-value.ts b/packages/workflows-sdk/src/utils/composer/helpers/resolve-value.ts index 73b5000de75e0..0984b2821a6bf 100644 --- a/packages/workflows-sdk/src/utils/composer/helpers/resolve-value.ts +++ b/packages/workflows-sdk/src/utils/composer/helpers/resolve-value.ts @@ -1,4 +1,4 @@ -import { OrchestrationUtils, deepCopy, promiseAll } from "@medusajs/utils" +import { deepCopy, OrchestrationUtils, promiseAll } from "@medusajs/utils" async function resolveProperty(property, transactionContext) { const { invoke: invokeRes } = transactionContext @@ -12,7 +12,8 @@ async function resolveProperty(property, transactionContext) { } else if (property?.__type === OrchestrationUtils.SymbolWorkflowHook) { return await property.__value(transactionContext) } else if (property?.__type === OrchestrationUtils.SymbolWorkflowStep) { - const output = invokeRes[property.__step__]?.output + const output = + invokeRes[property.__step__]?.output ?? invokeRes[property.__step__] if (output?.__type === OrchestrationUtils.SymbolWorkflowStepResponse) { return output.output }