feat: Product module events (#7598)

WIP, not ready to review

**what**
- add events
- integration tests of emitted events
- remove integration tests on auto generated services in favor of module method integration tests
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-06-06 11:47:38 +00:00
1 parent c3b6fc1d8e
commit 3fbb8aa671
20 files changed
+292 -1756

No files matched your search

@@ -22,6 +22,7 @@ import { ProductCategoryService, ProductService } from "@services"
import {
arrayDifference,
EmitEvents,
InjectManager,
InjectTransactionManager,
isPresent,
@@ -51,6 +52,7 @@ import {
UpdateTypeInput,
} from "../types"
import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config"
import { eventBuilders } from "../utils"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -128,7 +130,6 @@ export default class ProductModuleService<
protected readonly productService_: ProductService<TProduct>
// eslint-disable-next-line max-len
protected readonly productVariantService_: ModulesSdkTypes.InternalModuleService<TProductVariant>
// eslint-disable-next-line max-len
protected readonly productCategoryService_: ProductCategoryService<TProductCategory>
// eslint-disable-next-line max-len
@@ -193,6 +194,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductVariantDTO>
@InjectManager("baseRepository_")
@EmitEvents()
async createVariants(
data:
| ProductTypes.CreateProductVariantDTO[]
@@ -220,7 +222,7 @@ export default class ProductModuleService<
if (data.some((v) => !v.product_id)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Tried to create variants without specifying a product_id"
"Unable to create variants without specifying a product_id"
)
}
@@ -238,10 +240,17 @@ export default class ProductModuleService<
const productVariantsWithOptions =
ProductModuleService.assignOptionsToVariants(data, productOptions)
return await this.productVariantService_.create(
const createdVariants = await this.productVariantService_.create(
productVariantsWithOptions,
sharedContext
)
eventBuilders.createdProductVariant({
data: createdVariants,
sharedContext,
})
return createdVariants
}
async upsertVariants(
@@ -254,6 +263,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductVariantDTO>
@InjectTransactionManager("baseRepository_")
@EmitEvents()
async upsertVariants(
data:
| ProductTypes.UpsertProductVariantDTO[]
@@ -300,6 +310,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductVariantDTO[]>
@InjectManager("baseRepository_")
@EmitEvents()
async updateVariants(
idOrSelector: string | ProductTypes.FilterableProductVariantProps,
data: ProductTypes.UpdateProductVariantDTO,
@@ -373,7 +384,7 @@ export default class ProductModuleService<
sharedContext
)
const { entities: productVariants } =
const { entities: productVariants, performedActions } =
await this.productVariantService_.upsertWithReplace(
ProductModuleService.assignOptionsToVariants(
variantsWithProductId,
@@ -385,6 +396,19 @@ export default class ProductModuleService<
sharedContext
)
eventBuilders.createdProductVariant({
data: performedActions.created[ProductVariant.name] ?? [],
sharedContext,
})
eventBuilders.updatedProductVariant({
data: performedActions.updated[ProductVariant.name] ?? [],
sharedContext,
})
eventBuilders.deletedProductVariant({
data: performedActions.deleted[ProductVariant.name] ?? [],
sharedContext,
})
return productVariants
}
@@ -5,6 +5,8 @@ export type InitializeModuleInjectableDependencies = {
eventBusModuleService?: IEventBusModuleService
}
// TODO: remove and cleanup bellow code
export type ProductCategoryEventData = {
id: string
}
@@ -0,0 +1,102 @@
import {
CommonEvents,
eventBuilderFactory,
Modules,
ProductEvents,
} from "@medusajs/utils"
export const eventBuilders = {
createdProduct: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
object: "product",
eventsEnum: ProductEvents,
isMainEntity: true,
}),
updatedProduct: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
object: "product",
eventsEnum: ProductEvents,
isMainEntity: true,
}),
deletedProduct: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.DELETED,
object: "product",
eventsEnum: ProductEvents,
isMainEntity: true,
}),
createdProductVariant: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
object: "product_variant",
eventsEnum: ProductEvents,
}),
updatedProductVariant: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
object: "product_variant",
eventsEnum: ProductEvents,
}),
deletedProductVariant: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.DELETED,
object: "product_variant",
eventsEnum: ProductEvents,
}),
createdProductOption: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
object: "product_option",
eventsEnum: ProductEvents,
}),
updatedProductOption: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
object: "product_option",
eventsEnum: ProductEvents,
}),
deletedProductOption: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.DELETED,
object: "product_option",
eventsEnum: ProductEvents,
}),
createdProductType: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
object: "product_type",
eventsEnum: ProductEvents,
}),
updatedProductType: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
object: "product_type",
eventsEnum: ProductEvents,
}),
deletedProductType: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.DELETED,
object: "product_type",
eventsEnum: ProductEvents,
}),
createdProductTag: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
object: "product_tag",
eventsEnum: ProductEvents,
}),
updatedProductTag: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
object: "product_tag",
eventsEnum: ProductEvents,
}),
deletedProductTag: eventBuilderFactory({
service: Modules.PRODUCT,
action: CommonEvents.DELETED,
object: "product_tag",
eventsEnum: ProductEvents,
}),
}
@@ -0,0 +1 @@
export * from "./events"