feat: restructure events payload (#8143)
* refactor: restructure events payload Breaking change: This PR changes the event payload accepted by the event listeners * refactor: fix failing tests and implement feedback * add integration tests * fix timeout --------- Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
50
integration-tests/modules/__tests__/event-bus/index.spec.ts
Normal file
50
integration-tests/modules/__tests__/event-bus/index.spec.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { MedusaContainer } from "@medusajs/types"
|
||||
import { composeMessage, ModuleRegistrationName } from "@medusajs/utils"
|
||||
import testEventPayloadHandlerMock from "../../dist/subscribers/test-event-payload"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
testSuite: ({ getContainer }) => {
|
||||
let container!: MedusaContainer
|
||||
|
||||
describe("EventBusModule", () => {
|
||||
beforeAll(() => {
|
||||
container = getContainer()
|
||||
})
|
||||
|
||||
it(`should emit event with the expected shape to be received by the subscribers`, async () => {
|
||||
const eventBus = container.resolve(ModuleRegistrationName.EVENT_BUS)
|
||||
const eventName = "test-event-payload"
|
||||
|
||||
await eventBus.emit(
|
||||
composeMessage(eventName, {
|
||||
data: {
|
||||
test: "foo",
|
||||
},
|
||||
object: "object",
|
||||
source: "source",
|
||||
action: "action",
|
||||
})
|
||||
)
|
||||
|
||||
expect(testEventPayloadHandlerMock).toHaveBeenCalled()
|
||||
expect(
|
||||
testEventPayloadHandlerMock.mock.calls[0][0].pluginOptions
|
||||
).toEqual({})
|
||||
expect(testEventPayloadHandlerMock.mock.calls[0][0].event).toEqual({
|
||||
name: eventName,
|
||||
data: {
|
||||
test: "foo",
|
||||
},
|
||||
metadata: {
|
||||
object: "object",
|
||||
source: "source",
|
||||
action: "action",
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -173,7 +173,7 @@ medusaIntegrationTestRunner({
|
||||
const logSpy = jest.spyOn(logger, "info")
|
||||
|
||||
await eventBus.emit({
|
||||
eventName: "order.created",
|
||||
name: "order.created",
|
||||
data: {
|
||||
order: {
|
||||
id: "1234",
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
"scripts": {
|
||||
"test:integration": "jest --silent=false --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
|
||||
"test:integration:chunk": "jest --silent --no-cache --bail --maxWorkers=50% --forceExit --testPathPattern=$(echo $CHUNKS | jq -r \".[${CHUNK}] | .[]\")",
|
||||
"build": "tsc ./src/* --allowJs --outDir ./dist"
|
||||
"build": "tsc --allowJs --outDir ./dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/api-key": "workspace:^",
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { SubscriberConfig } from "@medusajs/medusa/src"
|
||||
|
||||
const testEventPayloadHandlerMock = jest.fn()
|
||||
|
||||
export default testEventPayloadHandlerMock
|
||||
|
||||
export const config: SubscriberConfig = {
|
||||
event: "test-event-payload",
|
||||
}
|
||||
31
integration-tests/modules/tsconfig.json
Normal file
31
integration-tests/modules/tsconfig.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es5", "es6", "es2019"],
|
||||
"target": "es2022",
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"declarationMap": true,
|
||||
"declaration": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"sourceMap": true,
|
||||
"noImplicitReturns": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"downlevelIteration": true // to use ES5 specific tooling
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"./dist/**/*",
|
||||
"__tests__",
|
||||
"helpers",
|
||||
"./**/helpers",
|
||||
"./**/__snapshots__",
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
@@ -28,7 +28,7 @@ export const emitEventStep = createStep(
|
||||
const data_ =
|
||||
typeof input.data === "function" ? await input.data(context) : input.data
|
||||
|
||||
const metadata: EventBusTypes.MessageBody["metadata"] = {
|
||||
const metadata: EventBusTypes.Event["metadata"] = {
|
||||
...input.metadata,
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ export const emitEventStep = createStep(
|
||||
metadata.eventGroupId = context.eventGroupId
|
||||
}
|
||||
|
||||
const message = {
|
||||
eventName: input.eventName,
|
||||
const message: EventBusTypes.Message = {
|
||||
name: input.eventName,
|
||||
data: data_,
|
||||
options: input.options,
|
||||
metadata,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Context } from "../shared-context"
|
||||
|
||||
export type Subscriber<TData = unknown> = (
|
||||
data: MessageBody<TData>
|
||||
) => Promise<void>
|
||||
export type Subscriber<TData = unknown> = (data: Event<TData>) => Promise<void>
|
||||
|
||||
export type SubscriberContext = {
|
||||
subscriberId: string
|
||||
@@ -17,13 +15,13 @@ export type EventMetadata = Record<string, unknown> & {
|
||||
eventGroupId?: string
|
||||
}
|
||||
|
||||
export type MessageBody<TData = unknown> = {
|
||||
eventName: string
|
||||
export type Event<TData = unknown> = {
|
||||
name: string
|
||||
metadata?: EventMetadata
|
||||
data: TData
|
||||
}
|
||||
|
||||
export type Message<TData = unknown> = MessageBody<TData> & {
|
||||
export type Message<TData = unknown> = Event<TData> & {
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ export function composeMessage(
|
||||
throw new Error("Action is required if eventName is not a CommonEvent")
|
||||
}
|
||||
|
||||
const metadata: EventBusTypes.MessageBody["metadata"] = {
|
||||
const metadata: EventBusTypes.Event["metadata"] = {
|
||||
source,
|
||||
object,
|
||||
action: act!,
|
||||
@@ -44,7 +44,7 @@ export function composeMessage(
|
||||
}
|
||||
|
||||
return {
|
||||
eventName,
|
||||
name: eventName,
|
||||
metadata,
|
||||
data,
|
||||
options,
|
||||
|
||||
@@ -261,7 +261,7 @@ export function MedusaService<
|
||||
|
||||
await this.eventBusModuleService_?.emit(
|
||||
primaryKeyValues_.map((primaryKeyValue) => ({
|
||||
eventName: `${kebabCase(modelName)}.deleted`,
|
||||
name: `${kebabCase(modelName)}.deleted`,
|
||||
data: isString(primaryKeyValue)
|
||||
? { id: primaryKeyValue }
|
||||
: primaryKeyValue,
|
||||
@@ -294,7 +294,7 @@ export function MedusaService<
|
||||
|
||||
await this.eventBusModuleService_?.emit(
|
||||
softDeletedModels.map(({ id }) => ({
|
||||
eventName: `${kebabCase(modelName)}.deleted`,
|
||||
name: `${kebabCase(modelName)}.deleted`,
|
||||
metadata: { source: "", action: "", object: "" },
|
||||
data: { id },
|
||||
}))
|
||||
|
||||
@@ -21,7 +21,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
// we delay the processing of the event to avoid a conflict caused by a race condition
|
||||
await eventBus.emit(
|
||||
{
|
||||
eventName: PaymentWebhookEvents.WebhookReceived,
|
||||
name: PaymentWebhookEvents.WebhookReceived,
|
||||
data: event,
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
Event,
|
||||
IEventBusModuleService,
|
||||
MedusaContainer,
|
||||
MessageBody,
|
||||
Subscriber,
|
||||
} from "@medusajs/types"
|
||||
import { kebabCase, ModuleRegistrationName } from "@medusajs/utils"
|
||||
@@ -193,10 +193,9 @@ export class SubscriberLoader {
|
||||
const subscriberId = this.inferIdentifier(fileName, config, handler)
|
||||
|
||||
for (const e of events) {
|
||||
const subscriber = async (data: MessageBody<T>) => {
|
||||
const subscriber = async (data: T) => {
|
||||
return await handler({
|
||||
eventName: e,
|
||||
data,
|
||||
event: { name: e, ...data } as unknown as Event<T>,
|
||||
container: this.container_,
|
||||
pluginOptions: this.pluginOptions_,
|
||||
})
|
||||
|
||||
@@ -44,8 +44,7 @@ const configAsMap = handlerConfig.reduce(
|
||||
)
|
||||
|
||||
export default async function configurableNotifications({
|
||||
data,
|
||||
eventName,
|
||||
event,
|
||||
container,
|
||||
}: SubscriberArgs<any>) {
|
||||
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
|
||||
@@ -53,8 +52,8 @@ export default async function configurableNotifications({
|
||||
ModuleRegistrationName.NOTIFICATION
|
||||
)
|
||||
|
||||
const handlers = configAsMap[eventName] ?? []
|
||||
const payload = data.data
|
||||
const handlers = configAsMap[event.name] ?? []
|
||||
const payload = event.data
|
||||
|
||||
await promiseAll(
|
||||
handlers.map(async (handler) => {
|
||||
@@ -75,7 +74,7 @@ export default async function configurableNotifications({
|
||||
await notificationService.createNotifications(notificationData)
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`Failed to send notification for ${eventName}`,
|
||||
`Failed to send notification for ${event.name}`,
|
||||
err.message
|
||||
)
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ type SerializedBuffer = {
|
||||
}
|
||||
|
||||
export default async function paymentWebhookhandler({
|
||||
data,
|
||||
event,
|
||||
container,
|
||||
}: SubscriberArgs<ProviderWebhookPayload>) {
|
||||
const paymentService: IPaymentModuleService = container.resolve(
|
||||
ModuleRegistrationName.PAYMENT
|
||||
)
|
||||
|
||||
const input = "data" in data ? data.data : data
|
||||
const input = event.data
|
||||
|
||||
if (
|
||||
(input.payload.rawData as unknown as SerializedBuffer).type === "Buffer"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MedusaContainer, MessageBody } from "@medusajs/types"
|
||||
import { Event, MedusaContainer } from "@medusajs/types"
|
||||
|
||||
interface SubscriberContext extends Record<string, unknown> {
|
||||
subscriberId?: string
|
||||
@@ -10,8 +10,7 @@ export type SubscriberConfig = {
|
||||
}
|
||||
|
||||
export type SubscriberArgs<T = unknown> = {
|
||||
data: MessageBody<T>
|
||||
eventName: string
|
||||
event: Event<T>
|
||||
container: MedusaContainer
|
||||
pluginOptions: Record<string, unknown>
|
||||
}
|
||||
|
||||
@@ -30,14 +30,14 @@ describe("LocalEventBusService", () => {
|
||||
eventEmitter.emit = jest.fn((data) => data)
|
||||
|
||||
await eventBus.emit({
|
||||
eventName: "eventName",
|
||||
name: "eventName",
|
||||
data: { hi: "1234" },
|
||||
})
|
||||
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(1)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("eventName", {
|
||||
data: { hi: "1234" },
|
||||
eventName: "eventName",
|
||||
name: "eventName",
|
||||
})
|
||||
})
|
||||
|
||||
@@ -45,18 +45,18 @@ describe("LocalEventBusService", () => {
|
||||
eventEmitter.emit = jest.fn((data) => data)
|
||||
|
||||
await eventBus.emit([
|
||||
{ eventName: "event-1", data: { hi: "1234" } },
|
||||
{ eventName: "event-2", data: { hi: "5678" } },
|
||||
{ name: "event-1", data: { hi: "1234" } },
|
||||
{ name: "event-2", data: { hi: "5678" } },
|
||||
])
|
||||
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(2)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-1", {
|
||||
data: { hi: "1234" },
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
})
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-2", {
|
||||
data: { hi: "5678" },
|
||||
eventName: "event-2",
|
||||
name: "event-2",
|
||||
})
|
||||
})
|
||||
|
||||
@@ -65,7 +65,7 @@ describe("LocalEventBusService", () => {
|
||||
eventEmitter.emit = jest.fn((data) => data)
|
||||
|
||||
await eventBus.emit({
|
||||
eventName: "test-event",
|
||||
name: "test-event",
|
||||
data: {
|
||||
test: "1234",
|
||||
},
|
||||
@@ -79,7 +79,7 @@ describe("LocalEventBusService", () => {
|
||||
expect(groupEventFn).toHaveBeenCalledWith("test", {
|
||||
data: { test: "1234" },
|
||||
metadata: { eventGroupId: "test" },
|
||||
eventName: "test-event",
|
||||
name: "test-event",
|
||||
})
|
||||
|
||||
jest.clearAllMocks()
|
||||
@@ -89,12 +89,12 @@ describe("LocalEventBusService", () => {
|
||||
|
||||
eventBus.emit([
|
||||
{
|
||||
eventName: "test-event",
|
||||
name: "test-event",
|
||||
data: { test: "1234" },
|
||||
metadata: { eventGroupId: "test" },
|
||||
},
|
||||
{
|
||||
eventName: "test-event",
|
||||
name: "test-event",
|
||||
data: { test: "test-1" },
|
||||
},
|
||||
])
|
||||
@@ -102,18 +102,18 @@ describe("LocalEventBusService", () => {
|
||||
expect(groupEventFn).toHaveBeenCalledTimes(1)
|
||||
|
||||
expect((eventBus as any).groupedEventsMap_.get("test")).toEqual([
|
||||
expect.objectContaining({ eventName: "test-event" }),
|
||||
expect.objectContaining({ eventName: "test-event" }),
|
||||
expect.objectContaining({ name: "test-event" }),
|
||||
expect.objectContaining({ name: "test-event" }),
|
||||
])
|
||||
|
||||
await eventBus.emit({
|
||||
eventName: "test-event",
|
||||
name: "test-event",
|
||||
data: { test: "1234" },
|
||||
metadata: { eventGroupId: "test-2" },
|
||||
})
|
||||
|
||||
expect((eventBus as any).groupedEventsMap_.get("test-2")).toEqual([
|
||||
expect.objectContaining({ eventName: "test-event" }),
|
||||
expect.objectContaining({ name: "test-event" }),
|
||||
])
|
||||
})
|
||||
|
||||
@@ -122,32 +122,32 @@ describe("LocalEventBusService", () => {
|
||||
|
||||
await eventBus.emit([
|
||||
{
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
data: { test: "1" },
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
},
|
||||
{
|
||||
eventName: "event-2",
|
||||
name: "event-2",
|
||||
data: { test: "2" },
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
},
|
||||
{
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
data: { test: "1" },
|
||||
metadata: { eventGroupId: "group-2" },
|
||||
},
|
||||
{
|
||||
eventName: "event-2",
|
||||
name: "event-2",
|
||||
data: { test: "2" },
|
||||
metadata: { eventGroupId: "group-2" },
|
||||
},
|
||||
{ eventName: "event-1", data: { test: "1" } },
|
||||
{ name: "event-1", data: { test: "1" } },
|
||||
])
|
||||
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(1)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-1", {
|
||||
data: { test: "1" },
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
})
|
||||
|
||||
expect((eventBus as any).groupedEventsMap_.get("group-1")).toHaveLength(
|
||||
@@ -171,12 +171,12 @@ describe("LocalEventBusService", () => {
|
||||
expect(eventEmitter.emit).toHaveBeenCalledTimes(2)
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-1", {
|
||||
data: { test: "1" },
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
})
|
||||
expect(eventEmitter.emit).toHaveBeenCalledWith("event-2", {
|
||||
data: { test: "2" },
|
||||
eventName: "event-2",
|
||||
name: "event-2",
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
})
|
||||
})
|
||||
@@ -187,12 +187,12 @@ describe("LocalEventBusService", () => {
|
||||
|
||||
await eventBus.emit([
|
||||
{
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
data: { test: "1" },
|
||||
metadata: { eventGroupId: "group-1" },
|
||||
},
|
||||
{
|
||||
eventName: "event-1",
|
||||
name: "event-1",
|
||||
data: { test: "1" },
|
||||
metadata: { eventGroupId: "group-2" },
|
||||
},
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
EventBusTypes,
|
||||
Logger,
|
||||
Message,
|
||||
MessageBody,
|
||||
Event,
|
||||
Subscriber,
|
||||
} from "@medusajs/types"
|
||||
import { AbstractEventBusModuleService } from "@medusajs/utils"
|
||||
@@ -45,11 +45,11 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
|
||||
for (const eventData of normalizedEventsData) {
|
||||
const eventListenersCount = this.eventEmitter_.listenerCount(
|
||||
eventData.eventName
|
||||
eventData.name
|
||||
)
|
||||
|
||||
this.logger_?.info(
|
||||
`Processing ${eventData.eventName} which has ${eventListenersCount} subscribers`
|
||||
`Processing ${eventData.name} which has ${eventListenersCount} subscribers`
|
||||
)
|
||||
|
||||
if (eventListenersCount === 0) {
|
||||
@@ -73,7 +73,7 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
await this.groupEvent(eventGroupId, eventData)
|
||||
} else {
|
||||
const { options, ...eventBody } = eventData
|
||||
this.eventEmitter_.emit(eventData.eventName, eventBody)
|
||||
this.eventEmitter_.emit(eventData.name, eventBody)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
for (const event of groupedEvents) {
|
||||
const { options, ...eventBody } = event
|
||||
|
||||
this.eventEmitter_.emit(event.eventName, eventBody)
|
||||
this.eventEmitter_.emit(event.name, eventBody)
|
||||
}
|
||||
|
||||
this.clearGroupedEvents(eventGroupId)
|
||||
@@ -108,7 +108,7 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
|
||||
subscribe(event: string | symbol, subscriber: Subscriber): this {
|
||||
const randId = ulid()
|
||||
this.storeSubscribers({ event, subscriberId: randId, subscriber })
|
||||
this.eventEmitter_.on(event, async (data: MessageBody) => {
|
||||
this.eventEmitter_.on(event, async (data: Event) => {
|
||||
try {
|
||||
await subscriber(data)
|
||||
} catch (e) {
|
||||
|
||||
@@ -101,7 +101,7 @@ describe("RedisEventBusService", () => {
|
||||
it("should add job to queue with default options", async () => {
|
||||
await eventBus.emit([
|
||||
{
|
||||
eventName: "eventName",
|
||||
name: "eventName",
|
||||
data: {
|
||||
hi: "1234",
|
||||
},
|
||||
@@ -112,7 +112,7 @@ describe("RedisEventBusService", () => {
|
||||
expect(queue.addBulk).toHaveBeenCalledWith([
|
||||
{
|
||||
name: "eventName",
|
||||
data: { eventName: "eventName", data: { hi: "1234" } },
|
||||
data: { hi: "1234" },
|
||||
opts: {
|
||||
attempts: 1,
|
||||
removeOnComplete: true,
|
||||
@@ -122,16 +122,17 @@ describe("RedisEventBusService", () => {
|
||||
})
|
||||
|
||||
it("should add job to queue with custom options passed directly upon emitting", async () => {
|
||||
await eventBus.emit(
|
||||
[{ eventName: "eventName", data: { hi: "1234" } }],
|
||||
{ attempts: 3, backoff: 5000, delay: 1000 }
|
||||
)
|
||||
await eventBus.emit([{ name: "eventName", data: { hi: "1234" } }], {
|
||||
attempts: 3,
|
||||
backoff: 5000,
|
||||
delay: 1000,
|
||||
})
|
||||
|
||||
expect(queue.addBulk).toHaveBeenCalledTimes(1)
|
||||
expect(queue.addBulk).toHaveBeenCalledWith([
|
||||
{
|
||||
name: "eventName",
|
||||
data: { eventName: "eventName", data: { hi: "1234" } },
|
||||
data: { hi: "1234" },
|
||||
opts: {
|
||||
attempts: 3,
|
||||
backoff: 5000,
|
||||
@@ -164,7 +165,7 @@ describe("RedisEventBusService", () => {
|
||||
await eventBus.emit(
|
||||
[
|
||||
{
|
||||
eventName: "eventName",
|
||||
name: "eventName",
|
||||
data: { hi: "1234" },
|
||||
},
|
||||
],
|
||||
@@ -175,7 +176,7 @@ describe("RedisEventBusService", () => {
|
||||
expect(queue.addBulk).toHaveBeenCalledWith([
|
||||
{
|
||||
name: "eventName",
|
||||
data: { eventName: "eventName", data: { hi: "1234" } },
|
||||
data: { hi: "1234" },
|
||||
opts: {
|
||||
attempts: 3,
|
||||
backoff: 5000,
|
||||
@@ -208,7 +209,7 @@ describe("RedisEventBusService", () => {
|
||||
|
||||
await eventBus.emit(
|
||||
{
|
||||
eventName: "eventName",
|
||||
name: "eventName",
|
||||
data: { hi: "1234" },
|
||||
},
|
||||
{ delay: 1000 }
|
||||
@@ -218,7 +219,7 @@ describe("RedisEventBusService", () => {
|
||||
expect(queue.addBulk).toHaveBeenCalledWith([
|
||||
{
|
||||
name: "eventName",
|
||||
data: { eventName: "eventName", data: { hi: "1234" } },
|
||||
data: { hi: "1234" },
|
||||
opts: {
|
||||
attempts: 1,
|
||||
removeOnComplete: 5,
|
||||
@@ -231,7 +232,7 @@ describe("RedisEventBusService", () => {
|
||||
it("should successfully group events", async () => {
|
||||
const options = { delay: 1000 }
|
||||
const event = {
|
||||
eventName: "eventName",
|
||||
name: "eventName",
|
||||
data: { hi: "1234" },
|
||||
metadata: { eventGroupId: "test-group-1" },
|
||||
}
|
||||
@@ -252,21 +253,21 @@ describe("RedisEventBusService", () => {
|
||||
const options = { delay: 1000 }
|
||||
const events = [
|
||||
{
|
||||
eventName: "grouped-event-1",
|
||||
name: "grouped-event-1",
|
||||
data: { hi: "1234" },
|
||||
metadata: { eventGroupId: "test-group-1" },
|
||||
},
|
||||
{
|
||||
eventName: "ungrouped-event-2",
|
||||
name: "ungrouped-event-2",
|
||||
data: { hi: "1234" },
|
||||
},
|
||||
{
|
||||
eventName: "grouped-event-2",
|
||||
name: "grouped-event-2",
|
||||
data: { hi: "1234" },
|
||||
metadata: { eventGroupId: "test-group-2" },
|
||||
},
|
||||
{
|
||||
eventName: "grouped-event-3",
|
||||
name: "grouped-event-3",
|
||||
data: { hi: "1235" },
|
||||
metadata: { eventGroupId: "test-group-2" },
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { InternalModuleDeclaration } from "@medusajs/modules-sdk"
|
||||
import { Logger, Message, MessageBody } from "@medusajs/types"
|
||||
import { Logger, Message, Event } from "@medusajs/types"
|
||||
import {
|
||||
AbstractEventBusModuleService,
|
||||
isPresent,
|
||||
@@ -14,9 +14,7 @@ type InjectedDependencies = {
|
||||
eventBusRedisConnection: Redis
|
||||
}
|
||||
|
||||
type IORedisEventType<T = unknown> = {
|
||||
name: string
|
||||
data: MessageBody<T>
|
||||
type IORedisEventType<T = unknown> = Event<T> & {
|
||||
opts: BulkJobOptions
|
||||
}
|
||||
|
||||
@@ -98,8 +96,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
const { options, ...eventBody } = eventData
|
||||
|
||||
return {
|
||||
name: eventData.eventName,
|
||||
data: eventBody,
|
||||
...eventBody,
|
||||
opts: {
|
||||
// options for event group
|
||||
...opts,
|
||||
|
||||
@@ -9,7 +9,7 @@ export function buildExpectedEventMessageShape(options: {
|
||||
options?: Record<string, unknown>
|
||||
}): EventBusTypes.Message {
|
||||
return {
|
||||
eventName: options.eventName,
|
||||
name: options.eventName,
|
||||
metadata: {
|
||||
action: options.action,
|
||||
eventGroupId: options.eventGroupId,
|
||||
|
||||
@@ -207,7 +207,7 @@ export default class LinkModuleService<TLink> implements ILinkModule {
|
||||
|
||||
await this.eventBusModuleService_?.emit<Record<string, unknown>>(
|
||||
(data as { id: unknown }[]).map(({ id }) => ({
|
||||
eventName: this.entityName_ + "." + CommonEvents.ATTACHED,
|
||||
name: this.entityName_ + "." + CommonEvents.ATTACHED,
|
||||
metadata: {
|
||||
source: this.serviceName_,
|
||||
action: CommonEvents.ATTACHED,
|
||||
@@ -258,7 +258,7 @@ export default class LinkModuleService<TLink> implements ILinkModule {
|
||||
const allData = Array.isArray(data) ? data : [data]
|
||||
await this.eventBusModuleService_?.emit<Record<string, unknown>>(
|
||||
allData.map(({ id }) => ({
|
||||
eventName: this.entityName_ + "." + CommonEvents.DETACHED,
|
||||
name: this.entityName_ + "." + CommonEvents.DETACHED,
|
||||
metadata: {
|
||||
source: this.serviceName_,
|
||||
action: CommonEvents.DETACHED,
|
||||
@@ -307,7 +307,7 @@ export default class LinkModuleService<TLink> implements ILinkModule {
|
||||
|
||||
await this.eventBusModuleService_?.emit<Record<string, unknown>>(
|
||||
(deletedEntities as { id: string }[]).map(({ id }) => ({
|
||||
eventName: this.entityName_ + "." + CommonEvents.DETACHED,
|
||||
name: this.entityName_ + "." + CommonEvents.DETACHED,
|
||||
metadata: {
|
||||
source: this.serviceName_,
|
||||
action: CommonEvents.DETACHED,
|
||||
@@ -365,7 +365,7 @@ export default class LinkModuleService<TLink> implements ILinkModule {
|
||||
|
||||
await this.eventBusModuleService_?.emit<Record<string, unknown>>(
|
||||
(restoredEntities as { id: string }[]).map(({ id }) => ({
|
||||
eventName: this.entityName_ + "." + CommonEvents.ATTACHED,
|
||||
name: this.entityName_ + "." + CommonEvents.ATTACHED,
|
||||
metadata: {
|
||||
source: this.serviceName_,
|
||||
action: CommonEvents.ATTACHED,
|
||||
|
||||
@@ -681,7 +681,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
data: { id: productCategoryOne.id },
|
||||
eventName: "product-category.deleted",
|
||||
name: "product-category.deleted",
|
||||
metadata: {
|
||||
action: "",
|
||||
object: "",
|
||||
|
||||
@@ -280,7 +280,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product-collection.deleted",
|
||||
name: "product-collection.deleted",
|
||||
data: { id: collectionId },
|
||||
metadata: {
|
||||
action: "",
|
||||
@@ -309,7 +309,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
data: { id: collectionId },
|
||||
eventName: "product-collection.updated",
|
||||
name: "product-collection.updated",
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -505,7 +505,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
data: { id: collections[0].id },
|
||||
eventName: "product-collection.created",
|
||||
name: "product-collection.created",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
@@ -283,7 +283,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product.updated",
|
||||
name: "product.updated",
|
||||
data: { id: productOne.id },
|
||||
},
|
||||
])
|
||||
@@ -631,7 +631,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product.created",
|
||||
name: "product.created",
|
||||
data: { id: products[0].id },
|
||||
},
|
||||
])
|
||||
@@ -721,7 +721,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
{
|
||||
eventName: "product.created",
|
||||
name: "product.created",
|
||||
data: { id: products[0].id },
|
||||
},
|
||||
])
|
||||
|
||||
@@ -873,7 +873,7 @@ export default class ProductModuleService
|
||||
|
||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
||||
collections.map(({ id }) => ({
|
||||
eventName: ProductCollectionEvents.COLLECTION_CREATED,
|
||||
name: ProductCollectionEvents.COLLECTION_CREATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -946,7 +946,7 @@ export default class ProductModuleService
|
||||
if (created.length) {
|
||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
||||
created.map(({ id }) => ({
|
||||
eventName: ProductCollectionEvents.COLLECTION_CREATED,
|
||||
name: ProductCollectionEvents.COLLECTION_CREATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -955,7 +955,7 @@ export default class ProductModuleService
|
||||
if (updated.length) {
|
||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
||||
updated.map(({ id }) => ({
|
||||
eventName: ProductCollectionEvents.COLLECTION_UPDATED,
|
||||
name: ProductCollectionEvents.COLLECTION_UPDATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -1016,7 +1016,7 @@ export default class ProductModuleService
|
||||
|
||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
||||
updatedCollections.map(({ id }) => ({
|
||||
eventName: ProductCollectionEvents.COLLECTION_UPDATED,
|
||||
name: ProductCollectionEvents.COLLECTION_UPDATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -1279,7 +1279,7 @@ export default class ProductModuleService
|
||||
|
||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
||||
createdProducts.map(({ id }) => ({
|
||||
eventName: ProductEvents.PRODUCT_CREATED,
|
||||
name: ProductEvents.PRODUCT_CREATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -1327,7 +1327,7 @@ export default class ProductModuleService
|
||||
if (created.length) {
|
||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
||||
created.map(({ id }) => ({
|
||||
eventName: ProductEvents.PRODUCT_CREATED,
|
||||
name: ProductEvents.PRODUCT_CREATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -1336,7 +1336,7 @@ export default class ProductModuleService
|
||||
if (updated.length) {
|
||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
||||
updated.map(({ id }) => ({
|
||||
eventName: ProductEvents.PRODUCT_UPDATED,
|
||||
name: ProductEvents.PRODUCT_UPDATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
@@ -1390,7 +1390,7 @@ export default class ProductModuleService
|
||||
|
||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
||||
updatedProducts.map(({ id }) => ({
|
||||
eventName: ProductEvents.PRODUCT_UPDATED,
|
||||
name: ProductEvents.PRODUCT_UPDATED,
|
||||
data: { id },
|
||||
}))
|
||||
)
|
||||
|
||||
@@ -175,7 +175,7 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
eventName: UserEvents.INVITE_UPDATED,
|
||||
name: UserEvents.INVITE_UPDATED,
|
||||
}),
|
||||
])
|
||||
})
|
||||
@@ -192,7 +192,7 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
eventName: UserEvents.INVITE_TOKEN_GENERATED,
|
||||
name: UserEvents.INVITE_TOKEN_GENERATED,
|
||||
}),
|
||||
])
|
||||
})
|
||||
@@ -221,19 +221,19 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
eventName: UserEvents.INVITE_CREATED,
|
||||
name: UserEvents.INVITE_CREATED,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
eventName: UserEvents.INVITE_CREATED,
|
||||
name: UserEvents.INVITE_CREATED,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
eventName: UserEvents.INVITE_TOKEN_GENERATED,
|
||||
name: UserEvents.INVITE_TOKEN_GENERATED,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
eventName: UserEvents.INVITE_TOKEN_GENERATED,
|
||||
name: UserEvents.INVITE_TOKEN_GENERATED,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
@@ -220,7 +220,7 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
eventName: UserEvents.USER_UPDATED,
|
||||
name: UserEvents.USER_UPDATED,
|
||||
}),
|
||||
])
|
||||
})
|
||||
@@ -250,11 +250,11 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
expect(eventBusSpy).toHaveBeenCalledWith([
|
||||
expect.objectContaining({
|
||||
data: { id: "1" },
|
||||
eventName: UserEvents.USER_CREATED,
|
||||
name: UserEvents.USER_CREATED,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
data: { id: "2" },
|
||||
eventName: UserEvents.USER_CREATED,
|
||||
name: UserEvents.USER_CREATED,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user