1 import { KebabCase, SnakeCase } from "@medusajs/types"
2 import { camelToSnakeCase, kebabCase, lowerCaseFirst } from "../common"
3 import { CommonEvents } from "./common-events"
4
5 type ReturnType<TNames extends string[], TPrefix extends string = ""> = {
6 [K in TNames[number] as `${Uppercase<
7 SnakeCase<K & string>
8 >}_CREATED`]: TPrefix extends ""
9 ? `${KebabCase<K & string>}.created`
10 : `${TPrefix}.${KebabCase<K & string>}.created`
11 } & {
12 [K in TNames[number] as `${Uppercase<
13 SnakeCase<K & string>
14 >}_UPDATED`]: TPrefix extends ""
15 ? `${KebabCase<K & string>}.updated`
16 : `${TPrefix}.${KebabCase<K & string>}.updated`
17 } & {
18 [K in TNames[number] as `${Uppercase<
19 SnakeCase<K & string>
20 >}_DELETED`]: TPrefix extends ""
21 ? `${KebabCase<K & string>}.deleted`
22 : `${TPrefix}.${KebabCase<K & string>}.deleted`
23 } & {
24 [K in TNames[number] as `${Uppercase<
25 SnakeCase<K & string>
26 >}_RESTORED`]: TPrefix extends ""
27 ? `${KebabCase<K & string>}.restored`
28 : `${TPrefix}.${KebabCase<K & string>}.restored`
29 } & {
30 [K in TNames[number] as `${Uppercase<
31 SnakeCase<K & string>
32 >}_ATTACHED`]: TPrefix extends ""
33 ? `${KebabCase<K & string>}.attached`
34 : `${TPrefix}.${KebabCase<K & string>}.attached`
35 } & {
36 [K in TNames[number] as `${Uppercase<
37 SnakeCase<K & string>
38 >}_DETACHED`]: TPrefix extends ""
39 ? `${KebabCase<K & string>}.detached`
40 : `${TPrefix}.${KebabCase<K & string>}.detached`
41 }
42
43 /**
44 * Build a conventional event name from the object name and the action and the prefix if provided
45 * @param prefix
46 * @param objectName
47 * @param action
48 */
49 export function buildModuleResourceEventName({
50 prefix,
51 objectName,
52 action,
53 }: {
54 prefix?: string
55 objectName: string
56 action: string
57 }): string {
58 const kebabCaseName = lowerCaseFirst(kebabCase(objectName))
59 return `${prefix ? `${prefix}.` : ""}${kebabCaseName}.${action}`
60 }
61
62 /**
63 * From the given strings it will produce the event names accordingly.
64 * the result will look like:
65 * input: 'serviceZone'
66 * output: {
67 * SERVICE_ZONE_CREATED: 'service-zone.created',
68 * SERVICE_ZONE_UPDATED: 'service-zone.updated',
69 * SERVICE_ZONE_DELETED: 'service-zone.deleted',
70 * SERVICE_ZONE_RESTORED: 'service-zone.restored',
71 * SERVICE_ZONE_ATTACHED: 'service-zone.attached',
72 * SERVICE_ZONE_DETACHED: 'service-zone.detached',
73 * ...
74 * }
75 *
76 * @param names
77 * @param prefix
78 */
79 export function buildEventNamesFromEntityName<
80 TNames extends string[],
81 TPrefix extends string = ""
82 >(names: TNames, prefix?: TPrefix): ReturnType<TNames, TPrefix> {
83 const events = {}
84
85 for (let i = 0; i < names.length; i++) {
86 const name = names[i]
87 const snakedCaseName = camelToSnakeCase(name).toUpperCase()
88
89 for (const event of Object.values(CommonEvents) as string[]) {
90 const upperCasedEvent = event.toUpperCase()
91 events[`${snakedCaseName}_${upperCasedEvent}`] =
92 buildModuleResourceEventName({
93 prefix,
94 objectName: name,
95 action: event,
96 }) as `${KebabCase<typeof name>}.${typeof event}`
97 }
98 }
99
100 return events as ReturnType<TNames, TPrefix>
101 }
102
103 export const EventPriority = {
104 CRITICAL: 10,
105 HIGH: 50,
106 DEFAULT: 100,
107 LOW: 500,
108 /**
109 * Lowest priority value supported by BullMQ (2^21)
110 * Internal events use this priority to ensure they don't block critical business events
111 */
112 LOWEST: 2_097_152,
113 } as const