Skip to content

Commit

Permalink
feat(core): Implement deleteChannel mutation
Browse files Browse the repository at this point in the history
Relates to #12
  • Loading branch information
michaelbromley committed Nov 7, 2019
1 parent 6a165dc commit 989960b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -1840,6 +1842,11 @@ export type MutationUpdateChannelArgs = {
};


export type MutationDeleteChannelArgs = {
id: Scalars['ID']
};


export type MutationCreateCollectionArgs = {
input: CreateCollectionInput
};
Expand Down
57 changes: 57 additions & 0 deletions packages/core/e2e/channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
CreateProduct,
CreateRole,
CurrencyCode,
DeleteChannel,
DeletionResult,
GetChannels,
GetCustomerList,
GetProductWithVariants,
LanguageCode,
Expand Down Expand Up @@ -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) {
Expand All @@ -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 changes: 32 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -1835,6 +1837,10 @@ export type MutationUpdateChannelArgs = {
input: UpdateChannelInput;
};

export type MutationDeleteChannelArgs = {
id: Scalars['ID'];
};

export type MutationCreateCollectionArgs = {
input: CreateCollectionInput;
};
Expand Down Expand Up @@ -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;
};
Expand All @@ -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' } & {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/api/resolvers/admin/channel.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import {
DeletionResponse,
MutationCreateChannelArgs,
MutationDeleteChannelArgs,
MutationUpdateChannelArgs,
Permission,
QueryChannelArgs,
Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions packages/core/src/api/schema/admin-api/channel.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Mutation {

"Update an existing Channel"
updateChannel(input: UpdateChannelInput!): Channel!

"Delete a Channel"
deleteChannel(id: ID!): DeletionResponse!
}

input CreateChannelInput {
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

0 comments on commit 989960b

Please sign in to comment.