chore(product): Update the events emitted from the product module (#9557)

This commit is contained in:
Adrien de Peretti
2024-10-14 14:19:32 +02:00
committed by GitHub
parent b40fa6353e
commit d8b7f90689
6 changed files with 117 additions and 127 deletions

View File

@@ -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(

View File

@@ -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<IProductModuleService>({
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<IProductModuleService>({
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,

View File

@@ -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<IProductModuleService>({
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<IProductModuleService>({
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<IProductModuleService>({
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,

View File

@@ -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<ProductTypes.ProductCollectionDTO>
@InjectManager()
@EmitEvents()
async createProductCollections(
data:
| ProductTypes.CreateProductCollectionDTO[]
@@ -875,15 +872,10 @@ export default class ProductModuleService
ProductTypes.ProductCollectionDTO[]
>(collections)
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
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<ProductTypes.ProductCollectionDTO>
@InjectTransactionManager()
@EmitEvents()
async upsertProductCollections(
data:
| ProductTypes.UpsertProductCollectionDTO[]
@@ -951,27 +945,17 @@ export default class ProductModuleService
>(result)
if (created.length) {
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
created.map(({ id }) => ({
name: ProductCollectionEvents.COLLECTION_CREATED,
data: { id },
})),
{
internal: true,
}
)
eventBuilders.createdProductCollection({
data: created,
sharedContext,
})
}
if (updated.length) {
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
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<ProductTypes.ProductCollectionDTO[]>
@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<ProductCollectionEventData>(
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<ProductTypes.ProductDTO>
@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<ProductEventData>(
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<ProductTypes.ProductDTO>
@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<ProductEventData>(
created.map(({ id }) => ({
name: ProductEvents.PRODUCT_CREATED,
data: { id },
})),
{
internal: true,
}
)
eventBuilders.createdProduct({
data: created,
sharedContext,
})
}
if (updated.length) {
await this.eventBusModuleService_?.emit<ProductEventData>(
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<ProductTypes.ProductDTO[]>
@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<ProductEventData>(
updatedProducts.map(({ id }) => ({
name: ProductEvents.PRODUCT_UPDATED,
data: { id },
})),
{
internal: true,
}
)
eventBuilders.updatedProduct({
data: updatedProducts,
sharedContext,
})
return isString(idOrSelector) ? updatedProducts[0] : updatedProducts
}

View File

@@ -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
}

View File

@@ -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,
}),
}