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:
1 parent
96fb7a962e
commit
25210369d9
4 files changed
+43
-4
No files matched your search
+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 { camelToSnakeCase, kebabCase, lowerCaseFirst } from "../common"
|
||||||
import { CommonEvents } from "./common-events"
|
import { CommonEvents } from "./common-events"
|
||||||
import { KebabCase, SnakeCase } from "@medusajs/types"
|
|
||||||
|
|
||||||
type ReturnType<TNames extends string[]> = {
|
type ReturnType<TNames extends string[]> = {
|
||||||
[K in TNames[number] as `${Uppercase<
|
[K in TNames[number] as `${Uppercase<
|
||||||
@@ -56,7 +56,8 @@ export function buildEventNamesFromEntityName<TNames extends string[]>(
|
|||||||
const kebabCaseName = lowerCaseFirst(kebabCase(name))
|
const kebabCaseName = lowerCaseFirst(kebabCase(name))
|
||||||
|
|
||||||
for (const event of Object.values(CommonEvents) as string[]) {
|
for (const event of Object.values(CommonEvents) as string[]) {
|
||||||
events[`${snakedCaseName}_${event}`] = `${
|
const upperCasedEvent = event.toUpperCase()
|
||||||
|
events[`${snakedCaseName}_${upperCasedEvent}`] = `${
|
||||||
prefix ? prefix + "." : ""
|
prefix ? prefix + "." : ""
|
||||||
}${kebabCaseName}.${event}` as `${KebabCase<typeof name>}.${typeof event}`
|
}${kebabCaseName}.${event}` as `${KebabCase<typeof name>}.${typeof event}`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,13 +46,17 @@ export function eventBuilderFactory({
|
|||||||
const aggregator = sharedContext.messageAggregator!
|
const aggregator = sharedContext.messageAggregator!
|
||||||
const messages: EventBusTypes.RawMessageFormat[] = []
|
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) => {
|
data.forEach((dataItem) => {
|
||||||
messages.push({
|
messages.push({
|
||||||
source,
|
source,
|
||||||
action,
|
action,
|
||||||
context: sharedContext,
|
context: sharedContext,
|
||||||
data: { id: dataItem.id },
|
data: { id: dataItem.id },
|
||||||
eventName: eventsEnum[`${object}_${action}`],
|
eventName,
|
||||||
object,
|
object,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
import { IProductModuleService } from "@medusajs/types"
|
import { IProductModuleService } from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
Modules,
|
|
||||||
CommonEvents,
|
CommonEvents,
|
||||||
|
Modules,
|
||||||
ProductEvents,
|
ProductEvents,
|
||||||
ProductStatus,
|
ProductStatus,
|
||||||
composeMessage,
|
composeMessage,
|
||||||
|
|||||||
Reference in new issue
Block a user