Browse Source

fix(core): Clean up Sessions when deleting a Channel

Closes #686
Michael Bromley 5 years ago
parent
commit
057ee8ae7a

+ 5 - 0
packages/core/e2e/channel.e2e-spec.ts

@@ -41,6 +41,7 @@ import {
     ME,
     UPDATE_CHANNEL,
 } from './graphql/shared-definitions';
+import { GET_ACTIVE_ORDER } from './graphql/shop-definitions';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
 
 describe('Channels', () => {
@@ -333,6 +334,10 @@ describe('Channels', () => {
         });
         expect(assignProductsToChannel[0].channels.map(c => c.id).sort()).toEqual(['T_1', 'T_2']);
 
+        // create a Session on the Channel to be deleted to ensure it gets cleaned up
+        shopClient.setChannelToken(SECOND_CHANNEL_TOKEN);
+        await shopClient.query(GET_ACTIVE_ORDER);
+
         const { deleteChannel } = await adminClient.query<DeleteChannel.Mutation, DeleteChannel.Variables>(
             DELETE_CHANNEL,
             {

+ 2 - 0
packages/core/src/service/services/channel.service.ts

@@ -22,6 +22,7 @@ import { ConfigService } from '../../config/config.service';
 import { VendureEntity } from '../../entity/base/base.entity';
 import { Channel } from '../../entity/channel/channel.entity';
 import { ProductVariantPrice } from '../../entity/product-variant/product-variant-price.entity';
+import { Session } from '../../entity/session/session.entity';
 import { Zone } from '../../entity/zone/zone.entity';
 import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
 import { patchEntity } from '../helpers/utils/patch-entity';
@@ -201,6 +202,7 @@ export class ChannelService {
 
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         await this.connection.getEntityOrThrow(ctx, Channel, id);
+        await this.connection.getRepository(ctx, Session).delete({ activeChannelId: id });
         await this.connection.getRepository(ctx, Channel).delete(id);
         await this.connection.getRepository(ctx, ProductVariantPrice).delete({
             channelId: id,