feat: emit product tags events (#7636)

**What**
emit product tags events

**note**
Stil does not take into consideration non overriden method, delete and soft delete emits event under the hood and we will have to migrate that at the end
This commit is contained in:
Adrien de Peretti
2024-06-06 17:14:40 +02:00
committed by GitHub
parent 3f62bfad5a
commit 9576de2be4
2 changed files with 122 additions and 3 deletions

View File

@@ -1,14 +1,32 @@
import { Modules } from "@medusajs/modules-sdk"
import { IProductModuleService } from "@medusajs/types"
import { ProductStatus } from "@medusajs/utils"
import {
CommonEvents,
composeMessage,
ProductEvents,
ProductStatus,
} from "@medusajs/utils"
import { Product, ProductTag } from "@models"
import { moduleIntegrationTestRunner } from "medusa-test-utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "medusa-test-utils"
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
let tagTwo: ProductTag
@@ -255,6 +273,16 @@ moduleIntegrationTestRunner<IProductModuleService>({
const productTag = await service.retrieveTag(tagId)
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",
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
])
})
it("should throw an error when an id does not exist", async () => {
@@ -276,7 +304,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
describe("createTags", () => {
it("should create a tag successfully", async () => {
const res = await service.createTags([
await service.createTags([
{
value: "UK",
},
@@ -287,6 +315,76 @@ 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",
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
])
})
})
describe("upsertTags", () => {
it("should upsert tags successfully", async () => {
await service.createTags([
{
value: "UK",
},
])
let productTags = await service.listTags({
value: "UK",
})
const tagsData = [
{
...productTags[0],
value: "updated",
},
{
value: "new",
},
]
jest.clearAllMocks()
await service.upsertTags(tagsData)
productTags = await service.listTags()
expect(productTags).toEqual(
expect.arrayContaining([
expect.objectContaining({
value: "updated",
}),
expect.objectContaining({
value: "new",
}),
])
)
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",
service: Modules.PRODUCT,
action: CommonEvents.CREATED,
}),
composeMessage(ProductEvents.product_tag_updated, {
data: { id: updatedTag.id },
object: "product_tag",
service: Modules.PRODUCT,
action: CommonEvents.UPDATED,
}),
])
})
})
})

View File

@@ -422,6 +422,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductTagDTO>
@InjectManager("baseRepository_")
@EmitEvents()
async createTags(
data: ProductTypes.CreateProductTagDTO[] | ProductTypes.CreateProductTagDTO,
@MedusaContext() sharedContext: Context = {}
@@ -434,6 +435,11 @@ export default class ProductModuleService<
ProductTypes.ProductTagDTO[]
>(tags)
eventBuilders.createdProductTag({
data: createdTags,
sharedContext,
})
return Array.isArray(data) ? createdTags : createdTags[0]
}
@@ -447,6 +453,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductTagDTO>
@InjectTransactionManager("baseRepository_")
@EmitEvents()
async upsertTags(
data: ProductTypes.UpsertProductTagDTO[] | ProductTypes.UpsertProductTagDTO,
@MedusaContext() sharedContext: Context = {}
@@ -462,9 +469,17 @@ export default class ProductModuleService<
if (forCreate.length) {
created = await this.productTagService_.create(forCreate, sharedContext)
eventBuilders.createdProductTag({
data: created,
sharedContext,
})
}
if (forUpdate.length) {
updated = await this.productTagService_.update(forUpdate, sharedContext)
eventBuilders.updatedProductTag({
data: updated,
sharedContext,
})
}
const result = [...created, ...updated]
@@ -487,6 +502,7 @@ export default class ProductModuleService<
): Promise<ProductTypes.ProductTagDTO[]>
@InjectManager("baseRepository_")
@EmitEvents()
async updateTags(
idOrSelector: string | ProductTypes.FilterableProductTagProps,
data: ProductTypes.UpdateProductTagDTO,
@@ -519,6 +535,11 @@ export default class ProductModuleService<
ProductTypes.ProductTagDTO[]
>(tags)
eventBuilders.updatedProductTag({
data: updatedTags,
sharedContext,
})
return isString(idOrSelector) ? updatedTags[0] : updatedTags
}