Browse Source

fix(core): Do not allow negative total with orderFixedDiscount action

Fixes #1823
Michael Bromley 3 years ago
parent
commit
a031956eaa

+ 37 - 0
packages/core/e2e/order-promotion.e2e-spec.ts

@@ -63,6 +63,8 @@ import {
     RemoveItemFromOrder,
     SetCustomerForOrder,
     SetShippingMethod,
+    SetShippingMethodMutation,
+    SetShippingMethodMutationVariables,
     TestOrderFragmentFragment,
     TestOrderWithPaymentsFragment,
     UpdatedOrderFragment,
@@ -785,6 +787,41 @@ describe('Promotions applied to Orders', () => {
                 expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
                 expect(applyCouponCode!.totalWithTax).toBe(5000);
             });
+
+            it('does not result in negative total when shipping is included', async () => {
+                shopClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
+                const { addItemToOrder } = await shopClient.query<
+                    AddItemToOrder.Mutation,
+                    AddItemToOrder.Variables
+                >(ADD_ITEM_TO_ORDER, {
+                    productVariantId: getVariantBySlug('item-100').id,
+                    quantity: 1,
+                });
+                orderResultGuard.assertSuccess(addItemToOrder);
+                expect(addItemToOrder!.totalWithTax).toBe(120);
+                expect(addItemToOrder!.discounts.length).toBe(0);
+
+                const { setOrderShippingMethod } = await shopClient.query<
+                    SetShippingMethodMutation,
+                    SetShippingMethodMutationVariables
+                >(SET_SHIPPING_METHOD, {
+                    id: 'T_1',
+                });
+                orderResultGuard.assertSuccess(setOrderShippingMethod);
+                expect(setOrderShippingMethod.totalWithTax).toBe(620);
+
+                const { applyCouponCode } = await shopClient.query<
+                    ApplyCouponCode.Mutation,
+                    ApplyCouponCode.Variables
+                >(APPLY_COUPON_CODE, {
+                    couponCode,
+                });
+                orderResultGuard.assertSuccess(applyCouponCode);
+                expect(applyCouponCode!.discounts.length).toBe(1);
+                expect(applyCouponCode!.discounts[0].description).toBe('$10 discount on order');
+                expect(applyCouponCode!.subTotalWithTax).toBe(0);
+                expect(applyCouponCode!.totalWithTax).toBe(500); // shipping price
+            });
         });
 
         describe('discountOnItemWithFacets', () => {

+ 1 - 1
packages/core/src/config/promotion/actions/order-fixed-discount-action.ts

@@ -13,7 +13,7 @@ export const orderFixedDiscount = new PromotionOrderAction({
         },
     },
     execute(ctx, order, args) {
-        return -Math.min(args.discount, order.total);
+        return -Math.min(args.discount, order.subTotal);
     },
     description: [{ languageCode: LanguageCode.en, value: 'Discount order by fixed amount' }],
 });