feat: Introduce order.placed event (#7682)
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import { composeMessage, ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { createStep, StepExecutionContext } from "@medusajs/workflows-sdk"
|
||||
import { EventBusTypes, IEventBusModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||
import { StepExecutionContext, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type Input = {
|
||||
eventName: string
|
||||
source: string
|
||||
object: string
|
||||
action?: string
|
||||
options?: Record<string, any>
|
||||
data: (
|
||||
context: StepExecutionContext
|
||||
) => Promise<Record<any, any>> | Record<any, any>
|
||||
metadata?: Record<string, any>
|
||||
data:
|
||||
| ((context: StepExecutionContext) => Promise<Record<any, any>>)
|
||||
| Record<any, any>
|
||||
}
|
||||
|
||||
export const emitEventStepId = "emit-event-step"
|
||||
@@ -22,18 +21,27 @@ export const emitEventStep = createStep(
|
||||
|
||||
const { container } = context
|
||||
|
||||
const eventBus = container.resolve(ModuleRegistrationName.EVENT_BUS)
|
||||
const eventBus: IEventBusModuleService = container.resolve(
|
||||
ModuleRegistrationName.EVENT_BUS
|
||||
)
|
||||
|
||||
const data_ =
|
||||
typeof input.data === "function" ? await input.data(context) : input.data
|
||||
const message = composeMessage(input.eventName, {
|
||||
|
||||
const metadata: EventBusTypes.MessageBody["metadata"] = {
|
||||
...input.metadata,
|
||||
}
|
||||
|
||||
if (context.eventGroupId) {
|
||||
metadata.eventGroupId = context.eventGroupId
|
||||
}
|
||||
|
||||
const message = {
|
||||
eventName: input.eventName,
|
||||
data: data_,
|
||||
action: input.action ?? "",
|
||||
object: input.object,
|
||||
source: input.source,
|
||||
options: input.options,
|
||||
context,
|
||||
})
|
||||
metadata,
|
||||
}
|
||||
|
||||
await eventBus.emit(message)
|
||||
},
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import { OrderDTO } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/utils"
|
||||
import { Modules, OrderEvents } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
|
||||
import {
|
||||
createRemoteLinkStep,
|
||||
emitEventStep,
|
||||
useRemoteQueryStep,
|
||||
} from "../../../common"
|
||||
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
|
||||
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
|
||||
import { reserveInventoryStep } from "../steps/reserve-inventory"
|
||||
@@ -88,6 +92,8 @@ export const completeCartWorkflow = createWorkflow(
|
||||
},
|
||||
])
|
||||
|
||||
emitEventStep({ eventName: OrderEvents.placed, data: { id: order.id } })
|
||||
|
||||
return order
|
||||
}
|
||||
)
|
||||
|
||||
5
packages/core/utils/src/order/events.ts
Normal file
5
packages/core/utils/src/order/events.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const OrderEvents = {
|
||||
placed: "order.placed",
|
||||
canceled: "order.canceled",
|
||||
completed: "order.completed",
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
export * from "./events"
|
||||
export * from "./status"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user