fix(inventory): Wrong event emitted on reservation update (#11714)

* fix(inventory): Wrong event emitted on reservation update

* fix(inventory): Wrong event emitted on reservation update

* Create tall-mice-smile.md
This commit is contained in:
Adrien de Peretti
2025-03-04 14:10:11 +01:00
committed by GitHub
parent d172ad8ebc
commit cad8b40c13
3 changed files with 64 additions and 15 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/inventory": patch
---
fix(inventory): Wrong event emitted on reservation update

View File

@@ -1,12 +1,25 @@
import { IInventoryService, InventoryItemDTO } from "@medusajs/framework/types"
import { BigNumber, Module, Modules } from "@medusajs/framework/utils"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import {
BigNumber,
CommonEvents,
composeMessage,
InventoryEvents,
Module,
Modules,
} from "@medusajs/framework/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
import { InventoryModuleService } from "../../src/services"
jest.setTimeout(100000)
moduleIntegrationTestRunner<IInventoryService>({
moduleName: Modules.INVENTORY,
injectedDependencies: {
[Modules.EVENT_BUS]: new MockEventBusService(),
},
testSuite: ({ service }) => {
describe("Inventory Module Service", () => {
it(`should export the appropriate linkable configuration`, () => {
@@ -414,6 +427,8 @@ moduleIntegrationTestRunner<IInventoryService>({
})
it("should update a reservationItem", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
const update = {
id: reservationItem.id,
quantity: 1,
@@ -422,6 +437,20 @@ moduleIntegrationTestRunner<IInventoryService>({
const updated = await service.updateReservationItems(update)
expect(updated).toEqual(expect.objectContaining(update))
expect(eventBusSpy).toHaveBeenNthCalledWith(
1,
[
composeMessage(InventoryEvents.RESERVATION_ITEM_UPDATED, {
data: { id: reservationItem.id },
object: "reservation-item",
source: Modules.INVENTORY,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
const update2 = {
id: reservationItem.id,
@@ -431,6 +460,20 @@ moduleIntegrationTestRunner<IInventoryService>({
const updated2 = await service.updateReservationItems(update2)
expect(updated2).toEqual(expect.objectContaining(update2))
expect(eventBusSpy).toHaveBeenNthCalledWith(
2,
[
composeMessage(InventoryEvents.RESERVATION_ITEM_UPDATED, {
data: { id: reservationItem.id },
object: "reservation-item",
source: Modules.INVENTORY,
action: CommonEvents.UPDATED,
}),
],
{
internal: true,
}
)
})
it("should adjust reserved_quantity of inventory level after updates increasing reserved quantity", async () => {

View File

@@ -26,6 +26,7 @@ import {
MedusaContext,
MedusaError,
MedusaService,
Modules,
partitionArray,
} from "@medusajs/framework/utils"
import { InventoryItem, InventoryLevel, ReservationItem } from "@models"
@@ -250,7 +251,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
created.map((reservationItem) => ({
eventName: InventoryEvents.RESERVATION_ITEM_CREATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.CREATED,
object: "reservation-item",
context,
@@ -352,7 +353,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
result.map((inventoryItem) => ({
eventName: InventoryEvents.INVENTORY_ITEM_CREATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.CREATED,
object: "inventory-item",
context,
@@ -408,7 +409,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
created.map((inventoryLevel) => ({
eventName: InventoryEvents.INVENTORY_LEVEL_CREATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.CREATED,
object: "inventory-level",
context,
@@ -464,7 +465,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
result.map((inventoryItem) => ({
eventName: InventoryEvents.INVENTORY_ITEM_UPDATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.UPDATED,
object: "inventory-item",
context,
@@ -505,7 +506,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
result[0].map((inventoryLevel) => ({
eventName: InventoryEvents.INVENTORY_LEVEL_DELETED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.DELETED,
object: "inventory-level",
context,
@@ -536,7 +537,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData({
eventName: InventoryEvents.INVENTORY_LEVEL_DELETED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.DELETED,
object: "inventory-level",
context,
@@ -581,7 +582,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
levels.map((inventoryLevel) => ({
eventName: InventoryEvents.INVENTORY_LEVEL_UPDATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.UPDATED,
object: "inventory-level",
context,
@@ -663,8 +664,8 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
result.map((reservationItem) => ({
eventName: InventoryEvents.INVENTORY_LEVEL_UPDATED,
source: this.constructor.name,
eventName: InventoryEvents.RESERVATION_ITEM_UPDATED,
source: Modules.INVENTORY,
action: CommonEvents.UPDATED,
object: "reservation-item",
context,
@@ -869,7 +870,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
reservations.map((reservationItem) => ({
eventName: InventoryEvents.RESERVATION_ITEM_DELETED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.DELETED,
object: "reservation-item",
context,
@@ -911,7 +912,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
reservations.map((reservationItem) => ({
eventName: InventoryEvents.RESERVATION_ITEM_DELETED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.DELETED,
object: "reservation-item",
context,
@@ -948,7 +949,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData(
reservations.map((reservationItem) => ({
eventName: InventoryEvents.RESERVATION_ITEM_CREATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.CREATED,
object: "reservation-item",
context,
@@ -1017,7 +1018,7 @@ export default class InventoryModuleService
context.messageAggregator?.saveRawMessageData({
eventName: InventoryEvents.INVENTORY_LEVEL_UPDATED,
source: this.constructor.name,
source: Modules.INVENTORY,
action: CommonEvents.UPDATED,
object: "inventory-level",
context,