1
0
Эх сурвалжийг харах

fix(core): Fix deletion of guest Customers

Fixes #1960
Michael Bromley 3 жил өмнө
parent
commit
cbc500f1e1

+ 45 - 2
packages/core/e2e/customer.e2e-spec.ts

@@ -37,7 +37,15 @@ import {
     UpdateCustomer,
     UpdateCustomerNote,
 } from './graphql/generated-e2e-admin-types';
-import { AddItemToOrder, UpdatedOrderFragment } from './graphql/generated-e2e-shop-types';
+import {
+    ActiveOrderCustomerFragment,
+    AddItemToOrder,
+    AddItemToOrderMutation,
+    AddItemToOrderMutationVariables,
+    SetCustomerForOrderMutation,
+    SetCustomerForOrderMutationVariables,
+    UpdatedOrderFragment,
+} from './graphql/generated-e2e-shop-types';
 import {
     CREATE_ADDRESS,
     CREATE_CUSTOMER,
@@ -51,7 +59,7 @@ import {
     UPDATE_CUSTOMER,
     UPDATE_CUSTOMER_NOTE,
 } from './graphql/shared-definitions';
-import { ADD_ITEM_TO_ORDER } from './graphql/shop-definitions';
+import { ADD_ITEM_TO_ORDER, SET_CUSTOMER } from './graphql/shop-definitions';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
 
 // tslint:disable:no-non-null-assertion
@@ -586,6 +594,41 @@ describe('Customer resolver', () => {
             expect(createCustomer.firstName).toBe('Reusing Email');
             expect(createCustomer.user?.identifier).toBe(thirdCustomer.emailAddress);
         });
+
+        // https://github.com/vendure-ecommerce/vendure/issues/1960
+        it('delete a guest Customer', async () => {
+            const orderErrorGuard: ErrorResultGuard<ActiveOrderCustomerFragment> = createErrorResultGuard(
+                input => !!input.lines,
+            );
+
+            await shopClient.asAnonymousUser();
+            await shopClient.query<AddItemToOrderMutation, AddItemToOrderMutationVariables>(
+                ADD_ITEM_TO_ORDER,
+                {
+                    productVariantId: 'T_1',
+                    quantity: 1,
+                },
+            );
+            const { setCustomerForOrder } = await shopClient.query<
+                SetCustomerForOrderMutation,
+                SetCustomerForOrderMutationVariables
+            >(SET_CUSTOMER, {
+                input: {
+                    firstName: 'Guest',
+                    lastName: 'Customer',
+                    emailAddress: 'guest@test.com',
+                },
+            });
+
+            orderErrorGuard.assertSuccess(setCustomerForOrder);
+
+            const result = await adminClient.query<DeleteCustomer.Mutation, DeleteCustomer.Variables>(
+                DELETE_CUSTOMER,
+                { id: setCustomerForOrder.customer!.id },
+            );
+
+            expect(result.deleteCustomer).toEqual({ result: DeletionResult.DELETED });
+        });
     });
 
     describe('customer notes', () => {

+ 3 - 1
packages/core/src/service/services/customer.service.ts

@@ -786,7 +786,9 @@ export class CustomerService {
             .getRepository(ctx, Customer)
             .update({ id: customerId }, { deletedAt: new Date() });
         // tslint:disable-next-line:no-non-null-assertion
-        await this.userService.softDelete(ctx, customer.user!.id);
+        if (customer.user) {
+            await this.userService.softDelete(ctx, customer.user.id);
+        }
         this.eventBus.publish(new CustomerEvent(ctx, customer, 'deleted', customerId));
         return {
             result: DeletionResult.DELETED,