From a73418453874ce299e0cd951d90fcf1181f58383 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Wed, 18 Sep 2024 17:57:00 +0200 Subject: [PATCH] chore(): Handle medusa service base methods events (#9179) * chore(): Handle medusa service base methods events * cleanup * cleanup * fix import * fix decorator order * fixes * apply default event emition * fix binding * fix binding * align tests with new event emition --- .../utils/src/event-bus/message-aggregator.ts | 4 + packages/core/utils/src/event-bus/utils.ts | 30 +++- .../__tests__/medusa-service.spec.ts | 15 +- .../src/modules-sdk/decorators/emit-events.ts | 7 +- .../src/modules-sdk/event-builder-factory.ts | 33 +++- .../utils/src/modules-sdk/medusa-service.ts | 158 ++++++++++++++---- .../src/modules-sdk/types/medusa-service.ts | 25 +++ .../modules/fulfillment/src/utils/events.ts | 52 +++--- .../modules/notification/src/utils/events.ts | 4 +- packages/modules/pricing/src/utils/events.ts | 22 +-- .../product-categories.spec.ts | 8 +- .../product-collections.spec.ts | 10 +- packages/modules/product/src/utils/events.ts | 38 ++--- 13 files changed, 290 insertions(+), 116 deletions(-) diff --git a/packages/core/utils/src/event-bus/message-aggregator.ts b/packages/core/utils/src/event-bus/message-aggregator.ts index 466ce1e289..33badbb9d9 100644 --- a/packages/core/utils/src/event-bus/message-aggregator.ts +++ b/packages/core/utils/src/event-bus/message-aggregator.ts @@ -15,6 +15,10 @@ export class MessageAggregator implements IMessageAggregator { this.messages = [] } + count(): number { + return this.messages.length + } + save(msg: Message | Message[]): void { if (!msg || (Array.isArray(msg) && msg.length === 0)) { return diff --git a/packages/core/utils/src/event-bus/utils.ts b/packages/core/utils/src/event-bus/utils.ts index 1a7b145038..92b40046dd 100644 --- a/packages/core/utils/src/event-bus/utils.ts +++ b/packages/core/utils/src/event-bus/utils.ts @@ -28,6 +28,25 @@ type ReturnType = { >}_DETACHED`]: `${KebabCase}.detached` } +/** + * Build a conventional event name from the object name and the action and the prefix if provided + * @param prefix + * @param objectName + * @param action + */ +export function buildModuleResourceEventName({ + prefix, + objectName, + action, +}: { + prefix?: string + objectName: string + action: string +}): string { + const kebabCaseName = lowerCaseFirst(kebabCase(objectName)) + return `${prefix ? `${prefix}.` : ""}${kebabCaseName}.${action}` +} + /** * From the given strings it will produce the event names accordingly. * the result will look like: @@ -43,6 +62,7 @@ type ReturnType = { * } * * @param names + * @param prefix */ export function buildEventNamesFromEntityName( names: TNames, @@ -53,13 +73,15 @@ export function buildEventNamesFromEntityName( for (let i = 0; i < names.length; i++) { const name = names[i] const snakedCaseName = camelToSnakeCase(name).toUpperCase() - const kebabCaseName = lowerCaseFirst(kebabCase(name)) for (const event of Object.values(CommonEvents) as string[]) { const upperCasedEvent = event.toUpperCase() - events[`${snakedCaseName}_${upperCasedEvent}`] = `${ - prefix ? prefix + "." : "" - }${kebabCaseName}.${event}` as `${KebabCase}.${typeof event}` + events[`${snakedCaseName}_${upperCasedEvent}`] = + buildModuleResourceEventName({ + prefix, + objectName: name, + action: event, + }) as `${KebabCase}.${typeof event}` } } diff --git a/packages/core/utils/src/modules-sdk/__tests__/medusa-service.spec.ts b/packages/core/utils/src/modules-sdk/__tests__/medusa-service.spec.ts index 5a84b918a4..708489b7f9 100644 --- a/packages/core/utils/src/modules-sdk/__tests__/medusa-service.spec.ts +++ b/packages/core/utils/src/modules-sdk/__tests__/medusa-service.spec.ts @@ -1,5 +1,7 @@ import { MedusaService } from "../medusa-service" import { model } from "../../dml" +import { MessageAggregator } from "../../event-bus" +import { ModuleJoinerConfig } from "@medusajs/types" const baseRepoMock = { serialize: jest.fn().mockImplementation((item) => item), @@ -7,10 +9,15 @@ const baseRepoMock = { getFreshManager: jest.fn().mockReturnThis(), } -const defaultContext = { __type: "MedusaContext", manager: baseRepoMock } +const defaultContext = { + __type: "MedusaContext", + manager: baseRepoMock, + messageAggregator: new MessageAggregator(), +} const defaultTransactionContext = { __type: "MedusaContext", manager: baseRepoMock, + messageAggregator: new MessageAggregator(), } describe("Abstract Module Service Factory", () => { @@ -58,6 +65,9 @@ describe("Abstract Module Service Factory", () => { beforeEach(() => { jest.clearAllMocks() instance = new medusaService(containerMock) + ;(instance as any).__joinerConfig = { + serviceName: "serviceName", + } as ModuleJoinerConfig }) it("should have retrieve method", async () => { @@ -120,6 +130,9 @@ describe("Abstract Module Service Factory", () => { beforeEach(() => { jest.clearAllMocks() instance = new medusaService(containerMock) + ;(instance as any).__joinerConfig = { + serviceName: "serviceName", + } }) it("should have retrieve method for other models", async () => { 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 ada510a4ed..1327031b57 100644 --- a/packages/core/utils/src/modules-sdk/decorators/emit-events.ts +++ b/packages/core/utils/src/modules-sdk/decorators/emit-events.ts @@ -36,9 +36,12 @@ export function EmitEvents( const argIndex = target.MedusaContextIndex_[propertyKey] const aggregator = args[argIndex].messageAggregator as MessageAggregator - await target.emitEvents_.apply(this, [aggregator.getMessages(options)]) - aggregator.clearMessages() + if (aggregator.count() > 0) { + await target.emitEvents_.apply(this, [aggregator.getMessages(options)]) + aggregator.clearMessages() + } + return result } } diff --git a/packages/core/utils/src/modules-sdk/event-builder-factory.ts b/packages/core/utils/src/modules-sdk/event-builder-factory.ts index bed6a6edd9..82390fb3cd 100644 --- a/packages/core/utils/src/modules-sdk/event-builder-factory.ts +++ b/packages/core/utils/src/modules-sdk/event-builder-factory.ts @@ -1,13 +1,14 @@ import { Context, EventBusTypes } from "@medusajs/types" +import { buildModuleResourceEventName } from "../event-bus" -// TODO should that move closer to the event bus? and maybe be rename to moduleEventBuilderFactory +// TODO should that move closer to the event bus? and maybe be rename to modulemoduleEventBuilderFactory /** * * Factory function to create event builders for different entities * * @example - * const createdFulfillment = eventBuilderFactory({ + * const createdFulfillment = moduleEventBuilderFactory({ * source: Modules.FULFILLMENT, * action: CommonEvents.CREATED, * object: "fulfillment", @@ -24,24 +25,31 @@ import { Context, EventBusTypes } from "@medusajs/types" * @param eventsEnum * @param service */ -export function eventBuilderFactory({ +export function moduleEventBuilderFactory({ action, object, eventsEnum, + eventName, source, }: { action: string object: string - eventsEnum: Record + /** + * @deprecated use eventName instead + */ + eventsEnum?: Record + eventName?: string source: string }) { return function ({ data, sharedContext, }: { - data: { id: string }[] + data: { id: string } | { id: string }[] sharedContext: Context }) { + data = Array.isArray(data) ? data : [data] + if (!data.length) { return } @@ -51,8 +59,17 @@ export function eventBuilderFactory({ // The event enums contains event formatted like so [object]_[action] e.g. PRODUCT_CREATED // We expect the keys of events to be fully uppercased - const eventName = - eventsEnum[`${object.toUpperCase()}_${action.toUpperCase()}`] + let eventName_ = eventsEnum + ? eventsEnum[`${object.toUpperCase()}_${action.toUpperCase()}`] + : eventName + + if (!eventName_) { + eventName_ = buildModuleResourceEventName({ + prefix: source, + objectName: object, + action, + }) + } data.forEach((dataItem) => { messages.push({ @@ -60,7 +77,7 @@ export function eventBuilderFactory({ action, context: sharedContext, data: { id: dataItem.id }, - eventName, + eventName: eventName_, object, }) }) diff --git a/packages/core/utils/src/modules-sdk/medusa-service.ts b/packages/core/utils/src/modules-sdk/medusa-service.ts index fd28799145..6bc175e504 100644 --- a/packages/core/utils/src/modules-sdk/medusa-service.ts +++ b/packages/core/utils/src/modules-sdk/medusa-service.ts @@ -11,16 +11,16 @@ import { SoftDeleteReturn, } from "@medusajs/types" import { - MapToConfig, + camelToSnakeCase, isString, - kebabCase, lowerCaseFirst, mapObjectTo, + MapToConfig, pluralize, upperCaseFirst, } from "../common" import { DmlEntity } from "../dml" -import { InjectManager, MedusaContext } from "./decorators" +import { EmitEvents, InjectManager, MedusaContext } from "./decorators" import { Modules } from "./definition" import { buildModelsNameToLinkableKeysMap } from "./joiner-config-builder" import { @@ -31,6 +31,8 @@ import { ModelEntries, ModelsConfigTemplate, } from "./types/medusa-service" +import { CommonEvents } from "../event-bus" +import { moduleEventBuilderFactory } from "./event-builder-factory" const readMethods = ["retrieve", "list", "listAndCount"] as BaseMethods[] const writeMethods = [ @@ -150,13 +152,10 @@ export function MedusaService< value: klassPrototype[methodName], } + // The order of the decorators is important, do not change it MedusaContext()(klassPrototype, methodName, contextIndex) - - InjectManager("baseRepository_")( - klassPrototype, - methodName, - descriptorMockRef - ) + EmitEvents()(klassPrototype, methodName, descriptorMockRef) + InjectManager()(klassPrototype, methodName, descriptorMockRef) klassPrototype[methodName] = descriptorMockRef.value } @@ -194,6 +193,13 @@ export function MedusaService< const models = await service.create(serviceData, sharedContext) const response = Array.isArray(data) ? models : models[0] + klassPrototype.aggregatedEvents.bind(this)({ + action: CommonEvents.CREATED, + object: camelToSnakeCase(modelName).toLowerCase(), + data: response, + context: sharedContext, + }) + return await this.baseRepository_.serialize(response) } @@ -211,6 +217,13 @@ export function MedusaService< const models = await service.update(serviceData, sharedContext) const response = Array.isArray(data) ? models : models[0] + klassPrototype.aggregatedEvents.bind(this)({ + action: CommonEvents.UPDATED, + object: camelToSnakeCase(modelName).toLowerCase(), + data: response, + context: sharedContext, + }) + return await this.baseRepository_.serialize(response) } @@ -259,22 +272,21 @@ export function MedusaService< const primaryKeyValues_ = Array.isArray(primaryKeyValues) ? primaryKeyValues : [primaryKeyValues] + await this.__container__[serviceRegistrationName].delete( primaryKeyValues_, sharedContext ) - await this.eventBusModuleService_?.emit( - primaryKeyValues_.map((primaryKeyValue) => ({ - name: `${kebabCase(modelName)}.deleted`, + primaryKeyValues_.map((primaryKeyValue) => + klassPrototype.aggregatedEvents.bind(this)({ + action: CommonEvents.DELETED, + object: camelToSnakeCase(modelName).toLowerCase(), data: isString(primaryKeyValue) ? { id: primaryKeyValue } : primaryKeyValue, - metadata: { source: "", action: "", object: "" }, - })), - { - internal: true, - } + context: sharedContext, + }) ) } @@ -292,25 +304,10 @@ export function MedusaService< ? primaryKeyValues : [primaryKeyValues] - const [models, cascadedModelsMap] = await this.__container__[ + const [, cascadedModelsMap] = await this.__container__[ serviceRegistrationName ].softDelete(primaryKeyValues_, sharedContext) - const softDeletedModels = await this.baseRepository_.serialize( - models - ) - - await this.eventBusModuleService_?.emit( - softDeletedModels.map(({ id }) => ({ - name: `${kebabCase(modelName)}.deleted`, - metadata: { source: "", action: "", object: "" }, - data: { id }, - })), - { - internal: true, - } - ) - // Map internal table/column names to their respective external linkable keys // eg: product.id = product_id, variant.id = variant_id const mappedCascadedModelsMap = mapObjectTo( @@ -321,6 +318,31 @@ export function MedusaService< } ) + if (mappedCascadedModelsMap) { + const joinerConfig = ( + typeof this.__joinerConfig === "function" + ? this.__joinerConfig() + : this.__joinerConfig + ) as ModuleJoinerConfig + + Object.entries(mappedCascadedModelsMap).forEach( + ([linkableKey, ids]) => { + const entity = joinerConfig.linkableKeys?.[linkableKey]! + if (entity) { + const linkableKeyEntity = + camelToSnakeCase(entity).toLowerCase() + + klassPrototype.aggregatedEvents.bind(this)({ + action: CommonEvents.DELETED, + object: linkableKeyEntity, + data: { id: ids }, + context: sharedContext, + }) + } + } + ) + } + return mappedCascadedModelsMap ? mappedCascadedModelsMap : void 0 } @@ -338,7 +360,7 @@ export function MedusaService< ? primaryKeyValues : [primaryKeyValues] - const [_, cascadedModelsMap] = await this.__container__[ + const [, cascadedModelsMap] = await this.__container__[ serviceRegistrationName ].restore(primaryKeyValues_, sharedContext) @@ -353,6 +375,30 @@ export function MedusaService< } ) + if (mappedCascadedModelsMap) { + const joinerConfig = ( + typeof this.__joinerConfig === "function" + ? this.__joinerConfig() + : this.__joinerConfig + ) as ModuleJoinerConfig + + Object.entries(mappedCascadedModelsMap).forEach( + ([linkableKey, ids]) => { + const entity = joinerConfig.linkableKeys?.[linkableKey]! + if (entity) { + const linkableKeyEntity = + camelToSnakeCase(entity).toLowerCase() + klassPrototype.aggregatedEvents.bind(this)({ + action: CommonEvents.CREATED, + object: linkableKeyEntity, + data: { id: ids }, + context: sharedContext, + }) + } + } + ) + } + return mappedCascadedModelsMap ? mappedCascadedModelsMap : void 0 } @@ -398,6 +444,50 @@ export function MedusaService< ) } + /** + * helper function to aggregate events. Will format the message properly and store in + * the message aggregator from the context. The method must be decorated with `@EmitEvents` + * @param action + * @param object + * @param eventName optional, can be inferred from the module joiner config + action + object + * @param source optional, can be inferred from the module joiner config + * @param data + * @param context + */ + protected aggregatedEvents({ + action, + object, + eventName, + source, + data, + context, + }: { + action: string + object: string + eventName?: string + source?: string + data: { id: any } | { id: any }[] + context: Context + }) { + const __joinerConfig = ( + typeof this.__joinerConfig === "function" + ? this.__joinerConfig() + : this.__joinerConfig + ) as ModuleJoinerConfig + + const eventBuilder = moduleEventBuilderFactory({ + action, + object, + source: source || __joinerConfig.serviceName!, + eventName, + }) + + eventBuilder({ + data, + sharedContext: context, + }) + } + /** * @internal this method is not meant to be used except by the internal team for now * @param groupedEvents diff --git a/packages/core/utils/src/modules-sdk/types/medusa-service.ts b/packages/core/utils/src/modules-sdk/types/medusa-service.ts index 2f76cc26ac..3e730ae8bb 100644 --- a/packages/core/utils/src/modules-sdk/types/medusa-service.ts +++ b/packages/core/utils/src/modules-sdk/types/medusa-service.ts @@ -265,4 +265,29 @@ export type MedusaServiceReturnType> = { new (...args: any[]): AbstractModuleService $modelObjects: InferModelFromConfig + /** + * helper function to aggregate events. Will format the message properly and store in + * the message aggregator in the context + * @param action + * @param object + * @param eventName optional, can be inferred from the module joiner config + action + object + * @param source optional, can be inferred from the module joiner config + * @param data + * @param context + */ + aggregatedEvents({ + action, + object, + eventName, + source, + data, + context, + }: { + action: string + object: string + eventName: string + source?: string + data: { id: any } | { id: any }[] + context: Context + }): void } diff --git a/packages/modules/fulfillment/src/utils/events.ts b/packages/modules/fulfillment/src/utils/events.ts index 9e0e0bfb46..6644c35c68 100644 --- a/packages/modules/fulfillment/src/utils/events.ts +++ b/packages/modules/fulfillment/src/utils/events.ts @@ -10,157 +10,157 @@ import { import { Context } from "@medusajs/types" import { CommonEvents, - eventBuilderFactory, FulfillmentEvents, + moduleEventBuilderFactory, Modules, } from "@medusajs/utils" export const eventBuilders = { - createdFulfillment: eventBuilderFactory({ + createdFulfillment: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "fulfillment", eventsEnum: FulfillmentEvents, }), - updatedFulfillment: eventBuilderFactory({ + updatedFulfillment: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "fulfillment", eventsEnum: FulfillmentEvents, }), - createdFulfillmentAddress: eventBuilderFactory({ + createdFulfillmentAddress: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "fulfillment_address", eventsEnum: FulfillmentEvents, }), - createdFulfillmentItem: eventBuilderFactory({ + createdFulfillmentItem: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "fulfillment_item", eventsEnum: FulfillmentEvents, }), - createdFulfillmentLabel: eventBuilderFactory({ + createdFulfillmentLabel: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "fulfillment_label", eventsEnum: FulfillmentEvents, }), - updatedFulfillmentLabel: eventBuilderFactory({ + updatedFulfillmentLabel: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "fulfillment_label", eventsEnum: FulfillmentEvents, }), - deletedFulfillmentLabel: eventBuilderFactory({ + deletedFulfillmentLabel: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.DELETED, object: "fulfillment_label", eventsEnum: FulfillmentEvents, }), - createdShippingProfile: eventBuilderFactory({ + createdShippingProfile: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "shipping_profile", eventsEnum: FulfillmentEvents, }), - createdShippingOptionType: eventBuilderFactory({ + createdShippingOptionType: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "shipping_option_type", eventsEnum: FulfillmentEvents, }), - updatedShippingOptionType: eventBuilderFactory({ + updatedShippingOptionType: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "shipping_option_type", eventsEnum: FulfillmentEvents, }), - deletedShippingOptionType: eventBuilderFactory({ + deletedShippingOptionType: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.DELETED, object: "shipping_option_type", eventsEnum: FulfillmentEvents, }), - createdShippingOptionRule: eventBuilderFactory({ + createdShippingOptionRule: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "shipping_option_rule", eventsEnum: FulfillmentEvents, }), - updatedShippingOptionRule: eventBuilderFactory({ + updatedShippingOptionRule: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "shipping_option_rule", eventsEnum: FulfillmentEvents, }), - deletedShippingOptionRule: eventBuilderFactory({ + deletedShippingOptionRule: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.DELETED, object: "shipping_option_rule", eventsEnum: FulfillmentEvents, }), - createdShippingOption: eventBuilderFactory({ + createdShippingOption: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "shipping_option", eventsEnum: FulfillmentEvents, }), - updatedShippingOption: eventBuilderFactory({ + updatedShippingOption: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "shipping_option", eventsEnum: FulfillmentEvents, }), - createdFulfillmentSet: eventBuilderFactory({ + createdFulfillmentSet: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "fulfillment_set", eventsEnum: FulfillmentEvents, }), - updatedFulfillmentSet: eventBuilderFactory({ + updatedFulfillmentSet: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "fulfillment_set", eventsEnum: FulfillmentEvents, }), - deletedFulfillmentSet: eventBuilderFactory({ + deletedFulfillmentSet: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.DELETED, object: "fulfillment_set", eventsEnum: FulfillmentEvents, }), - createdServiceZone: eventBuilderFactory({ + createdServiceZone: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "service_zone", eventsEnum: FulfillmentEvents, }), - updatedServiceZone: eventBuilderFactory({ + updatedServiceZone: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "service_zone", eventsEnum: FulfillmentEvents, }), - deletedServiceZone: eventBuilderFactory({ + deletedServiceZone: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.DELETED, object: "service_zone", eventsEnum: FulfillmentEvents, }), - createdGeoZone: eventBuilderFactory({ + createdGeoZone: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.CREATED, object: "geo_zone", eventsEnum: FulfillmentEvents, }), - updatedGeoZone: eventBuilderFactory({ + updatedGeoZone: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.UPDATED, object: "geo_zone", eventsEnum: FulfillmentEvents, }), - deletedGeoZone: eventBuilderFactory({ + deletedGeoZone: moduleEventBuilderFactory({ source: Modules.FULFILLMENT, action: CommonEvents.DELETED, object: "geo_zone", diff --git a/packages/modules/notification/src/utils/events.ts b/packages/modules/notification/src/utils/events.ts index d0dd691231..67b2efaeef 100644 --- a/packages/modules/notification/src/utils/events.ts +++ b/packages/modules/notification/src/utils/events.ts @@ -1,12 +1,12 @@ import { CommonEvents, - eventBuilderFactory, + moduleEventBuilderFactory, Modules, NotificationEvents, } from "@medusajs/utils" export const eventBuilders = { - createdNotification: eventBuilderFactory({ + createdNotification: moduleEventBuilderFactory({ source: Modules.NOTIFICATION, action: CommonEvents.CREATED, object: "notification", diff --git a/packages/modules/pricing/src/utils/events.ts b/packages/modules/pricing/src/utils/events.ts index 9856220fbd..cfa38f5cfc 100644 --- a/packages/modules/pricing/src/utils/events.ts +++ b/packages/modules/pricing/src/utils/events.ts @@ -1,66 +1,66 @@ import { CommonEvents, - eventBuilderFactory, + moduleEventBuilderFactory, Modules, PricingEvents, } from "@medusajs/utils" export const eventBuilders = { - createdPriceSet: eventBuilderFactory({ + createdPriceSet: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.CREATED, object: "price_set", eventsEnum: PricingEvents, }), - createdPrice: eventBuilderFactory({ + createdPrice: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.CREATED, object: "price", eventsEnum: PricingEvents, }), - createdPriceRule: eventBuilderFactory({ + createdPriceRule: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.CREATED, object: "price_rule", eventsEnum: PricingEvents, }), - createdPriceList: eventBuilderFactory({ + createdPriceList: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.CREATED, object: "price_list", eventsEnum: PricingEvents, }), - createdPriceListRule: eventBuilderFactory({ + createdPriceListRule: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.CREATED, object: "price_list_rule", eventsEnum: PricingEvents, }), - attachedPriceListRule: eventBuilderFactory({ + attachedPriceListRule: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.ATTACHED, object: "price_list_rule", eventsEnum: PricingEvents, }), - updatedPrice: eventBuilderFactory({ + updatedPrice: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.UPDATED, object: "price", eventsEnum: PricingEvents, }), - updatedPriceRule: eventBuilderFactory({ + updatedPriceRule: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.UPDATED, object: "price_rule", eventsEnum: PricingEvents, }), - deletedPrice: eventBuilderFactory({ + deletedPrice: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.DELETED, object: "price", eventsEnum: PricingEvents, }), - deletedPriceRule: eventBuilderFactory({ + deletedPriceRule: moduleEventBuilderFactory({ source: Modules.PRICING, action: CommonEvents.DELETED, object: "price_rule", 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 46285fc07c..1bdd1ace0a 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 @@ -692,11 +692,11 @@ moduleIntegrationTestRunner({ [ expect.objectContaining({ data: { id: productCategoryOne.id }, - name: "product-category.deleted", + name: "Product.product-category.deleted", metadata: { - action: "", - object: "", - source: "", + action: CommonEvents.DELETED, + object: "product_category", + source: Modules.PRODUCT, }, }), ], 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 fe7f36a3ca..bb866f6481 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 @@ -1,5 +1,5 @@ import { IProductModuleService } from "@medusajs/types" -import { Modules, ProductStatus } from "@medusajs/utils" +import { CommonEvents, Modules, ProductStatus } from "@medusajs/utils" import { Product, ProductCollection } from "@models" import { MockEventBusService, @@ -281,12 +281,12 @@ moduleIntegrationTestRunner({ expect(eventBusSpy).toHaveBeenCalledWith( [ { - name: "product-collection.deleted", + name: "Product.product-collection.deleted", data: { id: collectionId }, metadata: { - action: "", - object: "", - source: "", + action: CommonEvents.DELETED, + object: "product_collection", + source: Modules.PRODUCT, }, }, ], diff --git a/packages/modules/product/src/utils/events.ts b/packages/modules/product/src/utils/events.ts index 6ca5ae4aec..cd15db6306 100644 --- a/packages/modules/product/src/utils/events.ts +++ b/packages/modules/product/src/utils/events.ts @@ -1,114 +1,114 @@ import { CommonEvents, - eventBuilderFactory, + moduleEventBuilderFactory, Modules, ProductEvents, } from "@medusajs/utils" export const eventBuilders = { - createdProduct: eventBuilderFactory({ + createdProduct: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.CREATED, object: "product", eventsEnum: ProductEvents, }), - updatedProduct: eventBuilderFactory({ + updatedProduct: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.UPDATED, object: "product", eventsEnum: ProductEvents, }), - deletedProduct: eventBuilderFactory({ + deletedProduct: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.DELETED, object: "product", eventsEnum: ProductEvents, }), - createdProductVariant: eventBuilderFactory({ + createdProductVariant: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.CREATED, object: "product_variant", eventsEnum: ProductEvents, }), - updatedProductVariant: eventBuilderFactory({ + updatedProductVariant: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.UPDATED, object: "product_variant", eventsEnum: ProductEvents, }), - deletedProductVariant: eventBuilderFactory({ + deletedProductVariant: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.DELETED, object: "product_variant", eventsEnum: ProductEvents, }), - createdProductOption: eventBuilderFactory({ + createdProductOption: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.CREATED, object: "product_option", eventsEnum: ProductEvents, }), - updatedProductOption: eventBuilderFactory({ + updatedProductOption: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.UPDATED, object: "product_option", eventsEnum: ProductEvents, }), - deletedProductOption: eventBuilderFactory({ + deletedProductOption: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.DELETED, object: "product_option", eventsEnum: ProductEvents, }), - createdProductType: eventBuilderFactory({ + createdProductType: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.CREATED, object: "product_type", eventsEnum: ProductEvents, }), - updatedProductType: eventBuilderFactory({ + updatedProductType: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.UPDATED, object: "product_type", eventsEnum: ProductEvents, }), - deletedProductType: eventBuilderFactory({ + deletedProductType: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.DELETED, object: "product_type", eventsEnum: ProductEvents, }), - createdProductTag: eventBuilderFactory({ + createdProductTag: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.CREATED, object: "product_tag", eventsEnum: ProductEvents, }), - updatedProductTag: eventBuilderFactory({ + updatedProductTag: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.UPDATED, object: "product_tag", eventsEnum: ProductEvents, }), - deletedProductTag: eventBuilderFactory({ + deletedProductTag: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.DELETED, object: "product_tag", eventsEnum: ProductEvents, }), - createdProductCategory: eventBuilderFactory({ + createdProductCategory: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.CREATED, object: "product_category", eventsEnum: ProductEvents, }), - updatedProductCategory: eventBuilderFactory({ + updatedProductCategory: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.UPDATED, object: "product_category", eventsEnum: ProductEvents, }), - deletedProductCategory: eventBuilderFactory({ + deletedProductCategory: moduleEventBuilderFactory({ source: Modules.PRODUCT, action: CommonEvents.DELETED, object: "product_category",