Browse Source

fix(core): Remove deleted Customers from any CustomerGroups

Fixes #1785
Michael Bromley 3 years ago
parent
commit
9820d9e2a3

+ 34 - 0
packages/core/e2e/customer-group.e2e-spec.ts

@@ -9,7 +9,11 @@ import {
     AddCustomersToGroup,
     CreateCustomerGroup,
     DeleteCustomerGroup,
+    DeleteCustomerMutation,
+    DeleteCustomerMutationVariables,
     GetCustomerGroup,
+    GetCustomerGroupQuery,
+    GetCustomerGroupQueryVariables,
     GetCustomerGroups,
     GetCustomerHistory,
     GetCustomerList,
@@ -22,6 +26,7 @@ import { DeletionResult } from './graphql/generated-e2e-shop-types';
 import {
     ADD_CUSTOMERS_TO_GROUP,
     CREATE_CUSTOMER_GROUP,
+    DELETE_CUSTOMER,
     DELETE_CUSTOMER_GROUP,
     GET_CUSTOMER_GROUP,
     GET_CUSTOMER_GROUPS,
@@ -285,4 +290,33 @@ describe('CustomerGroup resolver', () => {
         const { customerGroups } = await adminClient.query<GetCustomerGroups.Query>(GET_CUSTOMER_GROUPS);
         expect(customerGroups.totalItems).toBe(0);
     });
+
+    // https://github.com/vendure-ecommerce/vendure/issues/1785
+    it('removes customer from group when customer is deleted', async () => {
+        const customer5Id = customers[4].id;
+        const { createCustomerGroup } = await adminClient.query<
+            CreateCustomerGroup.Mutation,
+            CreateCustomerGroup.Variables
+        >(CREATE_CUSTOMER_GROUP, {
+            input: {
+                name: 'group-1785',
+                customerIds: [customer5Id],
+            },
+        });
+
+        expect(createCustomerGroup.customers.items).toEqual([{ id: customer5Id }]);
+
+        await adminClient.query<DeleteCustomerMutation, DeleteCustomerMutationVariables>(DELETE_CUSTOMER, {
+            id: customer5Id,
+        });
+
+        const { customerGroup } = await adminClient.query<
+            GetCustomerGroupQuery,
+            GetCustomerGroupQueryVariables
+        >(GET_CUSTOMER_GROUP, {
+            id: createCustomerGroup.id,
+        });
+
+        expect(customerGroup?.customers.items).toEqual([]);
+    });
 });

+ 13 - 1
packages/core/src/api/resolvers/admin/customer.resolver.ts

@@ -22,6 +22,7 @@ import { PaginatedList } from '@vendure/common/lib/shared-types';
 import { ErrorResultUnion } from '../../../common/error/error-result';
 import { Address } from '../../../entity/address/address.entity';
 import { Customer } from '../../../entity/customer/customer.entity';
+import { CustomerGroupService } from '../../../service/index';
 import { CustomerService } from '../../../service/services/customer.service';
 import { OrderService } from '../../../service/services/order.service';
 import { RequestContext } from '../../common/request-context';
@@ -32,7 +33,11 @@ import { Transaction } from '../../decorators/transaction.decorator';
 
 @Resolver()
 export class CustomerResolver {
-    constructor(private customerService: CustomerService, private orderService: OrderService) {}
+    constructor(
+        private customerService: CustomerService,
+        private customerGroupService: CustomerGroupService,
+        private orderService: OrderService,
+    ) {}
 
     @Query()
     @Allow(Permission.ReadCustomer)
@@ -117,6 +122,13 @@ export class CustomerResolver {
         @Ctx() ctx: RequestContext,
         @Args() args: MutationDeleteCustomerArgs,
     ): Promise<DeletionResponse> {
+        const groups = await this.customerService.getCustomerGroups(ctx, args.id);
+        for (const group of groups) {
+            await this.customerGroupService.removeCustomersFromGroup(ctx, {
+                customerGroupId: group.id,
+                customerIds: [args.id],
+            });
+        }
         return this.customerService.softDelete(ctx, args.id);
     }
 

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

@@ -77,6 +77,7 @@ export class CustomerGroupService {
             .leftJoin('customer.groups', 'group')
             .leftJoin('customer.channels', 'channel')
             .andWhere('group.id = :groupId', { groupId: customerGroupId })
+            .andWhere('customer.deletedAt IS NULL', { groupId: customerGroupId })
             .andWhere('channel.id =:channelId', { channelId: ctx.channelId })
             .getManyAndCount()
             .then(([items, totalItems]) => ({ items, totalItems }));