From d8b7f9068921c82401d398ab7605bbffd16f0a3a Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Mon, 14 Oct 2024 14:19:32 +0200 Subject: [PATCH] chore(product): Update the events emitted from the product module (#9557) --- packages/core/utils/src/product/events.ts | 4 +- .../product-collections.spec.ts | 24 ++-- .../product-module-service/products.spec.ts | 51 ++++++-- .../src/services/product-module-service.ts | 115 ++++++------------ packages/modules/product/src/types/index.ts | 32 ----- packages/modules/product/src/utils/events.ts | 18 +++ 6 files changed, 117 insertions(+), 127 deletions(-) diff --git a/packages/core/utils/src/product/events.ts b/packages/core/utils/src/product/events.ts index 464972bc09..379ca42e3a 100644 --- a/packages/core/utils/src/product/events.ts +++ b/packages/core/utils/src/product/events.ts @@ -7,7 +7,8 @@ const eventBaseNames: [ "productOption", "productType", "productTag", - "productCategory" + "productCategory", + "productCollection" ] = [ "product", "productVariant", @@ -15,6 +16,7 @@ const eventBaseNames: [ "productType", "productTag", "productCategory", + "productCollection" ] export const ProductEvents = buildEventNamesFromEntityName( 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 f16acc2be6..2da9b772c0 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,11 @@ import { IProductModuleService } from "@medusajs/framework/types" -import { CommonEvents, Modules, ProductStatus } from "@medusajs/framework/utils" +import { + CommonEvents, + composeMessage, + Modules, + ProductEvents, + ProductStatus, +} from "@medusajs/framework/utils" import { Product, ProductCollection } from "@models" import { MockEventBusService, @@ -313,10 +319,12 @@ moduleIntegrationTestRunner({ expect(eventBusSpy).toHaveBeenCalledTimes(1) expect(eventBusSpy).toHaveBeenCalledWith( [ - { + composeMessage(ProductEvents.PRODUCT_COLLECTION_UPDATED, { data: { id: collectionId }, - name: "product-collection.updated", - }, + object: "product_collection", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), ], { internal: true, @@ -514,10 +522,12 @@ moduleIntegrationTestRunner({ expect(eventBusSpy).toHaveBeenCalledTimes(1) expect(eventBusSpy).toHaveBeenCalledWith( [ - { + composeMessage(ProductEvents.PRODUCT_COLLECTION_CREATED, { data: { id: collections[0].id }, - name: "product-collection.created", - }, + object: "product_collection", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), ], { internal: true, 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 2b7327491b..8e586753d9 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 @@ -3,7 +3,14 @@ import { ProductCategoryDTO, ProductTagDTO, } from "@medusajs/framework/types" -import { Modules, ProductStatus, kebabCase } from "@medusajs/framework/utils" +import { + CommonEvents, + composeMessage, + kebabCase, + Modules, + ProductEvents, + ProductStatus, +} from "@medusajs/framework/utils" import { Product, ProductCategory, @@ -376,10 +383,12 @@ moduleIntegrationTestRunner({ expect(eventBusSpy).toHaveBeenCalledTimes(1) expect(eventBusSpy).toHaveBeenCalledWith( [ - { - name: "product.updated", + composeMessage(ProductEvents.PRODUCT_UPDATED, { data: { id: productOne.id }, - }, + object: "product", + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + }), ], { internal: true, @@ -731,10 +740,12 @@ moduleIntegrationTestRunner({ expect(eventBusSpy).toHaveBeenCalledTimes(1) expect(eventBusSpy).toHaveBeenCalledWith( [ - { - name: "product.created", + composeMessage(ProductEvents.PRODUCT_CREATED, { data: { id: products[0].id }, - }, + object: "product", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), ], { internal: true, @@ -824,12 +835,30 @@ moduleIntegrationTestRunner({ await service.softDeleteProducts([products[0].id]) - expect(eventBusSpy).toHaveBeenCalledWith( + expect(eventBusSpy).toHaveBeenNthCalledWith( + 1, [ - { - name: "product.created", + composeMessage(ProductEvents.PRODUCT_CREATED, { data: { id: products[0].id }, - }, + object: "product", + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + }), + ], + { + internal: true, + } + ) + + expect(eventBusSpy).toHaveBeenNthCalledWith( + 2, + [ + composeMessage(ProductEvents.PRODUCT_DELETED, { + data: { id: [products[0].id] }, + object: "product", + source: Modules.PRODUCT, + action: CommonEvents.DELETED, + }), ], { 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 6fdf0c71f6..4e6ccdea68 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -39,10 +39,6 @@ import { toHandle, } from "@medusajs/framework/utils" import { - ProductCollectionEventData, - ProductCollectionEvents, - ProductEventData, - ProductEvents, UpdateCategoryInput, UpdateCollectionInput, UpdateProductInput, @@ -859,6 +855,7 @@ export default class ProductModuleService ): Promise @InjectManager() + @EmitEvents() async createProductCollections( data: | ProductTypes.CreateProductCollectionDTO[] @@ -875,15 +872,10 @@ export default class ProductModuleService ProductTypes.ProductCollectionDTO[] >(collections) - await this.eventBusModuleService_?.emit( - collections.map(({ id }) => ({ - name: ProductCollectionEvents.COLLECTION_CREATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.createdProductCollection({ + data: collections, + sharedContext, + }) return Array.isArray(data) ? createdCollections : createdCollections[0] } @@ -917,7 +909,9 @@ export default class ProductModuleService data: ProductTypes.UpsertProductCollectionDTO, sharedContext?: Context ): Promise + @InjectTransactionManager() + @EmitEvents() async upsertProductCollections( data: | ProductTypes.UpsertProductCollectionDTO[] @@ -951,27 +945,17 @@ export default class ProductModuleService >(result) if (created.length) { - await this.eventBusModuleService_?.emit( - created.map(({ id }) => ({ - name: ProductCollectionEvents.COLLECTION_CREATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.createdProductCollection({ + data: created, + sharedContext, + }) } if (updated.length) { - await this.eventBusModuleService_?.emit( - updated.map(({ id }) => ({ - name: ProductCollectionEvents.COLLECTION_UPDATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.updatedProductCollection({ + data: updated, + sharedContext, + }) } return Array.isArray(data) ? allCollections : allCollections[0] @@ -990,6 +974,7 @@ export default class ProductModuleService ): Promise @InjectManager() + @EmitEvents() async updateProductCollections( idOrSelector: string | ProductTypes.FilterableProductCollectionProps, data: ProductTypes.UpdateProductCollectionDTO, @@ -1027,15 +1012,10 @@ export default class ProductModuleService ProductTypes.ProductCollectionDTO[] >(collections) - await this.eventBusModuleService_?.emit( - updatedCollections.map(({ id }) => ({ - name: ProductCollectionEvents.COLLECTION_UPDATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.updatedProductCollection({ + data: updatedCollections, + sharedContext, + }) return isString(idOrSelector) ? updatedCollections[0] : updatedCollections } @@ -1282,6 +1262,7 @@ export default class ProductModuleService ): Promise @InjectManager() + @EmitEvents() async createProducts( data: ProductTypes.CreateProductDTO[] | ProductTypes.CreateProductDTO, @MedusaContext() sharedContext: Context = {} @@ -1293,15 +1274,10 @@ export default class ProductModuleService ProductTypes.ProductDTO[] >(products) - await this.eventBusModuleService_?.emit( - createdProducts.map(({ id }) => ({ - name: ProductEvents.PRODUCT_CREATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.createdProduct({ + data: createdProducts, + sharedContext, + }) return Array.isArray(data) ? createdProducts : createdProducts[0] } @@ -1316,6 +1292,7 @@ export default class ProductModuleService ): Promise @InjectTransactionManager() + @EmitEvents() async upsertProducts( data: ProductTypes.UpsertProductDTO[] | ProductTypes.UpsertProductDTO, @MedusaContext() sharedContext: Context = {} @@ -1344,27 +1321,17 @@ export default class ProductModuleService >(result) if (created.length) { - await this.eventBusModuleService_?.emit( - created.map(({ id }) => ({ - name: ProductEvents.PRODUCT_CREATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.createdProduct({ + data: created, + sharedContext, + }) } if (updated.length) { - await this.eventBusModuleService_?.emit( - updated.map(({ id }) => ({ - name: ProductEvents.PRODUCT_UPDATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.updatedProduct({ + data: updated, + sharedContext, + }) } return Array.isArray(data) ? allProducts : allProducts[0] @@ -1383,6 +1350,7 @@ export default class ProductModuleService ): Promise @InjectManager() + @EmitEvents() async updateProducts( idOrSelector: string | ProductTypes.FilterableProductProps, data: ProductTypes.UpdateProductDTO, @@ -1413,15 +1381,10 @@ export default class ProductModuleService ProductTypes.ProductDTO[] >(products) - await this.eventBusModuleService_?.emit( - updatedProducts.map(({ id }) => ({ - name: ProductEvents.PRODUCT_UPDATED, - data: { id }, - })), - { - internal: true, - } - ) + eventBuilders.updatedProduct({ + data: updatedProducts, + sharedContext, + }) return isString(idOrSelector) ? updatedProducts[0] : updatedProducts } diff --git a/packages/modules/product/src/types/index.ts b/packages/modules/product/src/types/index.ts index 61c6ea1999..b5c50e7dfa 100644 --- a/packages/modules/product/src/types/index.ts +++ b/packages/modules/product/src/types/index.ts @@ -9,38 +9,6 @@ export type InitializeModuleInjectableDependencies = { EventBus?: IEventBusModuleService } -// TODO: remove and cleanup bellow code - -export type ProductCategoryEventData = { - id: string -} - -export enum ProductCategoryEvents { - CATEGORY_UPDATED = "product-category.updated", - CATEGORY_CREATED = "product-category.created", - CATEGORY_DELETED = "product-category.deleted", -} - -export type ProductEventData = { - id: string -} - -export enum ProductEvents { - PRODUCT_UPDATED = "product.updated", - PRODUCT_CREATED = "product.created", - PRODUCT_DELETED = "product.deleted", -} - -export type ProductCollectionEventData = { - id: string -} - -export enum ProductCollectionEvents { - COLLECTION_UPDATED = "product-collection.updated", - COLLECTION_CREATED = "product-collection.created", - COLLECTION_DELETED = "product-collection.deleted", -} - export type UpdateProductInput = ProductTypes.UpdateProductDTO & { id: string } diff --git a/packages/modules/product/src/utils/events.ts b/packages/modules/product/src/utils/events.ts index ccdaf0c9cb..08decb4d40 100644 --- a/packages/modules/product/src/utils/events.ts +++ b/packages/modules/product/src/utils/events.ts @@ -114,4 +114,22 @@ export const eventBuilders = { object: "product_category", eventsEnum: ProductEvents, }), + createdProductCollection: moduleEventBuilderFactory({ + source: Modules.PRODUCT, + action: CommonEvents.CREATED, + object: "product_collection", + eventName: ProductEvents.PRODUCT_COLLECTION_CREATED, + }), + updatedProductCollection: moduleEventBuilderFactory({ + source: Modules.PRODUCT, + action: CommonEvents.UPDATED, + object: "product_collection", + eventName: ProductEvents.PRODUCT_COLLECTION_UPDATED, + }), + deletedProductCollection: moduleEventBuilderFactory({ + source: Modules.PRODUCT, + action: CommonEvents.DELETED, + object: "product_collection", + eventName: ProductEvents.PRODUCT_COLLECTION_DELETED, + }), }