Browse Source

feat(core): Implement deleteChannel mutation

Relates to #12
Michael Bromley 6 years ago
parent
commit
989960b98c

+ 7 - 0
packages/common/src/generated-types.ts

@@ -1683,6 +1683,8 @@ export type Mutation = {
   createChannel: Channel,
   /** Update an existing Channel */
   updateChannel: Channel,
+  /** Delete a Channel */
+  deleteChannel: DeletionResponse,
   /** Create a new Collection */
   createCollection: Collection,
   /** Update an existing Collection */
@@ -1840,6 +1842,11 @@ export type MutationUpdateChannelArgs = {
 };
 
 
+export type MutationDeleteChannelArgs = {
+  id: Scalars['ID']
+};
+
+
 export type MutationCreateCollectionArgs = {
   input: CreateCollectionInput
 };

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

@@ -14,6 +14,9 @@ import {
     CreateProduct,
     CreateRole,
     CurrencyCode,
+    DeleteChannel,
+    DeletionResult,
+    GetChannels,
     GetCustomerList,
     GetProductWithVariants,
     LanguageCode,
@@ -351,8 +354,53 @@ describe('Channels', () => {
             expect(removeProductsFromChannel[0].channels.map(c => c.id)).toEqual(['T_1']);
         });
     });
+
+    it('deleteChannel', async () => {
+        const PROD_ID = 'T_1';
+
+        const { assignProductsToChannel } = await adminClient.query<
+            AssignProductsToChannel.Mutation,
+            AssignProductsToChannel.Variables
+        >(ASSIGN_PRODUCT_TO_CHANNEL, {
+            input: {
+                channelId: 'T_2',
+                productIds: [PROD_ID],
+            },
+        });
+        expect(assignProductsToChannel[0].channels.map(c => c.id)).toEqual(['T_1', 'T_2']);
+
+        const { deleteChannel } = await adminClient.query<DeleteChannel.Mutation, DeleteChannel.Variables>(
+            DELETE_CHANNEL,
+            {
+                id: 'T_2',
+            },
+        );
+
+        expect(deleteChannel.result).toBe(DeletionResult.DELETED);
+
+        const { channels } = await adminClient.query<GetChannels.Query>(GET_CHANNELS);
+        expect(channels.map(c => c.id)).toEqual(['T_1', 'T_3']);
+
+        const { product } = await adminClient.query<
+            GetProductWithVariants.Query,
+            GetProductWithVariants.Variables
+        >(GET_PRODUCT_WITH_VARIANTS, {
+            id: PROD_ID,
+        });
+        expect(product!.channels.map(c => c.id)).toEqual(['T_1']);
+    });
 });
 
+const GET_CHANNELS = gql`
+    query GetChannels {
+        channels {
+            id
+            code
+            token
+        }
+    }
+`;
+
 const ASSIGN_PRODUCT_TO_CHANNEL = gql`
     mutation AssignProductsToChannel($input: AssignProductsToChannelInput!) {
         assignProductsToChannel(input: $input) {
@@ -370,3 +418,12 @@ const REMOVE_PRODUCT_FROM_CHANNEL = gql`
     }
     ${PRODUCT_WITH_VARIANTS_FRAGMENT}
 `;
+
+const DELETE_CHANNEL = gql`
+    mutation DeleteChannel($id: ID!) {
+        deleteChannel(id: $id) {
+            message
+            result
+        }
+    }
+`;

+ 32 - 0
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -1686,6 +1686,8 @@ export type Mutation = {
     createChannel: Channel;
     /** Update an existing Channel */
     updateChannel: Channel;
+    /** Delete a Channel */
+    deleteChannel: DeletionResponse;
     /** Create a new Collection */
     createCollection: Collection;
     /** Update an existing Collection */
@@ -1835,6 +1837,10 @@ export type MutationUpdateChannelArgs = {
     input: UpdateChannelInput;
 };
 
+export type MutationDeleteChannelArgs = {
+    id: Scalars['ID'];
+};
+
 export type MutationCreateCollectionArgs = {
     input: CreateCollectionInput;
 };
@@ -3430,6 +3436,12 @@ export type GetCustomerCountQuery = { __typename?: 'Query' } & {
     customers: { __typename?: 'CustomerList' } & Pick<CustomerList, 'totalItems'>;
 };
 
+export type GetChannelsQueryVariables = {};
+
+export type GetChannelsQuery = { __typename?: 'Query' } & {
+    channels: Array<{ __typename?: 'Channel' } & Pick<Channel, 'id' | 'code' | 'token'>>;
+};
+
 export type AssignProductsToChannelMutationVariables = {
     input: AssignProductsToChannelInput;
 };
@@ -3446,6 +3458,14 @@ export type RemoveProductsFromChannelMutation = { __typename?: 'Mutation' } & {
     removeProductsFromChannel: Array<{ __typename?: 'Product' } & ProductWithVariantsFragment>;
 };
 
+export type DeleteChannelMutationVariables = {
+    id: Scalars['ID'];
+};
+
+export type DeleteChannelMutation = { __typename?: 'Mutation' } & {
+    deleteChannel: { __typename?: 'DeletionResponse' } & Pick<DeletionResponse, 'message' | 'result'>;
+};
+
 export type GetCollectionsWithAssetsQueryVariables = {};
 
 export type GetCollectionsWithAssetsQuery = { __typename?: 'Query' } & {
@@ -5107,6 +5127,12 @@ export namespace GetCustomerCount {
     export type Customers = GetCustomerCountQuery['customers'];
 }
 
+export namespace GetChannels {
+    export type Variables = GetChannelsQueryVariables;
+    export type Query = GetChannelsQuery;
+    export type Channels = NonNullable<GetChannelsQuery['channels'][0]>;
+}
+
 export namespace AssignProductsToChannel {
     export type Variables = AssignProductsToChannelMutationVariables;
     export type Mutation = AssignProductsToChannelMutation;
@@ -5119,6 +5145,12 @@ export namespace RemoveProductsFromChannel {
     export type RemoveProductsFromChannel = ProductWithVariantsFragment;
 }
 
+export namespace DeleteChannel {
+    export type Variables = DeleteChannelMutationVariables;
+    export type Mutation = DeleteChannelMutation;
+    export type DeleteChannel = DeleteChannelMutation['deleteChannel'];
+}
+
 export namespace GetCollectionsWithAssets {
     export type Variables = GetCollectionsWithAssetsQueryVariables;
     export type Query = GetCollectionsWithAssetsQuery;

+ 8 - 0
packages/core/src/api/resolvers/admin/channel.resolver.ts

@@ -1,6 +1,8 @@
 import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
 import {
+    DeletionResponse,
     MutationCreateChannelArgs,
+    MutationDeleteChannelArgs,
     MutationUpdateChannelArgs,
     Permission,
     QueryChannelArgs,
@@ -51,4 +53,10 @@ export class ChannelResolver {
     async updateChannel(@Args() args: MutationUpdateChannelArgs): Promise<Channel> {
         return this.channelService.update(args.input);
     }
+
+    @Mutation()
+    @Allow(Permission.SuperAdmin)
+    async deleteChannel(@Args() args: MutationDeleteChannelArgs): Promise<DeletionResponse> {
+        return this.channelService.delete(args.id);
+    }
 }

+ 3 - 0
packages/core/src/api/schema/admin-api/channel.api.graphql

@@ -10,6 +10,9 @@ type Mutation {
 
     "Update an existing Channel"
     updateChannel(input: UpdateChannelInput!): Channel!
+
+    "Delete a Channel"
+    deleteChannel(id: ID!): DeletionResponse!
 }
 
 input CreateChannelInput {

+ 18 - 1
packages/core/src/service/services/channel.service.ts

@@ -1,6 +1,12 @@
 import { Injectable } from '@nestjs/common';
 import { InjectConnection } from '@nestjs/typeorm';
-import { CreateChannelInput, CurrencyCode, UpdateChannelInput } from '@vendure/common/lib/generated-types';
+import {
+    CreateChannelInput,
+    CurrencyCode,
+    DeletionResponse,
+    DeletionResult,
+    UpdateChannelInput,
+} from '@vendure/common/lib/generated-types';
 import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
 import { ID, Type } from '@vendure/common/lib/shared-types';
 import { unique } from '@vendure/common/lib/unique';
@@ -14,6 +20,7 @@ import { assertFound, idsAreEqual } from '../../common/utils';
 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 { Zone } from '../../entity/zone/zone.entity';
 import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
 import { patchEntity } from '../helpers/utils/patch-entity';
@@ -161,6 +168,16 @@ export class ChannelService {
         return assertFound(this.findOne(channel.id));
     }
 
+    async delete(id: ID): Promise<DeletionResponse> {
+        await this.connection.getRepository(Channel).delete(id);
+        await this.connection.getRepository(ProductVariantPrice).delete({
+            channelId: id,
+        });
+        return {
+            result: DeletionResult.DELETED,
+        };
+    }
+
     /**
      * There must always be a default Channel. If none yet exists, this method creates one.
      * Also ensures the default Channel token matches the defaultChannelToken config setting.

File diff suppressed because it is too large
+ 0 - 0
schema-admin.json


Some files were not shown because too many files changed in this diff