chore(): Module Internal Events (#13296)

* chore(): Ensure the product module emits all necessary events

* chore(): Ensure the product module emits all necessary events

* Update events tests

* more events and fixes

* more tests and category fixes

* more tests and category fixes

* Add todo

* update updateProduct_ event emitting and adjust test

* Adjust update products implementation to rely on already computed events

* rm unnecessary update variants events

* Fix formatting in changeset for product events

* refactor: Manage event emitting automatically (WIP)

* refactor: Manage event emitting automatically (WIP)

* chore(api-key): Add missing emit events and refactoring

* chore(cart): Add missing emit events and refactoring

* chore(customer): Add missing emit events and refactoring

* chore(fufillment, utils): Add missing emit events and refactoring

* chore(fufillment, utils): Add missing emit events and refactoring

* chore(inventory): Add missing emit events and refactoring

* chore(notification): Add missing emit events and refactoring

* chore(utils): Remove medusa service event handling legacy

* chore(product): Add missing emit events and refactoring

* chore(order): Add missing emit events and refactoring

* chore(payment): Add missing emit events and refactoring

* chore(pricing, util): Add missing emit events and refactoring, fix internal service upsertWithReplace event dispatching

* chore(promotions): Add missing emit events and refactoring

* chore(region): Add missing emit events and refactoring

* chore(sales-channel): Add missing emit events and refactoring

* chore(settings): Add missing emit events and refactoring

* chore(stock-location): Add missing emit events and refactoring

* chore(store): Add missing emit events and refactoring

* chore(taxes): Add missing emit events and refactoring

* chore(user): Add missing emit events and refactoring

* fix unit tests

* rm changeset for regeneration

* Create changeset for Medusa.js patch updates

Add a changeset for patch updates to multiple Medusa.js modules.

* rm unused product event builders

* address feedback

* remove old changeset

* fix event action for token generated

* fix user module events

* fix import

* fix promotion events

* add new module integration tests shard

* fix medusa service

* revert shard

* fix event action

* fix pipeline

* fix pipeline

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2025-09-10 14:37:38 +02:00
committed by GitHub
parent afe21741c4
commit e8822f3e69
55 changed files with 3614 additions and 2353 deletions

View File

@@ -79,7 +79,10 @@ export const buildProductAndRelationsData = ({
title: faker.commerce.productName(),
sku: faker.commerce.productName(),
options: options
? { [options[0].title]: options[0].values[0] }
? options.reduce((acc, option) => {
acc[option.title] = option.values[0]
return acc
}, {} as Record<string, string>)
: {
[defaultOptionTitle]: defaultOptionValue,
},

View File

@@ -1,15 +1,11 @@
import { IProductModuleService } from "@medusajs/framework/types"
import {
CommonEvents,
composeMessage,
Modules,
ProductEvents,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { Product, ProductCategory } from "@models"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
import { productCategoriesRankData } from "../../__fixtures__/product-category/data"
@@ -18,9 +14,6 @@ jest.setTimeout(30000)
moduleIntegrationTestRunner<IProductModuleService>({
moduleName: Modules.PRODUCT,
injectedDependencies: {
[Modules.EVENT_BUS]: new MockEventBusService(),
},
testSuite: ({ MikroOrmWrapper, service }) => {
describe("ProductModuleService product categories", () => {
let productOne: Product
@@ -404,29 +397,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
)
})
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
const category = await service.createProductCategories({
name: "New Category",
parent_category_id: productCategoryOne.id,
})
expect(eventBusSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_CATEGORY_CREATED, {
data: { id: category.id },
object: "product_category",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
],
{
internal: true,
}
)
})
it("should append rank from an existing category depending on parent", async () => {
await service.createProductCategories({
@@ -504,29 +474,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
productCategoryZeroTwo = categories[5]
})
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
eventBusSpy.mockClear()
await service.updateProductCategories(productCategoryZero.id, {
name: "New Category",
})
expect(eventBusSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_CATEGORY_UPDATED, {
data: { id: productCategoryZero.id },
object: "product_category",
source: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
it("should update the name of the category successfully", async () => {
await service.updateProductCategories(productCategoryZero.id, {
@@ -683,30 +630,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
productCategoryTwo = categories[2]
})
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
eventBusSpy.mockClear()
await service.deleteProductCategories([productCategoryOne.id])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
expect.objectContaining({
data: { id: productCategoryOne.id },
name: "product.product-category.deleted",
metadata: {
action: CommonEvents.DELETED,
object: "product_category",
source: Modules.PRODUCT,
},
}),
],
{
internal: true,
}
)
})
it("should throw an error when an id does not exist", async () => {
let error

View File

@@ -1,14 +1,10 @@
import { IProductModuleService } from "@medusajs/framework/types"
import {
CommonEvents,
composeMessage,
Modules,
ProductEvents,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
import { Product, ProductCollection } from "@models"
@@ -18,9 +14,6 @@ jest.setTimeout(30000)
moduleIntegrationTestRunner<IProductModuleService>({
moduleName: Modules.PRODUCT,
injectedDependencies: {
[Modules.EVENT_BUS]: new MockEventBusService(),
},
testSuite: ({ MikroOrmWrapper, service }) => {
describe("ProductModuleService product collections", () => {
let productOne: Product
@@ -284,59 +277,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
expect(collections).toHaveLength(0)
})
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.deleteProductCollections([collectionId])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
name: "product.product-collection.deleted",
data: { id: collectionId },
metadata: {
action: CommonEvents.DELETED,
object: "product_collection",
source: Modules.PRODUCT,
},
},
],
{
internal: true,
}
)
})
})
describe("updateCollections", () => {
const collectionId = "test-1"
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.upsertProductCollections([
{
id: collectionId,
title: "New Collection",
product_ids: ["product_id"],
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_COLLECTION_UPDATED, {
data: { id: collectionId },
object: "product_collection",
source: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
it("should update the value of the collection successfully", async () => {
await service.upsertProductCollections([
@@ -534,28 +479,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
)
})
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
const collections = await service.createProductCollections([
{ title: "New Collection" },
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_COLLECTION_CREATED, {
data: { id: collections[0].id },
object: "product_collection",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
],
{
internal: true,
}
)
})
})
})
},

View File

@@ -1,15 +1,11 @@
import { IProductModuleService } from "@medusajs/framework/types"
import {
CommonEvents,
composeMessage,
Modules,
ProductEvents,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { Product, ProductTag } from "@models"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
@@ -18,15 +14,6 @@ jest.setTimeout(30000)
moduleIntegrationTestRunner<IProductModuleService>({
moduleName: Modules.PRODUCT,
testSuite: ({ MikroOrmWrapper, service }) => {
let eventBusEmitSpy
beforeEach(() => {
eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
})
afterEach(() => {
jest.clearAllMocks()
})
describe("ProductModuleService product tags", () => {
let tagOne: ProductTag
@@ -303,20 +290,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
expect(productTag.value).toEqual("UK")
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_TAG_UPDATED, {
data: { id: productTag.id },
object: "product_tag",
source: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
it("should throw an error when an id does not exist", async () => {
@@ -350,20 +323,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
expect(productTag[0]?.value).toEqual("UK")
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_TAG_CREATED, {
data: { id: productTag[0].id },
object: "product_tag",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
],
{
internal: true,
}
)
})
})
@@ -409,26 +368,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
const newTag = productTags.find((t) => t.value === "new")!
const updatedTag = productTags.find((t) => t.value === "updated")!
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_TAG_CREATED, {
data: { id: newTag.id },
object: "product_tag",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
composeMessage(ProductEvents.PRODUCT_TAG_UPDATED, {
data: { id: updatedTag.id },
object: "product_tag",
source: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
})
})

View File

@@ -7,15 +7,11 @@ import {
UpdateProductVariantDTO,
} from "@medusajs/framework/types"
import {
CommonEvents,
composeMessage,
Modules,
ProductEvents,
ProductStatus,
} from "@medusajs/framework/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
@@ -24,15 +20,6 @@ jest.setTimeout(30000)
moduleIntegrationTestRunner<IProductModuleService>({
moduleName: Modules.PRODUCT,
testSuite: ({ service }) => {
let eventBusEmitSpy
beforeEach(() => {
eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
})
afterEach(() => {
jest.clearAllMocks()
})
describe("ProductModuleService product variants", () => {
let variantOne: ProductVariantDTO
@@ -212,20 +199,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
)
expect(productVariant.title).toEqual("new test")
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_VARIANT_UPDATED, {
data: { id: variantOne.id },
object: "product_variant",
source: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
it("should do a partial update on the options of a variant successfully", async () => {
@@ -294,20 +267,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
})
)
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_VARIANT_CREATED, {
data: { id: variant.id },
object: "product_variant",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
],
{
internal: true,
}
)
})
it("should correctly associate variants with own product options", async () => {

View File

@@ -4,11 +4,8 @@ import {
ProductTagDTO,
} from "@medusajs/framework/types"
import {
CommonEvents,
composeMessage,
kebabCase,
Modules,
ProductEvents,
ProductStatus,
} from "@medusajs/framework/utils"
import {
@@ -20,7 +17,6 @@ import {
} from "@models"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
import { UpdateProductInput } from "@types"
@@ -34,9 +30,6 @@ jest.setTimeout(300000)
moduleIntegrationTestRunner<IProductModuleService>({
moduleName: Modules.PRODUCT,
injectedDependencies: {
[Modules.EVENT_BUS]: new MockEventBusService(),
},
testSuite: ({ MikroOrmWrapper, service }) => {
describe("ProductModuleService products", function () {
let productCollectionOne: ProductCollection
@@ -573,37 +566,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
)
})
it("should emit events through event bus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
const data = buildProductAndRelationsData({
images,
thumbnail: images[0].url,
})
const updateData = {
...data,
options: data.options,
id: productOne.id,
title: "updated title",
}
await service.upsertProducts([updateData])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_UPDATED, {
data: { id: productOne.id },
object: "product",
source: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
it("should add relationships to a product", async () => {
const updateData = {
@@ -1086,29 +1048,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
)
})
it("should emit events through eventBus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
const data = buildProductAndRelationsData({
images,
thumbnail: images[0].url,
})
const products = await service.createProducts([data])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_CREATED, {
data: { id: products[0].id },
object: "product",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
],
{
internal: true,
}
)
})
it("should throw because variant doesn't have all options set", async () => {
const error = await service
@@ -1289,75 +1228,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
expect(softDeleted).toHaveLength(1)
})
it("should emit events through eventBus", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
const data = buildProductAndRelationsData({
images,
thumbnail: images[0].url,
})
const products = await service.createProducts([data])
await service.softDeleteProducts([products[0].id])
expect(eventBusSpy).toHaveBeenNthCalledWith(
1,
[
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,
}),
composeMessage(ProductEvents.PRODUCT_VARIANT_DELETED, {
data: { id: [products[0].variants[0].id] },
object: "product_variant",
source: Modules.PRODUCT,
action: CommonEvents.DELETED,
}),
composeMessage(ProductEvents.PRODUCT_OPTION_DELETED, {
data: { id: [products[0].options[0].id] },
object: "product_option",
source: Modules.PRODUCT,
action: CommonEvents.DELETED,
}),
composeMessage(ProductEvents.PRODUCT_IMAGE_DELETED, {
data: {
id: [products[0].images[0].id],
},
object: "product_image",
source: Modules.PRODUCT,
action: CommonEvents.DELETED,
}),
composeMessage(ProductEvents.PRODUCT_OPTION_VALUE_DELETED, {
data: {
id: [products[0].options[0].values[0].id],
},
object: "product_option_value",
source: Modules.PRODUCT,
action: CommonEvents.DELETED,
}),
],
{
internal: true,
}
)
})
})
describe("restore", function () {