feat: Event emitting part 1/N (Fulfillment) (#7391)
**What** Add support for event emitting in the fulfillment module **NOTE** It does not include the review of the events for the abstract module factory if the method is not implemented in the module itself and rely on the default implementation
This commit is contained in:
committed by
GitHub
parent
28d2a5347a
commit
9608bf06ef
@@ -1,16 +1,17 @@
|
||||
import { MessageAggregator } from "../../event-bus"
|
||||
import { InjectIntoContext } from "./inject-into-context"
|
||||
import {MessageAggregatorFormat} from "@medusajs/types";
|
||||
import { MessageAggregatorFormat } from "@medusajs/types"
|
||||
|
||||
export function EmitEvents(options: MessageAggregatorFormat = {} as MessageAggregatorFormat) {
|
||||
export function EmitEvents(
|
||||
options: MessageAggregatorFormat = {} as MessageAggregatorFormat
|
||||
) {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string | symbol,
|
||||
descriptor: any
|
||||
): void {
|
||||
const aggregator = new MessageAggregator()
|
||||
InjectIntoContext({
|
||||
messageAggregator: () => aggregator,
|
||||
messageAggregator: () => new MessageAggregator(),
|
||||
})(target, propertyKey, descriptor)
|
||||
|
||||
const original = descriptor.value
|
||||
@@ -18,6 +19,17 @@ export function EmitEvents(options: MessageAggregatorFormat = {} as MessageAggre
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const result = await original.apply(this, args)
|
||||
|
||||
if (!target.emitEvents_) {
|
||||
const logger = Object.keys(this.__container__ ?? {}).includes("logger")
|
||||
? this.__container__.logger
|
||||
: console
|
||||
logger.warn(
|
||||
`No emitEvents_ method found on ${target.constructor.name}. No events emitted. To be able to use the @EmitEvents() you need to have the emitEvents_ method implemented in the class.`
|
||||
)
|
||||
}
|
||||
|
||||
const argIndex = target.MedusaContextIndex_[propertyKey]
|
||||
const aggregator = args[argIndex].messageAggregator as MessageAggregator
|
||||
await target.emitEvents_.apply(this, [aggregator.getMessages(options)])
|
||||
|
||||
aggregator.clearMessages()
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { MessageAggregator } from "../../event-bus"
|
||||
|
||||
export function InjectIntoContext(
|
||||
properties: Record<string, unknown | Function>
|
||||
): MethodDecorator {
|
||||
|
||||
67
packages/core/utils/src/modules-sdk/event-builder-factory.ts
Normal file
67
packages/core/utils/src/modules-sdk/event-builder-factory.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { Context, EventBusTypes } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* Factory function to create event builders for different entities
|
||||
*
|
||||
* @example
|
||||
* const createdFulfillment = eventBuilderFactory({
|
||||
* service: Modules.FULFILLMENT,
|
||||
* action: CommonEvents.CREATED,
|
||||
* object: "fulfillment",
|
||||
* eventsEnum: FulfillmentEvents,
|
||||
* })
|
||||
*
|
||||
* createdFulfillment({
|
||||
* data,
|
||||
* sharedContext,
|
||||
* })
|
||||
*
|
||||
* @param isMainEntity
|
||||
* @param action
|
||||
* @param object
|
||||
* @param eventsEnum
|
||||
* @param service
|
||||
*/
|
||||
export function eventBuilderFactory({
|
||||
isMainEntity,
|
||||
action,
|
||||
object,
|
||||
eventsEnum,
|
||||
service,
|
||||
}: {
|
||||
isMainEntity?: boolean
|
||||
action: string
|
||||
object: string
|
||||
eventsEnum: Record<string, string>
|
||||
service: string
|
||||
}) {
|
||||
return function ({
|
||||
data,
|
||||
sharedContext,
|
||||
}: {
|
||||
data: { id: string }[]
|
||||
sharedContext: Context
|
||||
}) {
|
||||
if (!data.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const aggregator = sharedContext.messageAggregator!
|
||||
const messages: EventBusTypes.RawMessageFormat[] = []
|
||||
|
||||
data.forEach((dataItem) => {
|
||||
messages.push({
|
||||
service,
|
||||
action,
|
||||
context: sharedContext,
|
||||
data: { id: dataItem.id },
|
||||
eventName: isMainEntity
|
||||
? eventsEnum[action]
|
||||
: eventsEnum[`${object}_${action}`],
|
||||
object,
|
||||
})
|
||||
})
|
||||
|
||||
aggregator.saveRawMessageData(messages)
|
||||
}
|
||||
}
|
||||
@@ -9,3 +9,4 @@ export * from "./migration-scripts"
|
||||
export * from "./internal-module-service-factory"
|
||||
export * from "./abstract-module-service-factory"
|
||||
export * from "./definition"
|
||||
export * from "./event-builder-factory"
|
||||
|
||||
Reference in New Issue
Block a user