Browse Source

fix(core): Call OrderInterceptor `willRemoveItemFromOrder` on `removeAllItemsFromOrder` (#3578) (#3592)

gabriellbui 7 months ago
parent
commit
6c239bf451

+ 47 - 5
packages/core/e2e/order-interceptor.e2e-spec.ts

@@ -19,6 +19,7 @@ import {
     ADD_ITEM_TO_ORDER,
     ADD_ITEM_TO_ORDER,
     ADJUST_ITEM_QUANTITY,
     ADJUST_ITEM_QUANTITY,
     GET_ACTIVE_ORDER,
     GET_ACTIVE_ORDER,
+    REMOVE_ALL_ORDER_LINES,
     REMOVE_ITEM_FROM_ORDER,
     REMOVE_ITEM_FROM_ORDER,
 } from './graphql/shop-definitions';
 } from './graphql/shop-definitions';
 
 
@@ -154,7 +155,7 @@ describe('Order interceptor', () => {
         expect(interceptor1.willAdjustOrderLineSpy.mock.calls[0][2].customFields).toBeUndefined();
         expect(interceptor1.willAdjustOrderLineSpy.mock.calls[0][2].customFields).toBeUndefined();
     });
     });
 
 
-    it('willRemoveItemFromOrder', async () => {
+    it('willRemoveItemFromOrder when removing an item', async () => {
         const { removeOrderLine } = await shopClient.query<
         const { removeOrderLine } = await shopClient.query<
             CodegenShop.RemoveItemFromOrderMutation,
             CodegenShop.RemoveItemFromOrderMutation,
             CodegenShop.RemoveItemFromOrderMutationVariables
             CodegenShop.RemoveItemFromOrderMutationVariables
@@ -170,6 +171,47 @@ describe('Order interceptor', () => {
         expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[0][2].productVariant.id).toEqual(1);
         expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[0][2].productVariant.id).toEqual(1);
     });
     });
 
 
+    it('willRemoveItemFromOrder when removing all items', async () => {
+        const { addItemToOrder: addFirstItem } = await shopClient.query<
+            CodegenShop.AddItemToOrderMutation,
+            CodegenShop.AddItemToOrderMutationVariables
+        >(ADD_ITEM_TO_ORDER, {
+            productVariantId: 'T_1',
+            quantity: 2,
+        });
+
+        orderResultGuard.assertSuccess(addFirstItem);
+
+        const { addItemToOrder: addSecondItem } = await shopClient.query<
+            CodegenShop.AddItemToOrderMutation,
+            CodegenShop.AddItemToOrderMutationVariables
+        >(ADD_ITEM_TO_ORDER, {
+            productVariantId: 'T_2',
+            quantity: 2,
+        });
+
+        orderResultGuard.assertSuccess(addSecondItem);
+
+        interceptor1.willRemoveItemFromOrderSpy.mockClear();
+
+        const { removeAllOrderLines } = await shopClient.query<
+            CodegenShop.RemoveAllOrderLinesMutation,
+            CodegenShop.RemoveAllOrderLinesMutationVariables
+        >(REMOVE_ALL_ORDER_LINES, undefined, {
+            overridden: 1,
+        });
+
+        orderResultGuard.assertSuccess(removeAllOrderLines);
+
+        expect(interceptor1.willRemoveItemFromOrderSpy).toHaveBeenCalledTimes(2);
+        expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[0][0]).toBeInstanceOf(RequestContext);
+        expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[0][1]).toBeInstanceOf(Order);
+        expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[0][2].productVariant.id).toEqual(1);
+        expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[1][0]).toBeInstanceOf(RequestContext);
+        expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[1][1]).toBeInstanceOf(Order);
+        expect(interceptor1.willRemoveItemFromOrderSpy.mock.calls[1][2].productVariant.id).toEqual(2);
+    });
+
     it('willAddItemToOrder with error', async () => {
     it('willAddItemToOrder with error', async () => {
         const { addItemToOrder } = await shopClient.query<
         const { addItemToOrder } = await shopClient.query<
             CodegenShop.AddItemToOrderMutation,
             CodegenShop.AddItemToOrderMutation,
@@ -209,7 +251,7 @@ describe('Order interceptor', () => {
             CodegenShop.AdjustItemQuantityMutation,
             CodegenShop.AdjustItemQuantityMutation,
             CodegenShop.AdjustItemQuantityMutationVariables
             CodegenShop.AdjustItemQuantityMutationVariables
         >(ADJUST_ITEM_QUANTITY, {
         >(ADJUST_ITEM_QUANTITY, {
-            orderLineId: 'T_2',
+            orderLineId: 'T_4',
             quantity: 1,
             quantity: 1,
         });
         });
 
 
@@ -232,7 +274,7 @@ describe('Order interceptor', () => {
             CodegenShop.AdjustItemQuantityMutation,
             CodegenShop.AdjustItemQuantityMutation,
             CodegenShop.AdjustItemQuantityMutationVariables
             CodegenShop.AdjustItemQuantityMutationVariables
         >(ADJUST_ITEM_QUANTITY, {
         >(ADJUST_ITEM_QUANTITY, {
-            orderLineId: 'T_2',
+            orderLineId: 'T_4',
             quantity: 5,
             quantity: 5,
         });
         });
 
 
@@ -246,7 +288,7 @@ describe('Order interceptor', () => {
             CodegenShop.RemoveItemFromOrderMutation,
             CodegenShop.RemoveItemFromOrderMutation,
             CodegenShop.RemoveItemFromOrderMutationVariables
             CodegenShop.RemoveItemFromOrderMutationVariables
         >(REMOVE_ITEM_FROM_ORDER, {
         >(REMOVE_ITEM_FROM_ORDER, {
-            orderLineId: 'T_2',
+            orderLineId: 'T_4',
         });
         });
 
 
         orderResultGuard.assertErrorResult(removeOrderLine);
         orderResultGuard.assertErrorResult(removeOrderLine);
@@ -270,7 +312,7 @@ describe('Order interceptor', () => {
         >(
         >(
             REMOVE_ITEM_FROM_ORDER,
             REMOVE_ITEM_FROM_ORDER,
             {
             {
-                orderLineId: 'T_2',
+                orderLineId: 'T_4',
             },
             },
             {
             {
                 overridden: 1,
                 overridden: 1,

+ 13 - 0
packages/core/src/service/services/order.service.ts

@@ -891,6 +891,19 @@ export class OrderService {
         if (validationError) {
         if (validationError) {
             return validationError;
             return validationError;
         }
         }
+
+        const { orderInterceptors } = this.configService.orderOptions;
+        for (const orderLine of order.lines) {
+            for (const interceptor of orderInterceptors) {
+                if (interceptor.willRemoveItemFromOrder) {
+                    const error = await interceptor.willRemoveItemFromOrder(ctx, order, orderLine);
+                    if (error) {
+                        return new OrderInterceptorError({ interceptorError: error });
+                    }
+                }
+            }
+        }
+
         await this.connection.getRepository(ctx, OrderLine).remove(order.lines);
         await this.connection.getRepository(ctx, OrderLine).remove(order.lines);
         order.lines = [];
         order.lines = [];
         const updatedOrder = await this.applyPriceAdjustments(ctx, order);
         const updatedOrder = await this.applyPriceAdjustments(ctx, order);