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
2024-06-06 11:47:38 +00:00
committed by GitHub
parent c3b6fc1d8e
commit 3fbb8aa671
20 changed files with 292 additions and 1756 deletions
@@ -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
}