Skip to content

Commit

Permalink
feat(core): Add OrderPlacedEvent
Browse files Browse the repository at this point in the history
Relates to #1219
  • Loading branch information
michaelbromley committed Dec 2, 2021
1 parent 204f699 commit c1465dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/core/src/event-bus/events/order-placed-event.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -42,6 +44,7 @@ export class OrderStateMachine {
private stockMovementService: StockMovementService,
private historyService: HistoryService,
private promotionService: PromotionService,
private eventBus: EventBus,
) {
this.config = this.initConfig();
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c1465dc

Please sign in to comment.