feat: Introduce order.placed event (#7682)
This commit is contained in:
1 parent
6d43daa930
commit
73ca358606
4 files changed
+38
-17
No files matched your search
@@ -1,15 +1,14 @@
|
|||||||
import { composeMessage, ModuleRegistrationName } from "@medusajs/utils"
|
import { EventBusTypes, IEventBusModuleService } from "@medusajs/types"
|
||||||
import { createStep, StepExecutionContext } from "@medusajs/workflows-sdk"
|
import { ModuleRegistrationName } from "@medusajs/utils"
|
||||||
|
import { StepExecutionContext, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
type Input = {
|
type Input = {
|
||||||
eventName: string
|
eventName: string
|
||||||
source: string
|
|
||||||
object: string
|
|
||||||
action?: string
|
|
||||||
options?: Record<string, any>
|
options?: Record<string, any>
|
||||||
data: (
|
metadata?: Record<string, any>
|
||||||
context: StepExecutionContext
|
data:
|
||||||
) => Promise<Record<any, any>> | Record<any, any>
|
| ((context: StepExecutionContext) => Promise<Record<any, any>>)
|
||||||
|
| Record<any, any>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const emitEventStepId = "emit-event-step"
|
export const emitEventStepId = "emit-event-step"
|
||||||
@@ -22,18 +21,27 @@ export const emitEventStep = createStep(
|
|||||||
|
|
||||||
const { container } = context
|
const { container } = context
|
||||||
|
|
||||||
const eventBus = container.resolve(ModuleRegistrationName.EVENT_BUS)
|
const eventBus: IEventBusModuleService = container.resolve(
|
||||||
|
ModuleRegistrationName.EVENT_BUS
|
||||||
|
)
|
||||||
|
|
||||||
const data_ =
|
const data_ =
|
||||||
typeof input.data === "function" ? await input.data(context) : input.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_,
|
data: data_,
|
||||||
action: input.action ?? "",
|
|
||||||
object: input.object,
|
|
||||||
source: input.source,
|
|
||||||
options: input.options,
|
options: input.options,
|
||||||
context,
|
metadata,
|
||||||
})
|
}
|
||||||
|
|
||||||
await eventBus.emit(message)
|
await eventBus.emit(message)
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
import { OrderDTO } from "@medusajs/types"
|
import { OrderDTO } from "@medusajs/types"
|
||||||
import { Modules } from "@medusajs/utils"
|
import { Modules, OrderEvents } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
parallelize,
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
|
import {
|
||||||
|
createRemoteLinkStep,
|
||||||
|
emitEventStep,
|
||||||
|
useRemoteQueryStep,
|
||||||
|
} from "../../../common"
|
||||||
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
|
import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-payment-session"
|
||||||
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
|
import { createOrderFromCartStep, validateCartPaymentsStep } from "../steps"
|
||||||
import { reserveInventoryStep } from "../steps/reserve-inventory"
|
import { reserveInventoryStep } from "../steps/reserve-inventory"
|
||||||
@@ -88,6 +92,8 @@ export const completeCartWorkflow = createWorkflow(
|
|||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
|
emitEventStep({ eventName: OrderEvents.placed, data: { id: order.id } })
|
||||||
|
|
||||||
return order
|
return order
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -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"
|
export * from "./status"
|
||||||
|
|
||||||
Reference in new issue
Block a user