fix(utils): Uppercase event action name (#7822)
Currently, the keys of the events contain a lowercased action, e.g. `INVENTORY_ITEM_created`, which is breaking our usage across multiple modules
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
||||
import { buildEventNamesFromEntityName } from "../utils"
|
||||
|
||||
describe("MessageAggregator", function () {
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks
|
||||
})
|
||||
|
||||
it("should create event names from entity", function () {
|
||||
const eventBaseNames: ["inventoryItem", "reservationItem"] = [
|
||||
"inventoryItem",
|
||||
"reservationItem",
|
||||
]
|
||||
|
||||
const InventoryEvents = buildEventNamesFromEntityName(
|
||||
eventBaseNames,
|
||||
"inventory"
|
||||
)
|
||||
|
||||
expect(InventoryEvents).toEqual({
|
||||
INVENTORY_ITEM_CREATED: "inventory.inventory-item.created",
|
||||
INVENTORY_ITEM_UPDATED: "inventory.inventory-item.updated",
|
||||
INVENTORY_ITEM_DELETED: "inventory.inventory-item.deleted",
|
||||
INVENTORY_ITEM_RESTORED: "inventory.inventory-item.restored",
|
||||
INVENTORY_ITEM_ATTACHED: "inventory.inventory-item.attached",
|
||||
INVENTORY_ITEM_DETACHED: "inventory.inventory-item.detached",
|
||||
RESERVATION_ITEM_CREATED: "inventory.reservation-item.created",
|
||||
RESERVATION_ITEM_UPDATED: "inventory.reservation-item.updated",
|
||||
RESERVATION_ITEM_DELETED: "inventory.reservation-item.deleted",
|
||||
RESERVATION_ITEM_RESTORED: "inventory.reservation-item.restored",
|
||||
RESERVATION_ITEM_ATTACHED: "inventory.reservation-item.attached",
|
||||
RESERVATION_ITEM_DETACHED: "inventory.reservation-item.detached",
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { KebabCase, SnakeCase } from "@medusajs/types"
|
||||
import { camelToSnakeCase, kebabCase, lowerCaseFirst } from "../common"
|
||||
import { CommonEvents } from "./common-events"
|
||||
import { KebabCase, SnakeCase } from "@medusajs/types"
|
||||
|
||||
type ReturnType<TNames extends string[]> = {
|
||||
[K in TNames[number] as `${Uppercase<
|
||||
@@ -56,7 +56,8 @@ export function buildEventNamesFromEntityName<TNames extends string[]>(
|
||||
const kebabCaseName = lowerCaseFirst(kebabCase(name))
|
||||
|
||||
for (const event of Object.values(CommonEvents) as string[]) {
|
||||
events[`${snakedCaseName}_${event}`] = `${
|
||||
const upperCasedEvent = event.toUpperCase()
|
||||
events[`${snakedCaseName}_${upperCasedEvent}`] = `${
|
||||
prefix ? prefix + "." : ""
|
||||
}${kebabCaseName}.${event}` as `${KebabCase<typeof name>}.${typeof event}`
|
||||
}
|
||||
|
||||
@@ -46,13 +46,17 @@ export function eventBuilderFactory({
|
||||
const aggregator = sharedContext.messageAggregator!
|
||||
const messages: EventBusTypes.RawMessageFormat[] = []
|
||||
|
||||
// The event enums contains event formatted like so [object]_[action] e.g. PRODUCT_CREATED
|
||||
// We expect the keys of events to be fully uppercased
|
||||
const eventName = eventsEnum[`${object.toUpperCase()}_${action.toUpperCase()}`]
|
||||
|
||||
data.forEach((dataItem) => {
|
||||
messages.push({
|
||||
source,
|
||||
action,
|
||||
context: sharedContext,
|
||||
data: { id: dataItem.id },
|
||||
eventName: eventsEnum[`${object}_${action}`],
|
||||
eventName,
|
||||
object,
|
||||
})
|
||||
})
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { IProductModuleService } from "@medusajs/types"
|
||||
import {
|
||||
Modules,
|
||||
CommonEvents,
|
||||
Modules,
|
||||
ProductEvents,
|
||||
ProductStatus,
|
||||
composeMessage,
|
||||
|
||||
Reference in New Issue
Block a user