chore: local workflow proxying methods to pass context (#6263)

What:
- When calling a module's method inside a Local Workflow the MedusaContext is passed as the last argument to the method if not provided
- Add `requestId` to req
- A couple of fixes on Remote Joiner and the data fetcher for internal services

Why:
- The context used to initialize the workflow has to be shared with all modules. properties like transactionId will be used to emit events and requestId to trace logs for example.
This commit is contained in:
Carlos R. L. Rodrigues
2024-02-01 10:37:26 -03:00
committed by GitHub
parent a2bf6756ac
commit 45134e4d11
40 changed files with 576 additions and 170 deletions

View File

@@ -100,4 +100,98 @@ describe("POST /admin/campaigns", () => {
})
)
})
it("should create 3 campaigns in parallel and have the context passed as argument when calling createCampaigns with different transactionId", async () => {
const parallelPromotion = await promotionModuleService.create({
code: "PARALLEL",
type: "standard",
})
const spyCreateCampaigns = jest.spyOn(
promotionModuleService.constructor.prototype,
"createCampaigns"
)
const api = useApi() as any
const a = async () => {
return await api.post(
`/admin/campaigns`,
{
name: "camp_1",
campaign_identifier: "camp_1",
starts_at: new Date("01/01/2024").toISOString(),
ends_at: new Date("01/02/2024").toISOString(),
promotions: [{ id: parallelPromotion.id }],
budget: {
limit: 1000,
type: "usage",
},
},
adminHeaders
)
}
const b = async () => {
return await api.post(
`/admin/campaigns`,
{
name: "camp_2",
campaign_identifier: "camp_2",
starts_at: new Date("01/02/2024").toISOString(),
ends_at: new Date("01/03/2029").toISOString(),
promotions: [{ id: parallelPromotion.id }],
budget: {
limit: 500,
type: "usage",
},
},
adminHeaders
)
}
const c = async () => {
return await api.post(
`/admin/campaigns`,
{
name: "camp_3",
campaign_identifier: "camp_3",
starts_at: new Date("01/03/2024").toISOString(),
ends_at: new Date("01/04/2029").toISOString(),
promotions: [{ id: parallelPromotion.id }],
budget: {
limit: 250,
type: "usage",
},
},
{
headers: {
...adminHeaders.headers,
"x-request-id": "my-custom-request-id",
},
}
)
}
await Promise.all([a(), b(), c()])
expect(spyCreateCampaigns).toHaveBeenCalledTimes(3)
expect(spyCreateCampaigns.mock.calls[0][1].__type).toBe("MedusaContext")
const distinctTransactionId = [
...new Set(
spyCreateCampaigns.mock.calls.map((call) => call[1].transactionId)
),
]
expect(distinctTransactionId).toHaveLength(3)
const distinctRequestId = [
...new Set(
spyCreateCampaigns.mock.calls.map((call) => call[1].requestId)
),
]
expect(distinctRequestId).toHaveLength(3)
expect(distinctRequestId).toContain("my-custom-request-id")
})
})

View File

@@ -1,15 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IProductModuleService, WorkflowTypes } from "@medusajs/types"
import {
createProducts,
CreateProductsActions,
Handlers,
createProducts,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IProductModuleService, WorkflowTypes } from "@medusajs/types"
import { pipe } from "@medusajs/workflows-sdk"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { pipe } from "@medusajs/workflows-sdk"
jest.setTimeout(30000)
@@ -25,8 +25,6 @@ describe("CreateProduct workflow", function () {
})
afterAll(async () => {
console.log("GLOABL GC()", typeof global)
const db = useDb()
await db.shutdown()
await shutdownServer()