chore(product): Update the events emitted from the product module (#9557)
This commit is contained in:
1 parent
b40fa6353e
commit
d8b7f90689
6 files changed
+117
-127
No files matched your search
@@ -7,7 +7,8 @@ const eventBaseNames: [
|
|||||||
"productOption",
|
"productOption",
|
||||||
"productType",
|
"productType",
|
||||||
"productTag",
|
"productTag",
|
||||||
"productCategory"
|
"productCategory",
|
||||||
|
"productCollection"
|
||||||
] = [
|
] = [
|
||||||
"product",
|
"product",
|
||||||
"productVariant",
|
"productVariant",
|
||||||
@@ -15,6 +16,7 @@ const eventBaseNames: [
|
|||||||
"productType",
|
"productType",
|
||||||
"productTag",
|
"productTag",
|
||||||
"productCategory",
|
"productCategory",
|
||||||
|
"productCollection"
|
||||||
]
|
]
|
||||||
|
|
||||||
export const ProductEvents = buildEventNamesFromEntityName(
|
export const ProductEvents = buildEventNamesFromEntityName(
|
||||||
|
|||||||
+17
-7
@@ -1,5 +1,11 @@
|
|||||||
import { IProductModuleService } from "@medusajs/framework/types"
|
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 { Product, ProductCollection } from "@models"
|
||||||
import {
|
import {
|
||||||
MockEventBusService,
|
MockEventBusService,
|
||||||
@@ -313,10 +319,12 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||||
expect(eventBusSpy).toHaveBeenCalledWith(
|
expect(eventBusSpy).toHaveBeenCalledWith(
|
||||||
[
|
[
|
||||||
{
|
composeMessage(ProductEvents.PRODUCT_COLLECTION_UPDATED, {
|
||||||
data: { id: collectionId },
|
data: { id: collectionId },
|
||||||
name: "product-collection.updated",
|
object: "product_collection",
|
||||||
},
|
source: Modules.PRODUCT,
|
||||||
|
action: CommonEvents.UPDATED,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
internal: true,
|
internal: true,
|
||||||
@@ -514,10 +522,12 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||||
expect(eventBusSpy).toHaveBeenCalledWith(
|
expect(eventBusSpy).toHaveBeenCalledWith(
|
||||||
[
|
[
|
||||||
{
|
composeMessage(ProductEvents.PRODUCT_COLLECTION_CREATED, {
|
||||||
data: { id: collections[0].id },
|
data: { id: collections[0].id },
|
||||||
name: "product-collection.created",
|
object: "product_collection",
|
||||||
},
|
source: Modules.PRODUCT,
|
||||||
|
action: CommonEvents.CREATED,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
internal: true,
|
internal: true,
|
||||||
|
|||||||
+40
-11
@@ -3,7 +3,14 @@ import {
|
|||||||
ProductCategoryDTO,
|
ProductCategoryDTO,
|
||||||
ProductTagDTO,
|
ProductTagDTO,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import { Modules, ProductStatus, kebabCase } from "@medusajs/framework/utils"
|
import {
|
||||||
|
CommonEvents,
|
||||||
|
composeMessage,
|
||||||
|
kebabCase,
|
||||||
|
Modules,
|
||||||
|
ProductEvents,
|
||||||
|
ProductStatus,
|
||||||
|
} from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
Product,
|
Product,
|
||||||
ProductCategory,
|
ProductCategory,
|
||||||
@@ -376,10 +383,12 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||||
expect(eventBusSpy).toHaveBeenCalledWith(
|
expect(eventBusSpy).toHaveBeenCalledWith(
|
||||||
[
|
[
|
||||||
{
|
composeMessage(ProductEvents.PRODUCT_UPDATED, {
|
||||||
name: "product.updated",
|
|
||||||
data: { id: productOne.id },
|
data: { id: productOne.id },
|
||||||
},
|
object: "product",
|
||||||
|
source: Modules.PRODUCT,
|
||||||
|
action: CommonEvents.UPDATED,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
internal: true,
|
internal: true,
|
||||||
@@ -731,10 +740,12 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
expect(eventBusSpy).toHaveBeenCalledTimes(1)
|
||||||
expect(eventBusSpy).toHaveBeenCalledWith(
|
expect(eventBusSpy).toHaveBeenCalledWith(
|
||||||
[
|
[
|
||||||
{
|
composeMessage(ProductEvents.PRODUCT_CREATED, {
|
||||||
name: "product.created",
|
|
||||||
data: { id: products[0].id },
|
data: { id: products[0].id },
|
||||||
},
|
object: "product",
|
||||||
|
source: Modules.PRODUCT,
|
||||||
|
action: CommonEvents.CREATED,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
internal: true,
|
internal: true,
|
||||||
@@ -824,12 +835,30 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
|||||||
|
|
||||||
await service.softDeleteProducts([products[0].id])
|
await service.softDeleteProducts([products[0].id])
|
||||||
|
|
||||||
expect(eventBusSpy).toHaveBeenCalledWith(
|
expect(eventBusSpy).toHaveBeenNthCalledWith(
|
||||||
|
1,
|
||||||
[
|
[
|
||||||
{
|
composeMessage(ProductEvents.PRODUCT_CREATED, {
|
||||||
name: "product.created",
|
|
||||||
data: { id: products[0].id },
|
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,
|
internal: true,
|
||||||
|
|||||||
@@ -39,10 +39,6 @@ import {
|
|||||||
toHandle,
|
toHandle,
|
||||||
} from "@medusajs/framework/utils"
|
} from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
ProductCollectionEventData,
|
|
||||||
ProductCollectionEvents,
|
|
||||||
ProductEventData,
|
|
||||||
ProductEvents,
|
|
||||||
UpdateCategoryInput,
|
UpdateCategoryInput,
|
||||||
UpdateCollectionInput,
|
UpdateCollectionInput,
|
||||||
UpdateProductInput,
|
UpdateProductInput,
|
||||||
@@ -859,6 +855,7 @@ export default class ProductModuleService
|
|||||||
): Promise<ProductTypes.ProductCollectionDTO>
|
): Promise<ProductTypes.ProductCollectionDTO>
|
||||||
|
|
||||||
@InjectManager()
|
@InjectManager()
|
||||||
|
@EmitEvents()
|
||||||
async createProductCollections(
|
async createProductCollections(
|
||||||
data:
|
data:
|
||||||
| ProductTypes.CreateProductCollectionDTO[]
|
| ProductTypes.CreateProductCollectionDTO[]
|
||||||
@@ -875,15 +872,10 @@ export default class ProductModuleService
|
|||||||
ProductTypes.ProductCollectionDTO[]
|
ProductTypes.ProductCollectionDTO[]
|
||||||
>(collections)
|
>(collections)
|
||||||
|
|
||||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
eventBuilders.createdProductCollection({
|
||||||
collections.map(({ id }) => ({
|
data: collections,
|
||||||
name: ProductCollectionEvents.COLLECTION_CREATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return Array.isArray(data) ? createdCollections : createdCollections[0]
|
return Array.isArray(data) ? createdCollections : createdCollections[0]
|
||||||
}
|
}
|
||||||
@@ -917,7 +909,9 @@ export default class ProductModuleService
|
|||||||
data: ProductTypes.UpsertProductCollectionDTO,
|
data: ProductTypes.UpsertProductCollectionDTO,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<ProductTypes.ProductCollectionDTO>
|
): Promise<ProductTypes.ProductCollectionDTO>
|
||||||
|
|
||||||
@InjectTransactionManager()
|
@InjectTransactionManager()
|
||||||
|
@EmitEvents()
|
||||||
async upsertProductCollections(
|
async upsertProductCollections(
|
||||||
data:
|
data:
|
||||||
| ProductTypes.UpsertProductCollectionDTO[]
|
| ProductTypes.UpsertProductCollectionDTO[]
|
||||||
@@ -951,27 +945,17 @@ export default class ProductModuleService
|
|||||||
>(result)
|
>(result)
|
||||||
|
|
||||||
if (created.length) {
|
if (created.length) {
|
||||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
eventBuilders.createdProductCollection({
|
||||||
created.map(({ id }) => ({
|
data: created,
|
||||||
name: ProductCollectionEvents.COLLECTION_CREATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updated.length) {
|
if (updated.length) {
|
||||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
eventBuilders.updatedProductCollection({
|
||||||
updated.map(({ id }) => ({
|
data: updated,
|
||||||
name: ProductCollectionEvents.COLLECTION_UPDATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.isArray(data) ? allCollections : allCollections[0]
|
return Array.isArray(data) ? allCollections : allCollections[0]
|
||||||
@@ -990,6 +974,7 @@ export default class ProductModuleService
|
|||||||
): Promise<ProductTypes.ProductCollectionDTO[]>
|
): Promise<ProductTypes.ProductCollectionDTO[]>
|
||||||
|
|
||||||
@InjectManager()
|
@InjectManager()
|
||||||
|
@EmitEvents()
|
||||||
async updateProductCollections(
|
async updateProductCollections(
|
||||||
idOrSelector: string | ProductTypes.FilterableProductCollectionProps,
|
idOrSelector: string | ProductTypes.FilterableProductCollectionProps,
|
||||||
data: ProductTypes.UpdateProductCollectionDTO,
|
data: ProductTypes.UpdateProductCollectionDTO,
|
||||||
@@ -1027,15 +1012,10 @@ export default class ProductModuleService
|
|||||||
ProductTypes.ProductCollectionDTO[]
|
ProductTypes.ProductCollectionDTO[]
|
||||||
>(collections)
|
>(collections)
|
||||||
|
|
||||||
await this.eventBusModuleService_?.emit<ProductCollectionEventData>(
|
eventBuilders.updatedProductCollection({
|
||||||
updatedCollections.map(({ id }) => ({
|
data: updatedCollections,
|
||||||
name: ProductCollectionEvents.COLLECTION_UPDATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return isString(idOrSelector) ? updatedCollections[0] : updatedCollections
|
return isString(idOrSelector) ? updatedCollections[0] : updatedCollections
|
||||||
}
|
}
|
||||||
@@ -1282,6 +1262,7 @@ export default class ProductModuleService
|
|||||||
): Promise<ProductTypes.ProductDTO>
|
): Promise<ProductTypes.ProductDTO>
|
||||||
|
|
||||||
@InjectManager()
|
@InjectManager()
|
||||||
|
@EmitEvents()
|
||||||
async createProducts(
|
async createProducts(
|
||||||
data: ProductTypes.CreateProductDTO[] | ProductTypes.CreateProductDTO,
|
data: ProductTypes.CreateProductDTO[] | ProductTypes.CreateProductDTO,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1293,15 +1274,10 @@ export default class ProductModuleService
|
|||||||
ProductTypes.ProductDTO[]
|
ProductTypes.ProductDTO[]
|
||||||
>(products)
|
>(products)
|
||||||
|
|
||||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
eventBuilders.createdProduct({
|
||||||
createdProducts.map(({ id }) => ({
|
data: createdProducts,
|
||||||
name: ProductEvents.PRODUCT_CREATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return Array.isArray(data) ? createdProducts : createdProducts[0]
|
return Array.isArray(data) ? createdProducts : createdProducts[0]
|
||||||
}
|
}
|
||||||
@@ -1316,6 +1292,7 @@ export default class ProductModuleService
|
|||||||
): Promise<ProductTypes.ProductDTO>
|
): Promise<ProductTypes.ProductDTO>
|
||||||
|
|
||||||
@InjectTransactionManager()
|
@InjectTransactionManager()
|
||||||
|
@EmitEvents()
|
||||||
async upsertProducts(
|
async upsertProducts(
|
||||||
data: ProductTypes.UpsertProductDTO[] | ProductTypes.UpsertProductDTO,
|
data: ProductTypes.UpsertProductDTO[] | ProductTypes.UpsertProductDTO,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
@@ -1344,27 +1321,17 @@ export default class ProductModuleService
|
|||||||
>(result)
|
>(result)
|
||||||
|
|
||||||
if (created.length) {
|
if (created.length) {
|
||||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
eventBuilders.createdProduct({
|
||||||
created.map(({ id }) => ({
|
data: created,
|
||||||
name: ProductEvents.PRODUCT_CREATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updated.length) {
|
if (updated.length) {
|
||||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
eventBuilders.updatedProduct({
|
||||||
updated.map(({ id }) => ({
|
data: updated,
|
||||||
name: ProductEvents.PRODUCT_UPDATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array.isArray(data) ? allProducts : allProducts[0]
|
return Array.isArray(data) ? allProducts : allProducts[0]
|
||||||
@@ -1383,6 +1350,7 @@ export default class ProductModuleService
|
|||||||
): Promise<ProductTypes.ProductDTO[]>
|
): Promise<ProductTypes.ProductDTO[]>
|
||||||
|
|
||||||
@InjectManager()
|
@InjectManager()
|
||||||
|
@EmitEvents()
|
||||||
async updateProducts(
|
async updateProducts(
|
||||||
idOrSelector: string | ProductTypes.FilterableProductProps,
|
idOrSelector: string | ProductTypes.FilterableProductProps,
|
||||||
data: ProductTypes.UpdateProductDTO,
|
data: ProductTypes.UpdateProductDTO,
|
||||||
@@ -1413,15 +1381,10 @@ export default class ProductModuleService
|
|||||||
ProductTypes.ProductDTO[]
|
ProductTypes.ProductDTO[]
|
||||||
>(products)
|
>(products)
|
||||||
|
|
||||||
await this.eventBusModuleService_?.emit<ProductEventData>(
|
eventBuilders.updatedProduct({
|
||||||
updatedProducts.map(({ id }) => ({
|
data: updatedProducts,
|
||||||
name: ProductEvents.PRODUCT_UPDATED,
|
sharedContext,
|
||||||
data: { id },
|
})
|
||||||
})),
|
|
||||||
{
|
|
||||||
internal: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return isString(idOrSelector) ? updatedProducts[0] : updatedProducts
|
return isString(idOrSelector) ? updatedProducts[0] : updatedProducts
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,38 +9,6 @@ export type InitializeModuleInjectableDependencies = {
|
|||||||
EventBus?: IEventBusModuleService
|
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 & {
|
export type UpdateProductInput = ProductTypes.UpdateProductDTO & {
|
||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,4 +114,22 @@ export const eventBuilders = {
|
|||||||
object: "product_category",
|
object: "product_category",
|
||||||
eventsEnum: ProductEvents,
|
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,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user