diff --git a/packages/core/types/src/shared-context.ts b/packages/core/types/src/shared-context.ts index 035908b34e..d4000b4b79 100644 --- a/packages/core/types/src/shared-context.ts +++ b/packages/core/types/src/shared-context.ts @@ -21,6 +21,11 @@ export type SharedContext = { export interface MessageAggregatorFormat { groupBy?: string[] sortBy?: { [key: string]: string[] | string | number } + /** + * @internal + * will prevent the info log to be displayed about those events + */ + internal?: boolean } export interface IMessageAggregator { diff --git a/packages/core/utils/src/event-bus/message-aggregator.ts b/packages/core/utils/src/event-bus/message-aggregator.ts index 12740c70c3..466ce1e289 100644 --- a/packages/core/utils/src/event-bus/message-aggregator.ts +++ b/packages/core/utils/src/event-bus/message-aggregator.ts @@ -50,7 +50,7 @@ export class MessageAggregator implements IMessageAggregator { this.save(composedMessages) } - getMessages(format?: MessageAggregatorFormat): { + getMessages(format: MessageAggregatorFormat = {}): { [group: string]: Message[] } { const { groupBy, sortBy } = format ?? {} @@ -78,6 +78,15 @@ export class MessageAggregator implements IMessageAggregator { messages = groupedMessages } + if (format.internal) { + Object.values(messages).forEach((group) => { + group.forEach((msg) => { + msg.options = msg.options ?? {} + msg.options.internal = format.internal + }) + }) + } + return messages } diff --git a/packages/core/utils/src/modules-sdk/decorators/__tests__/emit-events.ts b/packages/core/utils/src/modules-sdk/decorators/__tests__/emit-events.ts new file mode 100644 index 0000000000..c393793c8e --- /dev/null +++ b/packages/core/utils/src/modules-sdk/decorators/__tests__/emit-events.ts @@ -0,0 +1,96 @@ +import { EmitEvents } from "../emit-events" +import { MedusaContext } from "../context-parameter" +import { Context } from "@medusajs/types" +import { InjectSharedContext } from "../inject-shared-context" + +describe("EmitEvents", () => { + it(`should call the emit event method from the base service including the messages and their options`, async () => { + const mock = jest.fn() + + class FakeService { + async emitEvents_(...args) { + return mock(...args) + } + + @InjectSharedContext() + @EmitEvents({ internal: true }) + async method(@MedusaContext() sharedContext: Context = {}) { + sharedContext.messageAggregator?.saveRawMessageData({ + data: { id: 1 }, + object: "test", + action: "create", + source: "test", + eventName: "test-event", + }) + } + } + + const service = new FakeService() + await service.method() + + expect(mock).toHaveBeenCalledTimes(1) + expect(mock).toHaveBeenCalledWith({ + default: [ + { + name: "test-event", + metadata: { + source: "test", + object: "test", + action: "create", + }, + data: { + id: 1, + }, + options: { + internal: true, + }, + }, + ], + }) + }) + + it(`should call the emit event method from the base service with grouped messages`, async () => { + const mock = jest.fn() + + class FakeService { + async emitEvents_(...args) { + return mock(...args) + } + + @InjectSharedContext() + @EmitEvents({ internal: true, groupBy: ["name"] }) + async method(@MedusaContext() sharedContext: Context = {}) { + sharedContext.messageAggregator?.saveRawMessageData({ + data: { id: 1 }, + object: "test", + action: "create", + source: "test", + eventName: "test-event", + }) + } + } + + const service = new FakeService() + await service.method() + + expect(mock).toHaveBeenCalledTimes(1) + expect(mock).toHaveBeenCalledWith({ + "test-event": [ + { + name: "test-event", + metadata: { + source: "test", + object: "test", + action: "create", + }, + data: { + id: 1, + }, + options: { + internal: true, + }, + }, + ], + }) + }) +}) diff --git a/packages/core/utils/src/modules-sdk/decorators/emit-events.ts b/packages/core/utils/src/modules-sdk/decorators/emit-events.ts index b8003c9f6d..ada510a4ed 100644 --- a/packages/core/utils/src/modules-sdk/decorators/emit-events.ts +++ b/packages/core/utils/src/modules-sdk/decorators/emit-events.ts @@ -2,6 +2,12 @@ import { MessageAggregator } from "../../event-bus" import { InjectIntoContext } from "./inject-into-context" import { MessageAggregatorFormat } from "@medusajs/types" +/** + * @internal this decorator is not meant to be used except by the internal team for now + * + * @param options + * @constructor + */ export function EmitEvents( options: MessageAggregatorFormat = {} as MessageAggregatorFormat ) { diff --git a/packages/core/utils/src/modules-sdk/medusa-service.ts b/packages/core/utils/src/modules-sdk/medusa-service.ts index 15e38d232c..499b209f71 100644 --- a/packages/core/utils/src/modules-sdk/medusa-service.ts +++ b/packages/core/utils/src/modules-sdk/medusa-service.ts @@ -11,11 +11,11 @@ import { SoftDeleteReturn, } from "@medusajs/types" import { - MapToConfig, isString, kebabCase, lowerCaseFirst, mapObjectTo, + MapToConfig, pluralize, upperCaseFirst, } from "../common" @@ -271,7 +271,10 @@ export function MedusaService< ? { id: primaryKeyValue } : primaryKeyValue, metadata: { source: "", action: "", object: "" }, - })) + })), + { + internal: true, + } ) } @@ -302,7 +305,10 @@ export function MedusaService< name: `${kebabCase(modelName)}.deleted`, metadata: { source: "", action: "", object: "" }, data: { id }, - })) + })), + { + internal: true, + } ) // Map internal table/column names to their respective external linkable keys @@ -392,6 +398,11 @@ export function MedusaService< ) } + /** + * @internal this method is not meant to be used except by the internal team for now + * @param groupedEvents + * @protected + */ protected async emitEvents_(groupedEvents) { if (!this.eventBusModuleService_ || !groupedEvents) { return @@ -399,7 +410,11 @@ export function MedusaService< const promises: Promise[] = [] for (const group of Object.keys(groupedEvents)) { - promises.push(this.eventBusModuleService_.emit(groupedEvents[group])) + promises.push( + this.eventBusModuleService_.emit(groupedEvents[group], { + internal: true, + }) + ) } await Promise.all(promises) diff --git a/packages/modules/event-bus-local/src/services/__tests__/event-bus-local.ts b/packages/modules/event-bus-local/src/services/__tests__/event-bus-local.ts index 71a89de7e1..671ad8267b 100644 --- a/packages/modules/event-bus-local/src/services/__tests__/event-bus-local.ts +++ b/packages/modules/event-bus-local/src/services/__tests__/event-bus-local.ts @@ -39,6 +39,49 @@ describe("LocalEventBusService", () => { data: { hi: "1234" }, name: "eventName", }) + + expect(loggerMock.info).toHaveBeenCalledTimes(1) + expect(loggerMock.info).toHaveBeenCalledWith( + "Processing eventName which has undefined subscribers" + ) + }) + + it("should emit an event but not log anything if it is internal", async () => { + eventEmitter.emit = jest.fn((data) => data) + + await eventBus.emit({ + name: "eventName", + data: { hi: "1234" }, + options: { + internal: true, + }, + }) + + expect(eventEmitter.emit).toHaveBeenCalledTimes(1) + expect(eventEmitter.emit).toHaveBeenCalledWith("eventName", { + data: { hi: "1234" }, + name: "eventName", + }) + + expect(loggerMock.info).toHaveBeenCalledTimes(0) + + await eventBus.emit( + { + name: "eventName", + data: { hi: "1234" }, + }, + { + internal: true, + } + ) + + expect(eventEmitter.emit).toHaveBeenCalledTimes(2) + expect(eventEmitter.emit).toHaveBeenCalledWith("eventName", { + data: { hi: "1234" }, + name: "eventName", + }) + + expect(loggerMock.info).toHaveBeenCalledTimes(0) }) it("should emit multiple events", async () => { @@ -87,7 +130,7 @@ describe("LocalEventBusService", () => { groupEventFn = jest.spyOn(eventBus, "groupEvent" as any) eventEmitter.emit = jest.fn((data) => data) - eventBus.emit([ + await eventBus.emit([ { name: "test-event", data: { test: "1234" }, @@ -201,12 +244,12 @@ describe("LocalEventBusService", () => { expect(getMap().get("group-1")).toHaveLength(1) expect(getMap().get("group-2")).toHaveLength(1) - eventBus.clearGroupedEvents("group-1") + await eventBus.clearGroupedEvents("group-1") expect(getMap().get("group-1")).not.toBeDefined() expect(getMap().get("group-2")).toHaveLength(1) - eventBus.clearGroupedEvents("group-2") + await eventBus.clearGroupedEvents("group-2") expect(getMap().get("group-2")).not.toBeDefined() }) diff --git a/packages/modules/event-bus-local/src/services/event-bus-local.ts b/packages/modules/event-bus-local/src/services/event-bus-local.ts index 1ed233824d..3be657f464 100644 --- a/packages/modules/event-bus-local/src/services/event-bus-local.ts +++ b/packages/modules/event-bus-local/src/services/event-bus-local.ts @@ -35,6 +35,12 @@ export default class LocalEventBusService extends AbstractEventBusModuleService this.groupedEventsMap_ = new Map() } + /** + * Accept an event name and some options + * + * @param eventsData + * @param options The options can include `internal` which will prevent the event from being logged + */ async emit( eventsData: Message | Message[], options: Record = {} @@ -48,9 +54,11 @@ export default class LocalEventBusService extends AbstractEventBusModuleService eventData.name ) - this.logger_?.info( - `Processing ${eventData.name} which has ${eventListenersCount} subscribers` - ) + if (!options.internal && !eventData.options?.internal) { + this.logger_?.info( + `Processing ${eventData.name} which has ${eventListenersCount} subscribers` + ) + } if (eventListenersCount === 0) { continue @@ -98,7 +106,7 @@ export default class LocalEventBusService extends AbstractEventBusModuleService this.eventEmitter_.emit(event.name, eventBody) } - this.clearGroupedEvents(eventGroupId) + await this.clearGroupedEvents(eventGroupId) } async clearGroupedEvents(eventGroupId: string) { diff --git a/packages/modules/event-bus-redis/src/services/__tests__/event-bus.ts b/packages/modules/event-bus-redis/src/services/__tests__/event-bus.ts index 04b504ba37..b9a0851cb9 100644 --- a/packages/modules/event-bus-redis/src/services/__tests__/event-bus.ts +++ b/packages/modules/event-bus-redis/src/services/__tests__/event-bus.ts @@ -307,6 +307,8 @@ describe("RedisEventBusService", () => { JSON.stringify(testGroup2Event2), ]) } + + return }) queue = (eventBus as any).queue_ @@ -337,7 +339,7 @@ describe("RedisEventBusService", () => { }) describe("worker_", () => { - let result + let result!: any describe("Successfully processes the jobs", () => { beforeEach(async () => { @@ -421,12 +423,16 @@ describe("RedisEventBusService", () => { }) it("should retry processing when subcribers fail, if configured - final attempt", async () => { - eventBus.subscribe("eventName", async () => Promise.resolve(), { + eventBus.subscribe("eventName", async () => await Promise.resolve(), { subscriberId: "1", }) - eventBus.subscribe("eventName", async () => Promise.reject("fail1"), { - subscriberId: "2", - }) + eventBus.subscribe( + "eventName", + async () => await Promise.reject("fail1"), + { + subscriberId: "2", + } + ) result = await eventBus .worker_({ @@ -456,12 +462,16 @@ describe("RedisEventBusService", () => { }) it("should retry processing when subcribers fail, if configured", async () => { - eventBus.subscribe("eventName", async () => Promise.resolve(), { + eventBus.subscribe("eventName", async () => await Promise.resolve(), { subscriberId: "1", }) - eventBus.subscribe("eventName", async () => Promise.reject("fail1"), { - subscriberId: "2", - }) + eventBus.subscribe( + "eventName", + async () => await Promise.reject("fail1"), + { + subscriberId: "2", + } + ) result = await eventBus .worker_({ diff --git a/packages/modules/event-bus-redis/src/services/event-bus-redis.ts b/packages/modules/event-bus-redis/src/services/event-bus-redis.ts index 76111b829f..8fc23acd62 100644 --- a/packages/modules/event-bus-redis/src/services/event-bus-redis.ts +++ b/packages/modules/event-bus-redis/src/services/event-bus-redis.ts @@ -11,7 +11,7 @@ import { } from "@medusajs/utils" import { BulkJobOptions, Queue, Worker } from "bullmq" import { Redis } from "ioredis" -import { BullJob, EventBusRedisModuleOptions } from "../types" +import { BullJob, EventBusRedisModuleOptions, Options } from "../types" type InjectedDependencies = { logger: Logger @@ -87,7 +87,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService private buildEvents( eventsData: Message[], - options: BulkJobOptions = {} + options: Options = {} ): IORedisEventType[] { const opts = { // default options @@ -127,7 +127,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService */ async emit( eventsData: Message | Message[], - options: BulkJobOptions & { groupedEventsTTL?: number } = {} + options: Options = {} ): Promise { let eventsDataArray = Array.isArray(eventsData) ? eventsData : [eventsData] @@ -169,7 +169,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService // This will be helpful in preventing stale data from staying in redis for too long // in the event the module fails to cleanup events. For long running workflows, setting a much higher // TTL or even skipping the TTL would be required - this.setExpire(groupId, groupedEventsTTL) + void this.setExpire(groupId, groupedEventsTTL) const eventsData = this.buildEvents(events, options) @@ -229,7 +229,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService * @return resolves to the results of the subscriber calls. */ worker_ = async (job: BullJob): Promise => { - const { data, name } = job + const { data, name, opts } = job const eventSubscribers = this.eventToSubscribersMap.get(name) || [] const wildcardSubscribers = this.eventToSubscribersMap.get("*") || [] @@ -250,18 +250,20 @@ export default class RedisEventBusService extends AbstractEventBusModuleService const isFinalAttempt = currentAttempt === configuredAttempts - if (isRetry) { - if (isFinalAttempt) { - this.logger_.info(`Final retry attempt for ${name}`) - } + if (!opts.internal) { + if (isRetry) { + if (isFinalAttempt) { + this.logger_.info(`Final retry attempt for ${name}`) + } - this.logger_.info( - `Retrying ${name} which has ${eventSubscribers.length} subscribers (${subscribersInCurrentAttempt.length} of them failed)` - ) - } else { - this.logger_.info( - `Processing ${name} which has ${eventSubscribers.length} subscribers` - ) + this.logger_.info( + `Retrying ${name} which has ${eventSubscribers.length} subscribers (${subscribersInCurrentAttempt.length} of them failed)` + ) + } else { + this.logger_.info( + `Processing ${name} which has ${eventSubscribers.length} subscribers` + ) + } } const completedSubscribersInCurrentAttempt: string[] = [] @@ -315,7 +317,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService this.logger_.warn(errorMessage) - return Promise.reject(Error(errorMessage)) + throw Error(errorMessage) } if (didSubscribersFail && !isFinalAttempt) { @@ -325,6 +327,6 @@ export default class RedisEventBusService extends AbstractEventBusModuleService ) } - return Promise.resolve(subscribersResult) + return subscribersResult } } diff --git a/packages/modules/event-bus-redis/src/types/index.ts b/packages/modules/event-bus-redis/src/types/index.ts index 9f7bdc8e5e..f2e75a7cf7 100644 --- a/packages/modules/event-bus-redis/src/types/index.ts +++ b/packages/modules/event-bus-redis/src/types/index.ts @@ -1,4 +1,10 @@ -import { Job, JobsOptions, QueueOptions, WorkerOptions } from "bullmq" +import { + BulkJobOptions, + Job, + JobsOptions, + QueueOptions, + WorkerOptions, +} from "bullmq" import { RedisOptions } from "ioredis" export type JobData = { @@ -7,8 +13,14 @@ export type JobData = { completedSubscriberIds?: string[] | undefined } +export type Options = BulkJobOptions & { + groupedEventsTTL?: number + internal?: boolean +} + export type BullJob = { data: JobData + opts: Job["opts"] & Options } & Job export type EmitOptions = JobsOptions diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment-set.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment-set.spec.ts index 1ff7f83dfc..b0aac84ada 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment-set.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment-set.spec.ts @@ -167,14 +167,19 @@ moduleIntegrationTestRunner({ }) ) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED, - action: "created", - object: "fulfillment_set", - data: { id: fulfillmentSet.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED, + action: "created", + object: "fulfillment_set", + data: { id: fulfillmentSet.id }, + }), + ], + { + internal: true, + } + ) }) it("should create a collection of fulfillment sets", async function () { @@ -211,7 +216,10 @@ moduleIntegrationTestRunner({ object: "fulfillment_set", data: { id: fulfillmentSets[i].id }, }), - ]) + ]), + { + internal: true, + } ) ++i @@ -245,20 +253,25 @@ moduleIntegrationTestRunner({ }) ) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED, - action: "created", - object: "fulfillment_set", - data: { id: fulfillmentSet.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SERVICE_ZONE_CREATED, - action: "created", - object: "service_zone", - data: { id: fulfillmentSet.service_zones[0].id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED, + action: "created", + object: "fulfillment_set", + data: { id: fulfillmentSet.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SERVICE_ZONE_CREATED, + action: "created", + object: "service_zone", + data: { id: fulfillmentSet.service_zones[0].id }, + }), + ], + { + internal: true, + } + ) }) it("should create a collection of fulfillment sets with new service zones", async function () { @@ -326,7 +339,10 @@ moduleIntegrationTestRunner({ object: "service_zone", data: { id: fulfillmentSets[i].service_zones[0].id }, }), - ]) + ]), + { + internal: true, + } ) ++i @@ -373,26 +389,31 @@ moduleIntegrationTestRunner({ }) ) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED, - action: "created", - object: "fulfillment_set", - data: { id: fulfillmentSet.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SERVICE_ZONE_CREATED, - action: "created", - object: "service_zone", - data: { id: fulfillmentSet.service_zones[0].id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.GEO_ZONE_CREATED, - action: "created", - object: "geo_zone", - data: { id: fulfillmentSet.service_zones[0].geo_zones[0].id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_SET_CREATED, + action: "created", + object: "fulfillment_set", + data: { id: fulfillmentSet.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SERVICE_ZONE_CREATED, + action: "created", + object: "service_zone", + data: { id: fulfillmentSet.service_zones[0].id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.GEO_ZONE_CREATED, + action: "created", + object: "geo_zone", + data: { id: fulfillmentSet.service_zones[0].geo_zones[0].id }, + }), + ], + { + internal: true, + } + ) }) it("should create a collection of fulfillment sets with new service zones and new geo zones", async function () { @@ -496,7 +517,10 @@ moduleIntegrationTestRunner({ id: fulfillmentSets[i].service_zones[0].geo_zones[0].id, }, }), - ]) + ]), + { + internal: true, + } ) ++i @@ -633,14 +657,19 @@ moduleIntegrationTestRunner({ }) ) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_SET_UPDATED, - action: "updated", - object: "fulfillment_set", - data: { id: updatedFulfillmentSets.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_SET_UPDATED, + action: "updated", + object: "fulfillment_set", + data: { id: updatedFulfillmentSets.id }, + }), + ], + { + internal: true, + } + ) }) it("should update a collection of fulfillment sets", async function () { @@ -698,7 +727,10 @@ moduleIntegrationTestRunner({ object: "fulfillment_set", data: { id: currentFullfillmentSet.id }, }), - ]) + ]), + { + internal: true, + } ) } }) @@ -816,7 +848,10 @@ moduleIntegrationTestRunner({ id: createdFulfillmentSet.service_zones[0].geo_zones[0].id, }, }), - ]) + ]), + { + internal: true, + } ) }) @@ -920,7 +955,10 @@ moduleIntegrationTestRunner({ id: createdServiceZone.geo_zones[0].id, }, }), - ]) + ]), + { + internal: true, + } ) }) @@ -1087,7 +1125,10 @@ moduleIntegrationTestRunner({ .id, }, }), - ]) + ]), + { + internal: true, + } ) } @@ -1230,7 +1271,10 @@ moduleIntegrationTestRunner({ id: createdServiceZone.geo_zones[0].id, }, }), - ]) + ]), + { + internal: true, + } ) } diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts index 4219b6aa07..c2075bfb7f 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/fulfillment.spec.ts @@ -153,32 +153,37 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_CREATED, - action: "created", - object: "fulfillment", - data: { id: fulfillment.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED, - action: "created", - object: "fulfillment_address", - data: { id: fulfillment.delivery_address.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_ITEM_CREATED, - action: "created", - object: "fulfillment_item", - data: { id: fulfillment.items[0].id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED, - action: "created", - object: "fulfillment_label", - data: { id: fulfillment.labels[0].id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_CREATED, + action: "created", + object: "fulfillment", + data: { id: fulfillment.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED, + action: "created", + object: "fulfillment_address", + data: { id: fulfillment.delivery_address.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_ITEM_CREATED, + action: "created", + object: "fulfillment_item", + data: { id: fulfillment.items[0].id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED, + action: "created", + object: "fulfillment_label", + data: { id: fulfillment.labels[0].id }, + }), + ], + { + internal: true, + } + ) }) it("should create a return fulfillment", async () => { @@ -240,32 +245,37 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_CREATED, - action: "created", - object: "fulfillment", - data: { id: fulfillment.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED, - action: "created", - object: "fulfillment_address", - data: { id: fulfillment.delivery_address.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_ITEM_CREATED, - action: "created", - object: "fulfillment_item", - data: { id: fulfillment.items[0].id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED, - action: "created", - object: "fulfillment_label", - data: { id: fulfillment.labels[0].id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_CREATED, + action: "created", + object: "fulfillment", + data: { id: fulfillment.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_ADDRESS_CREATED, + action: "created", + object: "fulfillment_address", + data: { id: fulfillment.delivery_address.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_ITEM_CREATED, + action: "created", + object: "fulfillment_item", + data: { id: fulfillment.items[0].id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED, + action: "created", + object: "fulfillment_label", + data: { id: fulfillment.labels[0].id }, + }), + ], + { + internal: true, + } + ) }) }) @@ -374,32 +384,37 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_UPDATED, - action: "updated", - object: "fulfillment", - data: { id: updatedFulfillment.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_LABEL_DELETED, - action: "deleted", - object: "fulfillment_label", - data: { id: label3.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_LABEL_UPDATED, - action: "updated", - object: "fulfillment_label", - data: { id: label2.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED, - action: "created", - object: "fulfillment_label", - data: { id: label4.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_UPDATED, + action: "updated", + object: "fulfillment", + data: { id: updatedFulfillment.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_LABEL_DELETED, + action: "deleted", + object: "fulfillment_label", + data: { id: label3.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_LABEL_UPDATED, + action: "updated", + object: "fulfillment_label", + data: { id: label2.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_LABEL_CREATED, + action: "created", + object: "fulfillment_label", + data: { id: label4.id }, + }), + ], + { + internal: true, + } + ) }) }) @@ -450,14 +465,20 @@ moduleIntegrationTestRunner({ expect(idempotentResult.canceled_at).toEqual(result.canceled_at) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenNthCalledWith(1, [ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.FULFILLMENT_UPDATED, - action: "updated", - object: "fulfillment", - data: { id: fulfillment.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenNthCalledWith( + 1, + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.FULFILLMENT_UPDATED, + action: "updated", + object: "fulfillment", + data: { id: fulfillment.id }, + }), + ], + { + internal: true, + } + ) }) it("should fail to cancel a fulfillment that is already shipped", async () => { diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/geo-zone.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/geo-zone.spec.ts index 243035ab8f..b52c861929 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/geo-zone.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/geo-zone.spec.ts @@ -107,14 +107,19 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.GEO_ZONE_CREATED, - action: "created", - object: "geo_zone", - data: { id: geoZone.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.GEO_ZONE_CREATED, + action: "created", + object: "geo_zone", + data: { id: geoZone.id }, + }), + ], + { + internal: true, + } + ) }) it("should create a collection of geo zones", async function () { @@ -165,7 +170,10 @@ moduleIntegrationTestRunner({ object: "geo_zone", data: { id: geoZones[i].id }, }), - ]) + ]), + { + internal: true, + } ) ++i @@ -265,14 +273,19 @@ moduleIntegrationTestRunner({ }) ) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.GEO_ZONE_UPDATED, - action: "updated", - object: "geo_zone", - data: { id: updatedGeoZone.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.GEO_ZONE_UPDATED, + action: "updated", + object: "geo_zone", + data: { id: updatedGeoZone.id }, + }), + ], + { + internal: true, + } + ) }) it("should update a collection of geo zones", async function () { @@ -337,7 +350,10 @@ moduleIntegrationTestRunner({ object: "geo_zone", data: { id: expectedGeoZone.id }, }), - ]) + ]), + { + internal: true, + } ) } }) diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/service-zone.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/service-zone.spec.ts index 52ff7790d5..a99fe8c58f 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/service-zone.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/service-zone.spec.ts @@ -366,32 +366,37 @@ moduleIntegrationTestRunner({ )! expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.GEO_ZONE_DELETED, - action: "deleted", - object: "geo_zone", - data: { id: ukGeoZone.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SERVICE_ZONE_UPDATED, - action: "updated", - object: "service_zone", - data: { id: updatedServiceZone.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.GEO_ZONE_CREATED, - action: "created", - object: "geo_zone", - data: { id: chGeoZone.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.GEO_ZONE_UPDATED, - action: "updated", - object: "geo_zone", - data: { id: usGeoZone.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.GEO_ZONE_DELETED, + action: "deleted", + object: "geo_zone", + data: { id: ukGeoZone.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SERVICE_ZONE_UPDATED, + action: "updated", + object: "service_zone", + data: { id: updatedServiceZone.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.GEO_ZONE_CREATED, + action: "created", + object: "geo_zone", + data: { id: chGeoZone.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.GEO_ZONE_UPDATED, + action: "updated", + object: "geo_zone", + data: { id: usGeoZone.id }, + }), + ], + { + internal: true, + } + ) }) it("should fail on duplicated service zone name", async function () { diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts index 812bdb3852..2dbdb21eb9 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-option.spec.ts @@ -517,26 +517,31 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(3) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SHIPPING_OPTION_CREATED, - action: "created", - object: "shipping_option", - data: { id: createdShippingOption.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED, - action: "created", - object: "shipping_option_type", - data: { id: createdShippingOption.type.id }, - }), - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED, - action: "created", - object: "shipping_option_rule", - data: { id: createdShippingOption.rules[0].id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SHIPPING_OPTION_CREATED, + action: "created", + object: "shipping_option", + data: { id: createdShippingOption.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED, + action: "created", + object: "shipping_option_type", + data: { id: createdShippingOption.type.id }, + }), + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED, + action: "created", + object: "shipping_option_rule", + data: { id: createdShippingOption.rules[0].id }, + }), + ], + { + internal: true, + } + ) }) it("should create multiple new shipping options", async function () { @@ -624,7 +629,10 @@ moduleIntegrationTestRunner({ object: "shipping_option_rule", data: { id: createdShippingOptions[i].rules[0].id }, }), - ]) + ]), + { + internal: true, + } ) ++i @@ -819,7 +827,10 @@ moduleIntegrationTestRunner({ object: "shipping_option_rule", data: { id: updatedShippingOption.rules[0].id }, }), - ]) + ]), + { + internal: true, + } ) }) @@ -1247,14 +1258,19 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED, - action: "created", - object: "shipping_option_rule", - data: { id: rule.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED, + action: "created", + object: "shipping_option_rule", + data: { id: rule.id }, + }), + ], + { + internal: true, + } + ) const rules = await service.listShippingOptionRules() expect(rules).toHaveLength(2) @@ -1324,14 +1340,19 @@ moduleIntegrationTestRunner({ }) ) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_UPDATED, - action: "updated", - object: "shipping_option_rule", - data: { id: updatedRule.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_UPDATED, + action: "updated", + object: "shipping_option_rule", + data: { id: updatedRule.id }, + }), + ], + { + internal: true, + } + ) }) it("should fail to update a non-existent shipping option rule", async () => { diff --git a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-profile.spec.ts b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-profile.spec.ts index 3ab8699046..5ec59c8949 100644 --- a/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-profile.spec.ts +++ b/packages/modules/fulfillment/integration-tests/__tests__/fulfillment-module-service/shipping-profile.spec.ts @@ -45,14 +45,19 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - buildExpectedEventMessageShape({ - eventName: FulfillmentEvents.SHIPPING_PROFILE_CREATED, - action: "created", - object: "shipping_profile", - data: { id: createdShippingProfile.id }, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + buildExpectedEventMessageShape({ + eventName: FulfillmentEvents.SHIPPING_PROFILE_CREATED, + action: "created", + object: "shipping_profile", + data: { id: createdShippingProfile.id }, + }), + ], + { + internal: true, + } + ) }) it("should create multiple new shipping profiles", async function () { @@ -90,7 +95,10 @@ moduleIntegrationTestRunner({ object: "shipping_profile", data: { id: createdShippingProfiles[i].id }, }), - ]) + ]), + { + internal: true, + } ) ++i diff --git a/packages/modules/fulfillment/src/services/fulfillment-module-service.ts b/packages/modules/fulfillment/src/services/fulfillment-module-service.ts index f4c0df2710..c2b9aca2c2 100644 --- a/packages/modules/fulfillment/src/services/fulfillment-module-service.ts +++ b/packages/modules/fulfillment/src/services/fulfillment-module-service.ts @@ -14,18 +14,18 @@ import { UpdateServiceZoneDTO, } from "@medusajs/types" import { - EmitEvents, - InjectManager, - InjectTransactionManager, - MedusaContext, - MedusaError, - ModulesSdkUtils, arrayDifference, deepEqualObj, + EmitEvents, getSetDifference, + InjectManager, + InjectTransactionManager, isDefined, isPresent, isString, + MedusaContext, + MedusaError, + ModulesSdkUtils, promiseAll, } from "@medusajs/utils" import { diff --git a/packages/modules/inventory-next/src/services/inventory-module.ts b/packages/modules/inventory-next/src/services/inventory-module.ts index 0d20aa0ce3..adce53b266 100644 --- a/packages/modules/inventory-next/src/services/inventory-module.ts +++ b/packages/modules/inventory-next/src/services/inventory-module.ts @@ -12,19 +12,19 @@ import { } from "@medusajs/types" import { IInventoryService } from "@medusajs/types/dist/inventory" import { + arrayDifference, BigNumber, CommonEvents, EmitEvents, InjectManager, InjectTransactionManager, InventoryEvents, + isDefined, + isString, MathBN, MedusaContext, MedusaError, MedusaService, - arrayDifference, - isDefined, - isString, partitionArray, } from "@medusajs/utils" import { InventoryItem, InventoryLevel, ReservationItem } from "@models" diff --git a/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts b/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts index c3b4573eaa..392ce19f06 100644 --- a/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts +++ b/packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts @@ -1,10 +1,10 @@ import { INotificationModuleService } from "@medusajs/types" import { CommonEvents, + composeMessage, Module, Modules, NotificationEvents, - composeMessage, } from "@medusajs/utils" import { MockEventBusService, @@ -94,14 +94,19 @@ moduleIntegrationTestRunner({ const result = await service.createNotifications(notification) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(NotificationEvents.NOTIFICATION_CREATED, { - data: { id: result.id }, - object: "notification", - source: Modules.NOTIFICATION, - action: CommonEvents.CREATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(NotificationEvents.NOTIFICATION_CREATED, { + data: { id: result.id }, + object: "notification", + source: Modules.NOTIFICATION, + action: CommonEvents.CREATED, + }), + ], + { + internal: true, + } + ) }) it("ensures the same notification is not sent twice", async () => { diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-categories.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-categories.spec.ts index 9bd489d68d..88e1e67382 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-categories.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-categories.spec.ts @@ -1,10 +1,10 @@ import { IProductModuleService } from "@medusajs/types" import { CommonEvents, + composeMessage, Modules, ProductEvents, ProductStatus, - composeMessage, } from "@medusajs/utils" import { Product, ProductCategory } from "@models" import { @@ -410,14 +410,19 @@ moduleIntegrationTestRunner({ }) expect(eventBusSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_CATEGORY_CREATED, { - data: { id: category.id }, - object: "product_category", - source: Modules.PRODUCT, - action: CommonEvents.CREATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_CATEGORY_CREATED, { + data: { id: category.id }, + object: "product_category", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), + ], + { + internal: true, + } + ) }) it("should append rank from an existing category depending on parent", async () => { @@ -505,14 +510,19 @@ moduleIntegrationTestRunner({ }) expect(eventBusSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_CATEGORY_UPDATED, { - data: { id: productCategoryZero.id }, - object: "product_category", - source: Modules.PRODUCT, - action: CommonEvents.UPDATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_CATEGORY_UPDATED, { + data: { id: productCategoryZero.id }, + object: "product_category", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), + ], + { + internal: true, + } + ) }) it("should update the name of the category successfully", async () => { @@ -678,17 +688,22 @@ moduleIntegrationTestRunner({ await service.deleteProductCategories([productCategoryOne.id]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - expect.objectContaining({ - data: { id: productCategoryOne.id }, - name: "product-category.deleted", - metadata: { - action: "", - object: "", - source: "", - }, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + expect.objectContaining({ + data: { id: productCategoryOne.id }, + name: "product-category.deleted", + metadata: { + action: "", + object: "", + source: "", + }, + }), + ], + { + internal: true, + } + ) }) it("should throw an error when an id does not exist", async () => { diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts index 632a573705..f0d4d38027 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-collections.spec.ts @@ -278,17 +278,22 @@ moduleIntegrationTestRunner({ await service.deleteProductCollections([collectionId]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - { - name: "product-collection.deleted", - data: { id: collectionId }, - metadata: { - action: "", - object: "", - source: "", + expect(eventBusSpy).toHaveBeenCalledWith( + [ + { + name: "product-collection.deleted", + data: { id: collectionId }, + metadata: { + action: "", + object: "", + source: "", + }, }, - }, - ]) + ], + { + internal: true, + } + ) }) }) @@ -306,12 +311,17 @@ moduleIntegrationTestRunner({ ]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ + expect(eventBusSpy).toHaveBeenCalledWith( + [ + { + data: { id: collectionId }, + name: "product-collection.updated", + }, + ], { - data: { id: collectionId }, - name: "product-collection.updated", - }, - ]) + internal: true, + } + ) }) it("should update the value of the collection successfully", async () => { @@ -502,12 +512,17 @@ moduleIntegrationTestRunner({ ]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ + expect(eventBusSpy).toHaveBeenCalledWith( + [ + { + data: { id: collections[0].id }, + name: "product-collection.created", + }, + ], { - data: { id: collections[0].id }, - name: "product-collection.created", - }, - ]) + internal: true, + } + ) }) }) }) diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts index 7654fd76db..5da8eae1ca 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-tags.spec.ts @@ -278,14 +278,19 @@ moduleIntegrationTestRunner({ expect(productTag.value).toEqual("UK") expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_TAG_UPDATED, { - data: { id: productTag.id }, - object: "product_tag", - source: Modules.PRODUCT, - action: CommonEvents.UPDATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_TAG_UPDATED, { + data: { id: productTag.id }, + object: "product_tag", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), + ], + { + internal: true, + } + ) }) it("should throw an error when an id does not exist", async () => { @@ -320,14 +325,19 @@ moduleIntegrationTestRunner({ expect(productTag[0]?.value).toEqual("UK") expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_TAG_CREATED, { - data: { id: productTag[0].id }, - object: "product_tag", - source: Modules.PRODUCT, - action: CommonEvents.CREATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_TAG_CREATED, { + data: { id: productTag[0].id }, + object: "product_tag", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), + ], + { + internal: true, + } + ) }) }) @@ -374,20 +384,25 @@ moduleIntegrationTestRunner({ const updatedTag = productTags.find((t) => t.value === "updated")! expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_TAG_CREATED, { - data: { id: newTag.id }, - object: "product_tag", - source: Modules.PRODUCT, - action: CommonEvents.CREATED, - }), - composeMessage(ProductEvents.PRODUCT_TAG_UPDATED, { - data: { id: updatedTag.id }, - object: "product_tag", - source: Modules.PRODUCT, - action: CommonEvents.UPDATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_TAG_CREATED, { + data: { id: newTag.id }, + object: "product_tag", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), + composeMessage(ProductEvents.PRODUCT_TAG_UPDATED, { + data: { id: updatedTag.id }, + object: "product_tag", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), + ], + { + internal: true, + } + ) }) }) }) diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts index c737657f4a..98a6d8ff3a 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/product-variants.spec.ts @@ -212,14 +212,19 @@ moduleIntegrationTestRunner({ expect(productVariant.title).toEqual("new test") expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_VARIANT_UPDATED, { - data: { id: variantOne.id }, - object: "product_variant", - source: Modules.PRODUCT, - action: CommonEvents.UPDATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_VARIANT_UPDATED, { + data: { id: variantOne.id }, + object: "product_variant", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), + ], + { + internal: true, + } + ) }) it("should upsert the options of a variant successfully", async () => { @@ -245,14 +250,19 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_VARIANT_UPDATED, { - data: { id: variantOne.id }, - object: "product_variant", - source: Modules.PRODUCT, - action: CommonEvents.UPDATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_VARIANT_UPDATED, { + data: { id: variantOne.id }, + object: "product_variant", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), + ], + { + internal: true, + } + ) }) it("should do a partial update on the options of a variant successfully", async () => { @@ -319,14 +329,19 @@ moduleIntegrationTestRunner({ ) expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1) - expect(eventBusEmitSpy).toHaveBeenCalledWith([ - composeMessage(ProductEvents.PRODUCT_VARIANT_CREATED, { - data: { id: variant.id }, - object: "product_variant", - source: Modules.PRODUCT, - action: CommonEvents.CREATED, - }), - ]) + expect(eventBusEmitSpy).toHaveBeenCalledWith( + [ + composeMessage(ProductEvents.PRODUCT_VARIANT_CREATED, { + data: { id: variant.id }, + object: "product_variant", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), + ], + { + internal: true, + } + ) }) it("should correctly associate variants with own product options", async () => { diff --git a/packages/modules/product/integration-tests/__tests__/product-module-service/products.spec.ts b/packages/modules/product/integration-tests/__tests__/product-module-service/products.spec.ts index 8b96064d45..3cad7e4af6 100644 --- a/packages/modules/product/integration-tests/__tests__/product-module-service/products.spec.ts +++ b/packages/modules/product/integration-tests/__tests__/product-module-service/products.spec.ts @@ -374,12 +374,17 @@ moduleIntegrationTestRunner({ await service.upsertProducts([updateData]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ + expect(eventBusSpy).toHaveBeenCalledWith( + [ + { + name: "product.updated", + data: { id: productOne.id }, + }, + ], { - name: "product.updated", - data: { id: productOne.id }, - }, - ]) + internal: true, + } + ) }) it("should add relationships to a product", async () => { @@ -724,12 +729,17 @@ moduleIntegrationTestRunner({ const products = await service.createProducts([data]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ + expect(eventBusSpy).toHaveBeenCalledWith( + [ + { + name: "product.created", + data: { id: products[0].id }, + }, + ], { - name: "product.created", - data: { id: products[0].id }, - }, - ]) + internal: true, + } + ) }) }) @@ -814,12 +824,17 @@ moduleIntegrationTestRunner({ await service.softDeleteProducts([products[0].id]) - expect(eventBusSpy).toHaveBeenCalledWith([ + expect(eventBusSpy).toHaveBeenCalledWith( + [ + { + name: "product.created", + data: { id: products[0].id }, + }, + ], { - name: "product.created", - data: { id: products[0].id }, - }, - ]) + internal: true, + } + ) }) }) diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 17bedb30b4..ce00df648f 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -8,10 +8,10 @@ import { ProductTypes, } from "@medusajs/types" import { + Image as ProductImage, Product, ProductCategory, ProductCollection, - Image as ProductImage, ProductOption, ProductOptionValue, ProductTag, @@ -875,7 +875,10 @@ export default class ProductModuleService collections.map(({ id }) => ({ name: ProductCollectionEvents.COLLECTION_CREATED, data: { id }, - })) + })), + { + internal: true, + } ) return Array.isArray(data) ? createdCollections : createdCollections[0] @@ -948,7 +951,10 @@ export default class ProductModuleService created.map(({ id }) => ({ name: ProductCollectionEvents.COLLECTION_CREATED, data: { id }, - })) + })), + { + internal: true, + } ) } @@ -957,7 +963,10 @@ export default class ProductModuleService updated.map(({ id }) => ({ name: ProductCollectionEvents.COLLECTION_UPDATED, data: { id }, - })) + })), + { + internal: true, + } ) } @@ -1018,7 +1027,10 @@ export default class ProductModuleService updatedCollections.map(({ id }) => ({ name: ProductCollectionEvents.COLLECTION_UPDATED, data: { id }, - })) + })), + { + internal: true, + } ) return isString(idOrSelector) ? updatedCollections[0] : updatedCollections @@ -1281,7 +1293,10 @@ export default class ProductModuleService createdProducts.map(({ id }) => ({ name: ProductEvents.PRODUCT_CREATED, data: { id }, - })) + })), + { + internal: true, + } ) return Array.isArray(data) ? createdProducts : createdProducts[0] @@ -1329,7 +1344,10 @@ export default class ProductModuleService created.map(({ id }) => ({ name: ProductEvents.PRODUCT_CREATED, data: { id }, - })) + })), + { + internal: true, + } ) } @@ -1338,7 +1356,10 @@ export default class ProductModuleService updated.map(({ id }) => ({ name: ProductEvents.PRODUCT_UPDATED, data: { id }, - })) + })), + { + internal: true, + } ) } @@ -1392,7 +1413,10 @@ export default class ProductModuleService updatedProducts.map(({ id }) => ({ name: ProductEvents.PRODUCT_UPDATED, data: { id }, - })) + })), + { + internal: true, + } ) return isString(idOrSelector) ? updatedProducts[0] : updatedProducts diff --git a/packages/modules/user/integration-tests/__tests__/invite.spec.ts b/packages/modules/user/integration-tests/__tests__/invite.spec.ts index ae780741a3..845b70b0a0 100644 --- a/packages/modules/user/integration-tests/__tests__/invite.spec.ts +++ b/packages/modules/user/integration-tests/__tests__/invite.spec.ts @@ -172,12 +172,17 @@ moduleIntegrationTestRunner({ ]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - expect.objectContaining({ - data: { id: "1" }, - name: UserEvents.INVITE_UPDATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + expect.objectContaining({ + data: { id: "1" }, + name: UserEvents.INVITE_UPDATED, + }), + ], + { + internal: true, + } + ) }) }) @@ -189,12 +194,17 @@ moduleIntegrationTestRunner({ await service.refreshInviteTokens(["1"]) expect(eventBusSpy).toHaveBeenCalledTimes(2) - expect(eventBusSpy).toHaveBeenCalledWith([ - expect.objectContaining({ - data: { id: "1" }, - name: UserEvents.INVITE_TOKEN_GENERATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + expect.objectContaining({ + data: { id: "1" }, + name: UserEvents.INVITE_TOKEN_GENERATED, + }), + ], + { + internal: true, + } + ) }) }) describe("createInvitie", () => { @@ -218,24 +228,29 @@ moduleIntegrationTestRunner({ await service.createInvites(defaultInviteData) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - expect.objectContaining({ - data: { id: "1" }, - name: UserEvents.INVITE_CREATED, - }), - expect.objectContaining({ - data: { id: "2" }, - name: UserEvents.INVITE_CREATED, - }), - expect.objectContaining({ - data: { id: "1" }, - name: UserEvents.INVITE_TOKEN_GENERATED, - }), - expect.objectContaining({ - data: { id: "2" }, - name: UserEvents.INVITE_TOKEN_GENERATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + expect.objectContaining({ + data: { id: "1" }, + name: UserEvents.INVITE_CREATED, + }), + expect.objectContaining({ + data: { id: "2" }, + name: UserEvents.INVITE_CREATED, + }), + expect.objectContaining({ + data: { id: "1" }, + name: UserEvents.INVITE_TOKEN_GENERATED, + }), + expect.objectContaining({ + data: { id: "2" }, + name: UserEvents.INVITE_TOKEN_GENERATED, + }), + ], + { + internal: true, + } + ) }) }) }) diff --git a/packages/modules/user/integration-tests/__tests__/user.spec.ts b/packages/modules/user/integration-tests/__tests__/user.spec.ts index 07eddfc09b..0268f35322 100644 --- a/packages/modules/user/integration-tests/__tests__/user.spec.ts +++ b/packages/modules/user/integration-tests/__tests__/user.spec.ts @@ -217,12 +217,17 @@ moduleIntegrationTestRunner({ ]) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - expect.objectContaining({ - data: { id: "1" }, - name: UserEvents.USER_UPDATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + expect.objectContaining({ + data: { id: "1" }, + name: UserEvents.USER_UPDATED, + }), + ], + { + internal: true, + } + ) }) }) @@ -247,16 +252,21 @@ moduleIntegrationTestRunner({ await service.createUsers(defaultUserData) expect(eventBusSpy).toHaveBeenCalledTimes(1) - expect(eventBusSpy).toHaveBeenCalledWith([ - expect.objectContaining({ - data: { id: "1" }, - name: UserEvents.USER_CREATED, - }), - expect.objectContaining({ - data: { id: "2" }, - name: UserEvents.USER_CREATED, - }), - ]) + expect(eventBusSpy).toHaveBeenCalledWith( + [ + expect.objectContaining({ + data: { id: "1" }, + name: UserEvents.USER_CREATED, + }), + expect.objectContaining({ + data: { id: "2" }, + name: UserEvents.USER_CREATED, + }), + ], + { + internal: true, + } + ) }) }) })