Browse Source

fix(core): Fix merging order with conflicting products using UseGuestStrategy (#3155)

gkielwasser 1 year ago
parent
commit
f0607aad91

+ 19 - 0
packages/core/e2e/order-merge.e2e-spec.ts

@@ -193,6 +193,25 @@ describe('Order merging', () => {
         ).toEqual([{ productVariantId: 'T_5', quantity: 3 }]);
     });
 
+    it('UseGuestStrategy with conflicting lines', async () => {
+        const result = await testMerge({
+            strategy: new UseGuestStrategy(),
+            customerEmailAddress: customers[8].emailAddress,
+            existingOrderLines: [
+                { productVariantId: 'T_7', quantity: 1 },
+                { productVariantId: 'T_8', quantity: 1 },
+            ],
+            guestOrderLines: [{ productVariantId: 'T_8', quantity: 3 }],
+        });
+
+        expect(
+            (result?.lines || []).sort(sortById).map(line => ({
+                productVariantId: line.productVariant.id,
+                quantity: line.quantity,
+            })),
+        ).toEqual([{ productVariantId: 'T_8', quantity: 3 }]);
+    });
+
     it('UseGuestIfExistingEmptyStrategy with empty existing', async () => {
         const result = await testMerge({
             strategy: new UseGuestIfExistingEmptyStrategy(),

+ 9 - 9
packages/core/src/service/services/order.service.ts

@@ -1619,6 +1619,15 @@ export class OrderService {
         if (orderToDelete) {
             await this.deleteOrder(ctx, orderToDelete);
         }
+        if (order && linesToDelete) {
+            const orderId = order.id;
+            for (const line of linesToDelete) {
+                const result = await this.removeItemFromOrder(ctx, orderId, line.orderLineId);
+                if (!isGraphQlErrorResult(result)) {
+                    order = result;
+                }
+            }
+        }
         if (order && linesToInsert) {
             const orderId = order.id;
             for (const line of linesToInsert) {
@@ -1649,15 +1658,6 @@ export class OrderService {
                 }
             }
         }
-        if (order && linesToDelete) {
-            const orderId = order.id;
-            for (const line of linesToDelete) {
-                const result = await this.removeItemFromOrder(ctx, orderId, line.orderLineId);
-                if (!isGraphQlErrorResult(result)) {
-                    order = result;
-                }
-            }
-        }
         const customer = await this.customerService.findOneByUserId(ctx, user.id);
         if (order && customer) {
             order.customer = customer;