chore: Treat internal event differently, primarely do not display info logs for those events (#8767)

* chore: Treat internal event differently, primarely do not display info log for those events

* revert doc

* add few tests

* only set internal option if present

* revert to previous condition

* start including feedback after discussion

* include feedback

* fix modules integration tests

* fix modules integration tests

* fix event bus local
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-08-28 16:46:40 +02:00
1 parent 6cfe9bd874
commit 5bec38538a
27 files changed
+915 -465

No files matched your search

@@ -1,10 +1,10 @@
import { IProductModuleService } from "@medusajs/types"
import {
CommonEvents,
composeMessage,
Modules,
ProductEvents,
ProductStatus,
composeMessage,
} from "@medusajs/utils"
import { Product, ProductCategory } from "@models"
import {
@@ -410,14 +410,19 @@ moduleIntegrationTestRunner<IProductModuleService>({
})
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,
}),
])
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 () => {
@@ -505,14 +510,19 @@ moduleIntegrationTestRunner<IProductModuleService>({
})
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,
}),
])
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 () => {
@@ -678,17 +688,22 @@ moduleIntegrationTestRunner<IProductModuleService>({
await service.deleteProductCategories([productCategoryOne.id])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
data: { id: productCategoryOne.id },
name: "product-category.deleted",
metadata: {
action: "",
object: "",
source: "",
},
}),
])
expect(eventBusSpy).toHaveBeenCalledWith(
[
expect.objectContaining({
data: { id: productCategoryOne.id },
name: "product-category.deleted",
metadata: {
action: "",
object: "",
source: "",
},
}),
],
{
internal: true,
}
)
})
it("should throw an error when an id does not exist", async () => {
@@ -278,17 +278,22 @@ moduleIntegrationTestRunner<IProductModuleService>({
await service.deleteProductCollections([collectionId])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
{
name: "product-collection.deleted",
data: { id: collectionId },
metadata: {
action: "",
object: "",
source: "",
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
name: "product-collection.deleted",
data: { id: collectionId },
metadata: {
action: "",
object: "",
source: "",
},
},
},
])
],
{
internal: true,
}
)
})
})
@@ -306,12 +311,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
data: { id: collectionId },
name: "product-collection.updated",
},
],
{
data: { id: collectionId },
name: "product-collection.updated",
},
])
internal: true,
}
)
})
it("should update the value of the collection successfully", async () => {
@@ -502,12 +512,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
data: { id: collections[0].id },
name: "product-collection.created",
},
],
{
data: { id: collections[0].id },
name: "product-collection.created",
},
])
internal: true,
}
)
})
})
})
@@ -278,14 +278,19 @@ 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,
}),
])
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 () => {
@@ -320,14 +325,19 @@ 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,
}),
])
expect(eventBusEmitSpy).toHaveBeenCalledWith(
[
composeMessage(ProductEvents.PRODUCT_TAG_CREATED, {
data: { id: productTag[0].id },
object: "product_tag",
source: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
],
{
internal: true,
}
)
})
})
@@ -374,20 +384,25 @@ moduleIntegrationTestRunner<IProductModuleService>({
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,
}),
])
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,
}
)
})
})
})
@@ -212,14 +212,19 @@ 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,
}),
])
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 upsert the options of a variant successfully", async () => {
@@ -245,14 +250,19 @@ moduleIntegrationTestRunner<IProductModuleService>({
)
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,
}),
])
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 () => {
@@ -319,14 +329,19 @@ 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,
}),
])
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 () => {
@@ -374,12 +374,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
await service.upsertProducts([updateData])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
name: "product.updated",
data: { id: productOne.id },
},
],
{
name: "product.updated",
data: { id: productOne.id },
},
])
internal: true,
}
)
})
it("should add relationships to a product", async () => {
@@ -724,12 +729,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
const products = await service.createProducts([data])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
name: "product.created",
data: { id: products[0].id },
},
],
{
name: "product.created",
data: { id: products[0].id },
},
])
internal: true,
}
)
})
})
@@ -814,12 +824,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
await service.softDeleteProducts([products[0].id])
expect(eventBusSpy).toHaveBeenCalledWith([
expect(eventBusSpy).toHaveBeenCalledWith(
[
{
name: "product.created",
data: { id: products[0].id },
},
],
{
name: "product.created",
data: { id: products[0].id },
},
])
internal: true,
}
)
})
})