Explorar el Código

fix(core): Allow cancelling an Order that has deleted ProductVariants

Closes #1567
Michael Bromley hace 3 años
padre
commit
79c36b5340

+ 23 - 0
packages/core/e2e/shop-order.e2e-spec.ts

@@ -22,6 +22,9 @@ import {
 import { countryCodeShippingEligibilityChecker } from './fixtures/test-shipping-eligibility-checkers';
 import {
     AttemptLogin,
+    CanceledOrderFragment,
+    CancelOrderMutation,
+    CancelOrderMutationVariables,
     CreateAddressInput,
     CreateShippingMethod,
     CreateShippingMethodInput,
@@ -65,6 +68,7 @@ import {
 } from './graphql/generated-e2e-shop-types';
 import {
     ATTEMPT_LOGIN,
+    CANCEL_ORDER,
     CREATE_SHIPPING_METHOD,
     DELETE_PRODUCT,
     DELETE_PRODUCT_VARIANT,
@@ -138,6 +142,7 @@ describe('Shop orders', () => {
         | UpdatedOrderFragment
         | TestOrderFragmentFragment
         | TestOrderWithPaymentsFragment
+        | CanceledOrderFragment
         | ActiveOrderCustomerFragment;
     const orderResultGuard: ErrorResultGuard<OrderSuccessResult> = createErrorResultGuard(
         input => !!input.lines,
@@ -1831,6 +1836,7 @@ describe('Shop orders', () => {
             }, `No ProductVariant with the id '34' could be found`),
         );
 
+        let orderWithDeletedProductVariantId: string;
         it('errors when transitioning to ArrangingPayment with deleted variant', async () => {
             const orchidProductId = 'T_19';
             const orchidVariantId = 'T_33';
@@ -1845,6 +1851,7 @@ describe('Shop orders', () => {
             });
 
             orderResultGuard.assertSuccess(addItemToOrder);
+            orderWithDeletedProductVariantId = addItemToOrder.id;
 
             await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
                 id: orchidProductId,
@@ -1863,6 +1870,22 @@ describe('Shop orders', () => {
             );
             expect(transitionOrderToState!.errorCode).toBe(ErrorCode.ORDER_STATE_TRANSITION_ERROR);
         });
+
+        // https://github.com/vendure-ecommerce/vendure/issues/1567
+        it('allows transitioning to Cancelled with deleted variant', async () => {
+            const { cancelOrder } = await adminClient.query<
+                CancelOrderMutation,
+                CancelOrderMutationVariables
+            >(CANCEL_ORDER, {
+                input: {
+                    orderId: orderWithDeletedProductVariantId,
+                },
+            });
+
+            orderResultGuard.assertSuccess(cancelOrder);
+
+            expect(cancelOrder.state).toBe('Cancelled');
+        });
     });
 
     // https://github.com/vendure-ecommerce/vendure/issues/1195

+ 1 - 1
packages/core/src/service/helpers/order-state-machine/order-state-machine.ts

@@ -108,7 +108,7 @@ export class OrderStateMachine {
                 return `message.cannot-transition-from-arranging-additional-payment`;
             }
         }
-        if (fromState === 'AddingItems') {
+        if (fromState === 'AddingItems' && toState !== 'Cancelled') {
             const variantIds = unique(data.order.lines.map(l => l.productVariant.id));
             const qb = this.connection
                 .getRepository(data.ctx, ProductVariant)