feat(modules-sdk, types, user, utils):init user module events (#6431)
* init user module events * refactor utils * undo test script update * fix feedback * add eventbus service to module test-runner * add injected dependencies * move events to utils * use const eventname in tests * rm withTransaction
This commit is contained in:
@@ -11,4 +11,5 @@ export * as ProductUtils from "./product"
|
||||
export * as PromotionUtils from "./promotion"
|
||||
export * as SearchUtils from "./search"
|
||||
export * as ShippingProfileUtils from "./shipping"
|
||||
export * as UserUtils from "./user"
|
||||
export * as ApiKeyUtils from "./api-key"
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import {
|
||||
EventBusTypes,
|
||||
IMessageAggregator,
|
||||
Message,
|
||||
MessageAggregatorFormat,
|
||||
} from "@medusajs/types"
|
||||
|
||||
import { buildEventMessages } from "./build-event-messages"
|
||||
|
||||
export class MessageAggregator implements IMessageAggregator {
|
||||
private messages: Message[]
|
||||
|
||||
@@ -23,6 +26,15 @@ export class MessageAggregator implements IMessageAggregator {
|
||||
}
|
||||
}
|
||||
|
||||
saveRawMessageData<T>(
|
||||
messageData:
|
||||
| EventBusTypes.MessageFormat<T>
|
||||
| EventBusTypes.MessageFormat<T>[],
|
||||
options?: Record<string, unknown>
|
||||
): void {
|
||||
this.save(buildEventMessages(messageData, options))
|
||||
}
|
||||
|
||||
getMessages(format?: MessageAggregatorFormat): {
|
||||
[group: string]: Message[]
|
||||
} {
|
||||
|
||||
@@ -19,6 +19,7 @@ export * from "./search"
|
||||
export * from "./shipping"
|
||||
export * from "./totals"
|
||||
export * from "./totals/big-number"
|
||||
export * from "./user"
|
||||
export * from "./api-key"
|
||||
|
||||
export const MedusaModuleType = Symbol.for("MedusaModule")
|
||||
|
||||
@@ -474,6 +474,19 @@ export function abstractModuleServiceFactory<
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
protected async emitEvents_(groupedEvents) {
|
||||
if (!this.eventBusModuleService_ || !groupedEvents) {
|
||||
return
|
||||
}
|
||||
|
||||
const promises: Promise<void>[] = []
|
||||
for (const group of Object.keys(groupedEvents)) {
|
||||
promises.push(this.eventBusModuleService_?.emit(groupedEvents[group]))
|
||||
}
|
||||
|
||||
await Promise.all(promises)
|
||||
}
|
||||
}
|
||||
|
||||
const mainModelMethods = buildMethodNamesFromModel(mainModel, false)
|
||||
|
||||
26
packages/utils/src/modules-sdk/decorators/emit-events.ts
Normal file
26
packages/utils/src/modules-sdk/decorators/emit-events.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { MessageAggregator } from "../../event-bus"
|
||||
import { InjectIntoContext } from "./inject-into-context"
|
||||
|
||||
export function EmitEvents() {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string | symbol,
|
||||
descriptor: any
|
||||
): void {
|
||||
const aggregator = new MessageAggregator()
|
||||
InjectIntoContext({
|
||||
messageAggregator: () => aggregator,
|
||||
})(target, propertyKey, descriptor)
|
||||
|
||||
const original = descriptor.value
|
||||
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const result = await original.apply(this, args)
|
||||
|
||||
await target.emitEvents_.apply(this, [aggregator.getMessages()])
|
||||
|
||||
aggregator.clearMessages()
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,5 @@ export * from "./context-parameter"
|
||||
export * from "./inject-manager"
|
||||
export * from "./inject-shared-context"
|
||||
export * from "./inject-transaction-manager"
|
||||
export * from "./inject-into-context"
|
||||
export * from "./emit-events"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { MessageAggregator } from "../../event-bus"
|
||||
|
||||
export function InjectIntoContext(
|
||||
properties: Record<string, unknown | Function>
|
||||
): MethodDecorator {
|
||||
|
||||
8
packages/utils/src/user/events.ts
Normal file
8
packages/utils/src/user/events.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { CommonEvents } from "../event-bus"
|
||||
|
||||
export const UserEvents = {
|
||||
created: "user." + CommonEvents.CREATED,
|
||||
updated: "user." + CommonEvents.UPDATED,
|
||||
invite_created: "invite." + CommonEvents.CREATED,
|
||||
invite_updated: "invite." + CommonEvents.UPDATED,
|
||||
}
|
||||
1
packages/utils/src/user/index.ts
Normal file
1
packages/utils/src/user/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./events"
|
||||
Reference in New Issue
Block a user