diff --git a/packages/core/src/event-bus/events/order-placed-event.ts b/packages/core/src/event-bus/events/order-placed-event.ts new file mode 100644 index 0000000000..a82709dede --- /dev/null +++ b/packages/core/src/event-bus/events/order-placed-event.ts @@ -0,0 +1,26 @@ +import { RequestContext } from '../../api/common/request-context'; +import { Order } from '../../entity/order/order.entity'; +import { OrderState } from '../../service/helpers/order-state-machine/order-state'; +import { VendureEvent } from '../vendure-event'; + +/** + * @description + * This event is fired whenever an {@link Order} is set as "placed", which by default is + * when it transitions from 'ArrangingPayment' to either 'PaymentAuthorized' or 'PaymentSettled'. + * + * Note that the exact point that it is set as "placed" can be configured according to the + * {@link OrderPlacedStrategy}. + * + * @docsCategory events + * @docsPage Event Types + */ +export class OrderPlacedEvent extends VendureEvent { + constructor( + public fromState: OrderState, + public toState: OrderState, + public ctx: RequestContext, + public order: Order, + ) { + super(); + } +} diff --git a/packages/core/src/event-bus/events/order-state-transition-event.ts b/packages/core/src/event-bus/events/order-state-transition-event.ts index 241910d801..1a65dc9832 100644 --- a/packages/core/src/event-bus/events/order-state-transition-event.ts +++ b/packages/core/src/event-bus/events/order-state-transition-event.ts @@ -1,5 +1,4 @@ import { RequestContext } from '../../api/common/request-context'; -import { Customer } from '../../entity/customer/customer.entity'; import { Order } from '../../entity/order/order.entity'; import { OrderState } from '../../service/helpers/order-state-machine/order-state'; import { VendureEvent } from '../vendure-event'; diff --git a/packages/core/src/service/helpers/order-state-machine/order-state-machine.ts b/packages/core/src/service/helpers/order-state-machine/order-state-machine.ts index 797e3f9453..73322b08d7 100644 --- a/packages/core/src/service/helpers/order-state-machine/order-state-machine.ts +++ b/packages/core/src/service/helpers/order-state-machine/order-state-machine.ts @@ -16,6 +16,8 @@ import { OrderModification } from '../../../entity/order-modification/order-modi import { Order } from '../../../entity/order/order.entity'; import { Payment } from '../../../entity/payment/payment.entity'; import { ProductVariant } from '../../../entity/product-variant/product-variant.entity'; +import { OrderPlacedEvent } from '../../../event-bus/events/order-placed-event'; +import { EventBus } from '../../../event-bus/index'; import { HistoryService } from '../../services/history.service'; import { PromotionService } from '../../services/promotion.service'; import { StockMovementService } from '../../services/stock-movement.service'; @@ -42,6 +44,7 @@ export class OrderStateMachine { private stockMovementService: StockMovementService, private historyService: HistoryService, private promotionService: PromotionService, + private eventBus: EventBus, ) { this.config = this.initConfig(); } @@ -179,6 +182,7 @@ export class OrderStateMachine { order.active = false; order.orderPlacedAt = new Date(); await this.promotionService.addPromotionsToOrder(ctx, order); + this.eventBus.publish(new OrderPlacedEvent(fromState, toState, ctx, order)); } } const shouldAllocateStock = await stockAllocationStrategy.shouldAllocateStock(