|
|
@@ -55,6 +55,7 @@ import { CustomerService } from './customer.service';
|
|
|
import { HistoryService } from './history.service';
|
|
|
import { PaymentMethodService } from './payment-method.service';
|
|
|
import { ProductVariantService } from './product-variant.service';
|
|
|
+import { PromotionService } from './promotion.service';
|
|
|
import { StockMovementService } from './stock-movement.service';
|
|
|
|
|
|
export class OrderService {
|
|
|
@@ -74,6 +75,7 @@ export class OrderService {
|
|
|
private stockMovementService: StockMovementService,
|
|
|
private refundStateMachine: RefundStateMachine,
|
|
|
private historyService: HistoryService,
|
|
|
+ private promotionService: PromotionService,
|
|
|
) {}
|
|
|
|
|
|
findAll(ctx: RequestContext, options?: ListQueryOptions<Order>): Promise<PaginatedList<Order>> {
|
|
|
@@ -191,6 +193,7 @@ export class OrderService {
|
|
|
code: generatePublicId(),
|
|
|
state: this.orderStateMachine.getInitialState(),
|
|
|
lines: [],
|
|
|
+ couponCodes: [],
|
|
|
shippingAddress: {},
|
|
|
billingAddress: {},
|
|
|
pendingAdjustments: [],
|
|
|
@@ -291,6 +294,19 @@ export class OrderService {
|
|
|
return updatedOrder;
|
|
|
}
|
|
|
|
|
|
+ async applyCouponCode(ctx: RequestContext, orderId: ID, couponCode: string) {
|
|
|
+ await this.promotionService.validateCouponCode(couponCode);
|
|
|
+ const order = await this.getOrderOrThrow(ctx, orderId);
|
|
|
+ order.couponCodes.push(couponCode);
|
|
|
+ return this.applyPriceAdjustments(ctx, order);
|
|
|
+ }
|
|
|
+
|
|
|
+ async removeCouponCode(ctx: RequestContext, orderId: ID, couponCode: string) {
|
|
|
+ const order = await this.getOrderOrThrow(ctx, orderId);
|
|
|
+ order.couponCodes = order.couponCodes.filter(cc => cc !== couponCode);
|
|
|
+ return this.applyPriceAdjustments(ctx, order);
|
|
|
+ }
|
|
|
+
|
|
|
getNextOrderStates(order: Order): OrderState[] {
|
|
|
return this.orderStateMachine.getNextStates(order);
|
|
|
}
|
|
|
@@ -719,7 +735,7 @@ export class OrderService {
|
|
|
*/
|
|
|
private async applyPriceAdjustments(ctx: RequestContext, order: Order): Promise<Order> {
|
|
|
const promotions = await this.connection.getRepository(Promotion).find({
|
|
|
- where: { enabled: true },
|
|
|
+ where: { enabled: true, deletedAt: null },
|
|
|
order: { priorityScore: 'ASC' },
|
|
|
});
|
|
|
order = await this.orderCalculator.applyPriceAdjustments(ctx, order, promotions);
|