Răsfoiți Sursa

fix(core): Correctly validate couponCode when adding guest customer

Michael Bromley 2 ani în urmă
părinte
comite
a0458e2965

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

@@ -1538,6 +1538,35 @@ describe('Promotions applied to Orders', () => {
                 expect(activeOrder!.couponCodes).toEqual([]);
                 expect(activeOrder!.totalWithTax).toBe(6000);
             });
+
+            it('does not remove valid couponCode when setting guest customer', async () => {
+                await shopClient.asAnonymousUser();
+                await createNewActiveOrder();
+
+                const { applyCouponCode } = await shopClient.query<
+                    ApplyCouponCode.Mutation,
+                    ApplyCouponCode.Variables
+                >(APPLY_COUPON_CODE, { couponCode: TEST_COUPON_CODE });
+                orderResultGuard.assertSuccess(applyCouponCode);
+
+                expect(applyCouponCode!.totalWithTax).toBe(0);
+                expect(applyCouponCode!.couponCodes).toEqual([TEST_COUPON_CODE]);
+
+                await shopClient.query<SetCustomerForOrder.Mutation, SetCustomerForOrder.Variables>(
+                    SET_CUSTOMER,
+                    {
+                        input: {
+                            emailAddress: 'new-guest@test.com',
+                            firstName: 'New Guest',
+                            lastName: 'Customer',
+                        },
+                    },
+                );
+
+                const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+                expect(activeOrder!.couponCodes).toEqual([TEST_COUPON_CODE]);
+                expect(applyCouponCode!.totalWithTax).toBe(0);
+            });
         });
 
         describe('signed-in customer', () => {

+ 2 - 1
packages/core/src/service/services/promotion.service.ts

@@ -324,7 +324,8 @@ export class PromotionService {
             .leftJoin('order.promotions', 'promotion')
             .where('promotion.id = :promotionId', { promotionId })
             .andWhere('order.customer = :customerId', { customerId })
-            .andWhere('order.state != :state', { state: 'Cancelled' as OrderState });
+            .andWhere('order.state != :state', { state: 'Cancelled' as OrderState })
+            .andWhere('order.active = :active', { active: false });
 
         return qb.getCount();
     }