Jelajahi Sumber

fix(core): Fix incorrect quantity adjustment (#983)

Fixes #931
William Milne 4 tahun lalu
induk
melakukan
2441ce74f1

+ 2 - 2
packages/core/e2e/fixtures/e2e-products-full.csv

@@ -1,7 +1,7 @@
 name              ,slug              ,description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ,assets                            ,facets                                 ,optionGroups   ,optionValues     ,sku         ,price  ,taxCategory,stockOnHand,trackInventory,variantAssets,variantFacets
 Laptop            ,laptop            ,"Now equipped with seventh-generation Intel Core processors, Laptop is snappier than ever. From daily tasks like launching apps and opening files to more advanced computing, you can power through your day thanks to faster SSDs and Turbo Boost processing up to 3.6GHz."                                                                                                                                                                                                                 ,derick-david-409858-unsplash.jpg  ,category:electronics|category:computers,screen size|RAM,13 inch|8GB      ,L2201308    ,1299.00,standard   ,100        ,false         ,             ,
-                  ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,15 inch|8GB      ,L2201508    ,1399.00,standard   ,100        ,false         ,             ,
-                  ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,13 inch|16GB     ,L2201316    ,2199.00,standard   ,100        ,false         ,             ,
+                  ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,15 inch|8GB      ,L2201508    ,1399.00,standard   ,100        ,false          ,             ,
+                  ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,13 inch|16GB     ,L2201316    ,2199.00,standard   ,100        ,true         ,             ,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,15 inch|16GB     ,L2201516    ,2299.00,standard   ,100        ,false         ,             ,
 Curvy Monitor     ,curvy-monitor     ,"Discover a truly immersive viewing experience with this monitor curved more deeply than any other. Wrapping around your field of vision the 1,800 R screencreates a wider field of view, enhances depth perception, and minimises peripheral distractions to draw you deeper in to your content."                                                                                                                                                                                           ,alexandru-acea-686569-unsplash.jpg,category:electronics|category:computers,monitor size   ,24 inch          ,C24F390     ,143.74 ,standard   ,100        ,false         ,             ,
                   ,                  ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ,                                  ,                                       ,               ,27 inch          ,C27F390     ,169.94 ,standard   ,100        ,false         ,             ,

+ 48 - 5
packages/core/e2e/shop-order.e2e-spec.ts

@@ -109,7 +109,7 @@ describe('Shop orders', () => {
                 ],
             },
             orderOptions: {
-                orderItemsLimit: 99,
+                orderItemsLimit: 199,
             },
         }),
     );
@@ -473,12 +473,12 @@ describe('Shop orders', () => {
                 AddItemToOrder.Variables
             >(ADD_ITEM_TO_ORDER, {
                 productVariantId: 'T_1',
-                quantity: 100,
+                quantity: 200,
             });
 
             orderResultGuard.assertErrorResult(addItemToOrder);
             expect(addItemToOrder.message).toBe(
-                'Cannot add items. An order may consist of a maximum of 99 items',
+                'Cannot add items. An order may consist of a maximum of 199 items',
             );
             expect(addItemToOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
         });
@@ -520,17 +520,60 @@ describe('Shop orders', () => {
             expect(adjustOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
         });
 
+        it('adjustOrderLine with quantity > stockOnHand only allows user to have stock on hand', async () => {
+            const { addItemToOrder } = await shopClient.query<
+                AddItemToOrder.Mutation,
+                AddItemToOrder.Variables
+            >(ADD_ITEM_TO_ORDER, {
+                productVariantId: 'T_3',
+                quantity: 111,
+            });
+            orderResultGuard.assertErrorResult(addItemToOrder);
+            // Insufficient stock error should return because there are only 100 available
+            expect(addItemToOrder.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');
+
+            // But it should still add the item to the order
+            expect(addItemToOrder!.order.lines[1].quantity).toBe(100);
+
+            const { adjustOrderLine } = await shopClient.query<
+                AdjustItemQuantity.Mutation,
+                AdjustItemQuantity.Variables
+            >(ADJUST_ITEM_QUANTITY, {
+                orderLineId: 'T_8',
+                quantity: 101,
+            });
+            orderResultGuard.assertErrorResult(adjustOrderLine);
+            expect(adjustOrderLine.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');
+            expect(adjustOrderLine.message).toBe(
+                'Only 100 items were added to the order due to insufficient stock',
+            );
+
+            const order = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+            expect(order.activeOrder?.lines[1].quantity).toBe(100);
+
+            const { adjustOrderLine: adjustLine2 } = await shopClient.query<
+                AdjustItemQuantity.Mutation,
+                AdjustItemQuantity.Variables
+            >(ADJUST_ITEM_QUANTITY, {
+                orderLineId: 'T_8',
+                quantity: 0,
+            });
+            orderResultGuard.assertSuccess(adjustLine2);
+            expect(adjustLine2!.lines.length).toBe(1);
+            expect(adjustLine2!.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
+        });
+
         it('adjustOrderLine errors when going beyond orderItemsLimit', async () => {
             const { adjustOrderLine } = await shopClient.query<
                 AdjustItemQuantity.Mutation,
                 AdjustItemQuantity.Variables
             >(ADJUST_ITEM_QUANTITY, {
                 orderLineId: firstOrderLineId,
-                quantity: 100,
+                quantity: 200,
             });
             orderResultGuard.assertErrorResult(adjustOrderLine);
             expect(adjustOrderLine.message).toBe(
-                'Cannot add items. An order may consist of a maximum of 99 items',
+                'Cannot add items. An order may consist of a maximum of 199 items',
             );
             expect(adjustOrderLine.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
         });

+ 7 - 2
packages/core/src/service/services/order.service.ts

@@ -409,7 +409,12 @@ export class OrderService {
             productVariantId,
             customFields,
         );
-        await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
+        if (correctedQuantity < quantity) {
+            const newQuantity = (existingOrderLine ? existingOrderLine?.quantity : 0) + correctedQuantity;
+            await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, newQuantity, order);
+        } else {
+            await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
+        }
         const quantityWasAdjustedDown = correctedQuantity < quantity;
         const updatedOrder = await this.applyPriceAdjustments(ctx, order, orderLine);
         if (quantityWasAdjustedDown) {
@@ -457,7 +462,7 @@ export class OrderService {
             order.lines = order.lines.filter(l => !idsAreEqual(l.id, orderLine.id));
             await this.connection.getRepository(ctx, OrderLine).remove(orderLine);
         } else {
-            await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, quantity, order);
+            await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
         }
         const quantityWasAdjustedDown = correctedQuantity < quantity;
         const updatedOrder = await this.applyPriceAdjustments(ctx, order, orderLine);