Jelajahi Sumber

fix(core): Fix coupon code removal not clearing adjustments

The removeCouponCode() method was capturing OrderLine references before
calling applyPriceAdjustments(), then saving those stale references
afterward, which overwrote the correctly cleared adjustments.

Since applyPriceAdjustments() already saves OrderLines with the correct
(cleared) adjustments, the separate save was both redundant and buggy.

Fixes #4016
Jan Tokic 2 hari lalu
induk
melakukan
bf397cccfb
1 mengubah file dengan 1 tambahan dan 12 penghapusan
  1. 1 12
      packages/core/src/service/services/order.service.ts

+ 1 - 12
packages/core/src/service/services/order.service.ts

@@ -12,7 +12,6 @@ import {
     AddFulfillmentToOrderResult,
     AddFulfillmentToOrderResult,
     AddManualPaymentToOrderResult,
     AddManualPaymentToOrderResult,
     AddNoteToOrderInput,
     AddNoteToOrderInput,
-    AdjustmentType,
     CancelOrderInput,
     CancelOrderInput,
     CancelOrderResult,
     CancelOrderResult,
     CancelPaymentResult,
     CancelPaymentResult,
@@ -1028,14 +1027,6 @@ export class OrderService {
     async removeCouponCode(ctx: RequestContext, orderId: ID, couponCode: string) {
     async removeCouponCode(ctx: RequestContext, orderId: ID, couponCode: string) {
         const order = await this.getOrderOrThrow(ctx, orderId);
         const order = await this.getOrderOrThrow(ctx, orderId);
         if (order.couponCodes.includes(couponCode)) {
         if (order.couponCodes.includes(couponCode)) {
-            // When removing a couponCode which has triggered an Order-level discount
-            // we need to make sure we persist the changes to the adjustments array of
-            // any affected OrderLines.
-            const affectedOrderLines = order.lines.filter(
-                line =>
-                    line.adjustments.filter(a => a.type === AdjustmentType.DISTRIBUTED_ORDER_PROMOTION)
-                        .length,
-            );
             order.couponCodes = order.couponCodes.filter(cc => cc !== couponCode);
             order.couponCodes = order.couponCodes.filter(cc => cc !== couponCode);
             await this.historyService.createHistoryEntryForOrder({
             await this.historyService.createHistoryEntryForOrder({
                 ctx,
                 ctx,
@@ -1044,9 +1035,7 @@ export class OrderService {
                 data: { couponCode },
                 data: { couponCode },
             });
             });
             await this.eventBus.publish(new CouponCodeEvent(ctx, couponCode, orderId, 'removed'));
             await this.eventBus.publish(new CouponCodeEvent(ctx, couponCode, orderId, 'removed'));
-            const result = await this.applyPriceAdjustments(ctx, order);
-            await this.connection.getRepository(ctx, OrderLine).save(affectedOrderLines);
-            return result;
+            return this.applyPriceAdjustments(ctx, order);
         } else {
         } else {
             return order;
             return order;
         }
         }