diff --git a/packages/core/workflows-sdk/src/helper/__tests__/compose.ts b/packages/core/workflows-sdk/src/helper/__tests__/compose.ts index a5f2e49baf..4506c0c163 100644 --- a/packages/core/workflows-sdk/src/helper/__tests__/compose.ts +++ b/packages/core/workflows-sdk/src/helper/__tests__/compose.ts @@ -24,6 +24,52 @@ describe("Workflow composer", function () { describe("Using steps returning plain values", function () { afterEach(afterEach_) + it("should compose a workflow and pass down the event group id provided as part of the context", async () => { + let context + const mockStep1Fn = jest + .fn() + .mockImplementation((input, { context: stepContext }) => { + context = stepContext + }) + + const step1 = createStep("step1", mockStep1Fn) + + const workflow = createWorkflow("workflow1", function (input) { + step1(input) + }) + + await workflow().run({ + context: { + eventGroupId: "event-group-id", + }, + }) + + expect(mockStep1Fn).toHaveBeenCalledTimes(1) + expect(mockStep1Fn.mock.calls[0]).toHaveLength(2) + expect(context.eventGroupId).toEqual("event-group-id") + }) + + it("should compose a workflow and pass down the autogenerated event group id if not provided as part of the context", async () => { + let context + const mockStep1Fn = jest + .fn() + .mockImplementation((input, { context: stepContext }) => { + context = stepContext + }) + + const step1 = createStep("step1", mockStep1Fn) + + const workflow = createWorkflow("workflow1", function (input) { + step1(input) + }) + + await workflow().run({}) + + expect(mockStep1Fn).toHaveBeenCalledTimes(1) + expect(mockStep1Fn.mock.calls[0]).toHaveLength(2) + expect(context.eventGroupId).toBeTruthy() + }) + it("should compose a new workflow composed retryable steps", async () => { const maxRetries = 1 diff --git a/packages/core/workflows-sdk/src/helper/workflow-export.ts b/packages/core/workflows-sdk/src/helper/workflow-export.ts index c5fa4dde67..4d0d2f21e6 100644 --- a/packages/core/workflows-sdk/src/helper/workflow-export.ts +++ b/packages/core/workflows-sdk/src/helper/workflow-export.ts @@ -5,7 +5,7 @@ import { TransactionState, } from "@medusajs/orchestration" import { LoadedModule, MedusaContainer } from "@medusajs/types" -import { MedusaContextType, isPresent } from "@medusajs/utils" +import { isPresent, MedusaContextType } from "@medusajs/utils" import { EOL } from "os" import { ulid } from "ulid" import { MedusaWorkflow } from "../medusa-workflow" @@ -140,6 +140,7 @@ function createContextualWorkflowRunner< } context.transactionId ??= ulid() + context.eventGroupId ??= ulid() if (typeof dataPreparation === "function") { try { @@ -193,6 +194,8 @@ function createContextualWorkflowRunner< __type: MedusaContextType, } + context.eventGroupId ??= ulid() + return await originalExecution( originalRegisterStepSuccess, { throwOnError, resultFrom, container }, @@ -229,6 +232,8 @@ function createContextualWorkflowRunner< __type: MedusaContextType, } + context.eventGroupId ??= ulid() + return await originalExecution( originalRegisterStepFailure, { throwOnError, resultFrom, container }, @@ -260,6 +265,8 @@ function createContextualWorkflowRunner< __type: MedusaContextType, } + context.eventGroupId ??= ulid() + return await originalExecution( originalCancel, {