From 19bbae61f8de1ac0ed574caff17b33e17705005a Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 17 Jan 2024 09:17:37 -0300 Subject: [PATCH] fix(modules-sdk): create workflow returning step transformer (#6102) --- .changeset/three-files-yawn.md | 5 + .../workflows/utils/composer/compose.ts | 106 ++++++++++++++++++ .../src/helper/workflow-export.ts | 33 +----- 3 files changed, 116 insertions(+), 28 deletions(-) create mode 100644 .changeset/three-files-yawn.md diff --git a/.changeset/three-files-yawn.md b/.changeset/three-files-yawn.md new file mode 100644 index 0000000000..d894fc6595 --- /dev/null +++ b/.changeset/three-files-yawn.md @@ -0,0 +1,5 @@ +--- +"@medusajs/workflows-sdk": patch +--- + +create workflow can return destructured properties of a step diff --git a/integration-tests/plugins/__tests__/workflows/utils/composer/compose.ts b/integration-tests/plugins/__tests__/workflows/utils/composer/compose.ts index 27fb772003..9fdedc22c2 100644 --- a/integration-tests/plugins/__tests__/workflows/utils/composer/compose.ts +++ b/integration-tests/plugins/__tests__/workflows/utils/composer/compose.ts @@ -1980,4 +1980,110 @@ describe("Workflow composer", function () { expect(mockStep1Fn.mock.calls[0][0]).toEqual(workflowInput) expect(mockCompensateSte1.mock.calls[0][0]).toEqual(undefined) }) + + it("should compose a workflow that returns destructured properties", async () => { + const step = function () { + return new StepResponse({ + propertyNotReturned: 1234, + property: { + complex: { + nested: 123, + }, + a: "bc", + }, + obj: "return from 2", + }) + } + + const step1 = createStep("step1", step) + + const workflow = createWorkflow("workflow1", function () { + const { property, obj } = step1() + + return { someOtherName: property, obj } + }) + + const { result } = await workflow().run({ + throwOnError: false, + }) + + expect(result).toEqual({ + someOtherName: { + complex: { + nested: 123, + }, + a: "bc", + }, + obj: "return from 2", + }) + }) + + it("should compose a workflow that returns an array of steps", async () => { + const step1 = createStep("step1", () => { + return new StepResponse({ + obj: "return from 1", + }) + }) + const step2 = createStep("step2", () => { + return new StepResponse({ + obj: "returned from 2**", + }) + }) + + const workflow = createWorkflow("workflow1", function () { + const s1 = step1() + const s2 = step2() + + return [s1, s2] + }) + + const { result } = await workflow().run({ + throwOnError: false, + }) + + expect(result).toEqual([ + { + obj: "return from 1", + }, + { + obj: "returned from 2**", + }, + ]) + }) + + it("should compose a workflow that returns an object mixed of steps and properties", async () => { + const step1 = createStep("step1", () => { + return new StepResponse({ + obj: { + nested: "nested", + }, + }) + }) + + const step2 = createStep("step2", () => { + return new StepResponse({ + obj: "returned from 2**", + }) + }) + + const workflow = createWorkflow("workflow1", function () { + const { obj } = step1() + const s2 = step2() + + return [{ step1_nested_obj: obj.nested }, s2] + }) + + const { result } = await workflow().run({ + throwOnError: false, + }) + + expect(result).toEqual([ + { + step1_nested_obj: "nested", + }, + { + obj: "returned from 2**", + }, + ]) + }) }) diff --git a/packages/workflows-sdk/src/helper/workflow-export.ts b/packages/workflows-sdk/src/helper/workflow-export.ts index 7a319cb156..e65e9452dd 100644 --- a/packages/workflows-sdk/src/helper/workflow-export.ts +++ b/packages/workflows-sdk/src/helper/workflow-export.ts @@ -9,7 +9,6 @@ import { import { Context, LoadedModule, MedusaContainer } from "@medusajs/types" import { MedusaModule } from "@medusajs/modules-sdk" -import { OrchestrationUtils } from "@medusajs/utils" import { EOL } from "os" import { ulid } from "ulid" import { MedusaWorkflow } from "../medusa-workflow" @@ -129,33 +128,11 @@ export const exportWorkflow = ( throw new Error(errorMessage) } - let result: any = undefined - - const resFrom = - resultFrom?.__type === OrchestrationUtils.SymbolWorkflowStep - ? resultFrom.__step__ - : resultFrom - - if (resFrom) { - if (Array.isArray(resFrom)) { - result = resFrom.map((from) => { - const res = transaction.getContext().invoke?.[from] - return res?.__type === OrchestrationUtils.SymbolWorkflowWorkflowData - ? res.output - : res - }) - } else { - const res = transaction.getContext().invoke?.[resFrom] - result = - res?.__type === OrchestrationUtils.SymbolWorkflowWorkflowData - ? res.output - : res - } - - const ret = result || resFrom - result = options?.wrappedInput - ? await resolveValue(ret, transaction.getContext()) - : ret + let result + if (options?.wrappedInput) { + result = await resolveValue(resultFrom, transaction.getContext()) + } else { + result = transaction.getContext().invoke?.[resultFrom] } return {