From 6fa98b6a4d503b3110cfb0213fbe5ce4c3c6e6ad Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Fri, 18 Oct 2024 18:08:06 -0300 Subject: [PATCH] chore(orchestration): modules method context (#9669) ** What ** * Test to check if the MedusaContext is being injected when calling a Module's method --- .../__fixtures__/testing-module/index.ts | 11 +++ .../testing-module/models/dml-entity.ts | 6 ++ .../testing-module/models/index.ts | 1 + .../testing-module/services/module-service.ts | 17 ++++ .../workflow-engine/workflow-engine.ts | 93 +++++++++++++++++++ integration-tests/modules/medusa-config.js | 3 + .../src/workflow/local-workflow.ts | 5 +- 7 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 integration-tests/modules/__tests__/__fixtures__/testing-module/index.ts create mode 100644 integration-tests/modules/__tests__/__fixtures__/testing-module/models/dml-entity.ts create mode 100644 integration-tests/modules/__tests__/__fixtures__/testing-module/models/index.ts create mode 100644 integration-tests/modules/__tests__/__fixtures__/testing-module/services/module-service.ts create mode 100644 integration-tests/modules/__tests__/workflow-engine/workflow-engine.ts diff --git a/integration-tests/modules/__tests__/__fixtures__/testing-module/index.ts b/integration-tests/modules/__tests__/__fixtures__/testing-module/index.ts new file mode 100644 index 0000000000000..c965430a94dc9 --- /dev/null +++ b/integration-tests/modules/__tests__/__fixtures__/testing-module/index.ts @@ -0,0 +1,11 @@ +import { ModuleExports } from "@medusajs/types" +import { ModuleService } from "./services/module-service" + +const moduleExports: ModuleExports = { + service: ModuleService, +} + +export * from "./models" +export * from "./services/module-service" + +export default moduleExports diff --git a/integration-tests/modules/__tests__/__fixtures__/testing-module/models/dml-entity.ts b/integration-tests/modules/__tests__/__fixtures__/testing-module/models/dml-entity.ts new file mode 100644 index 0000000000000..29662827f75ca --- /dev/null +++ b/integration-tests/modules/__tests__/__fixtures__/testing-module/models/dml-entity.ts @@ -0,0 +1,6 @@ +import { model } from "@medusajs/utils" + +export const dmlEntity = model.define("dmlEntity", { + id: model.id().primaryKey(), + name: model.text(), +}) diff --git a/integration-tests/modules/__tests__/__fixtures__/testing-module/models/index.ts b/integration-tests/modules/__tests__/__fixtures__/testing-module/models/index.ts new file mode 100644 index 0000000000000..c68e1e718bc6e --- /dev/null +++ b/integration-tests/modules/__tests__/__fixtures__/testing-module/models/index.ts @@ -0,0 +1 @@ +export * from "./dml-entity" diff --git a/integration-tests/modules/__tests__/__fixtures__/testing-module/services/module-service.ts b/integration-tests/modules/__tests__/__fixtures__/testing-module/services/module-service.ts new file mode 100644 index 0000000000000..9b38a2757693e --- /dev/null +++ b/integration-tests/modules/__tests__/__fixtures__/testing-module/services/module-service.ts @@ -0,0 +1,17 @@ +import { IModuleService } from "@medusajs/types" +import { MedusaContext } from "@medusajs/utils" + +// @ts-expect-error +export class ModuleService implements IModuleService { + public property = "value" + public dynProperty + + constructor() { + this.dynProperty = { + key: "key value", + } + } + async methodName(input, @MedusaContext() context) { + return input + " called" + } +} diff --git a/integration-tests/modules/__tests__/workflow-engine/workflow-engine.ts b/integration-tests/modules/__tests__/workflow-engine/workflow-engine.ts new file mode 100644 index 0000000000000..d18b5ee3dcc67 --- /dev/null +++ b/integration-tests/modules/__tests__/workflow-engine/workflow-engine.ts @@ -0,0 +1,93 @@ +import { Modules } from "@medusajs/framework/utils" +import { + createStep, + createWorkflow, + StepResponse, + WorkflowData, + WorkflowResponse, +} from "@medusajs/framework/workflows-sdk" +import { medusaIntegrationTestRunner } from "@medusajs/test-utils" +import { + adminHeaders, + createAdminUser, +} from "../../../helpers/create-admin-user" + +jest.setTimeout(50000) + +medusaIntegrationTestRunner({ + testSuite: ({ dbConnection, getContainer, api }) => { + describe("Workflow Engine API", () => { + let medusaContainer + + beforeAll(() => { + medusaContainer = getContainer() + }) + + beforeEach(async () => { + await createAdminUser(dbConnection, adminHeaders, medusaContainer) + }) + + describe("Testing WorkflowEngine.run", () => { + beforeAll(async () => { + const step1 = createStep( + { + name: "my-step", + }, + async (input: { initial: string }, { container }) => { + const testMod = container.resolve("testingModule") as any + + return new StepResponse(testMod.methodName(input.initial)) + } + ) + + createWorkflow( + { + name: "my-workflow-name", + }, + function (input: WorkflowData<{ initial: string }>) { + const stepRes = step1(input) + + return new WorkflowResponse(stepRes) + } + ) + }) + + it("Should invoke modules passing the current medusa context as argument", async () => { + const testMod = medusaContainer.resolve("testingModule") as any + + const methodSpy = jest.spyOn(testMod, "methodName") + + const engine = medusaContainer.resolve(Modules.WORKFLOW_ENGINE) + + const res = await engine.run("my-workflow-name", { + transactionId: "trx-id", + input: { + initial: "abc", + }, + context: { + meta: { + myStuff: "myStuff", + }, + }, + }) + + expect(res.result).toEqual("abc called") + + expect(methodSpy).toHaveBeenCalledTimes(1) + expect(methodSpy).toHaveBeenCalledWith( + "abc", + expect.objectContaining({ + transactionId: "trx-id", + __type: "MedusaContext", + eventGroupId: expect.any(String), + idempotencyKey: "my-workflow-name:trx-id:my-step:invoke", + meta: { + myStuff: "myStuff", + }, + }) + ) + }) + }) + }) + }, +}) diff --git a/integration-tests/modules/medusa-config.js b/integration-tests/modules/medusa-config.js index bc20e5f1f0137..81ce1c7557bf1 100644 --- a/integration-tests/modules/medusa-config.js +++ b/integration-tests/modules/medusa-config.js @@ -39,6 +39,9 @@ module.exports = { medusa_v2: enableMedusaV2, }, modules: { + testingModule: { + resolve: "__tests__/__fixtures__/testing-module", + }, [Modules.AUTH]: { resolve: "@medusajs/auth", options: { diff --git a/packages/core/orchestration/src/workflow/local-workflow.ts b/packages/core/orchestration/src/workflow/local-workflow.ts index 5a03f96edab77..bad8a37fb2710 100644 --- a/packages/core/orchestration/src/workflow/local-workflow.ts +++ b/packages/core/orchestration/src/workflow/local-workflow.ts @@ -107,12 +107,9 @@ export class LocalWorkflow { return resolved } - const nonWrappable = Object.getOwnPropertyNames(resolved) return new Proxy(resolved, { get: function (target, prop) { - const shouldWrap = !nonWrappable.includes(prop as string) - - if (!shouldWrap) { + if (typeof target[prop] !== "function") { return target[prop] }