Skip to content

Commit

Permalink
fix(core): Return all ProductVariant.channels from default Channel
Browse files Browse the repository at this point in the history
michaelbromley committed Nov 24, 2020
1 parent 1bad22a commit 799f306
Showing 2 changed files with 35 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/core/e2e/product-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -169,6 +169,30 @@ describe('ChannelAware Products and ProductVariants', () => {
);
});

it('ProductVariant.channels includes all Channels from default Channel', async () => {
adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
const { product } = await adminClient.query<
GetProductWithVariants.Query,
GetProductWithVariants.Variables
>(GET_PRODUCT_WITH_VARIANTS, {
id: product1.id,
});

expect(product?.variants[0].channels.map(c => c.id)).toEqual(['T_1', 'T_2']);
});

it('ProductVariant.channels includes only current Channel from non-default Channel', async () => {
adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
const { product } = await adminClient.query<
GetProductWithVariants.Query,
GetProductWithVariants.Variables
>(GET_PRODUCT_WITH_VARIANTS, {
id: product1.id,
});

expect(product?.variants[0].channels.map(c => c.id)).toEqual(['T_2']);
});

it('does not assign Product to same channel twice', async () => {
const { assignProductsToChannel } = await adminClient.query<
AssignProductsToChannel.Mutation,
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Args, Parent, ResolveField, Resolver } from '@nestjs/graphql';
import { StockMovementListOptions } from '@vendure/common/lib/generated-types';
import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
import { PaginatedList } from '@vendure/common/lib/shared-types';

import { Translated } from '../../../common/types/locale-types';
import { idsAreEqual } from '../../../common/utils';
import { Asset, Channel, FacetValue, Product, ProductOption } from '../../../entity';
import { ProductVariant } from '../../../entity/product-variant/product-variant.entity';
import { StockMovement } from '../../../entity/stock-movement/stock-movement.entity';
@@ -100,10 +102,17 @@ export class ProductVariantAdminEntityResolver {

@ResolveField()
async channels(@Ctx() ctx: RequestContext, @Parent() productVariant: ProductVariant): Promise<Channel[]> {
if (productVariant.channels) {
const isDefaultChannel = ctx.channel.code === DEFAULT_CHANNEL_CODE;
if (!isDefaultChannel && productVariant.channels) {
return productVariant.channels;
} else {
return this.productVariantService.getProductVariantChannels(ctx, productVariant.id);
const channels = await this.productVariantService.getProductVariantChannels(
ctx,
productVariant.id,
);
return channels.filter(channel =>
isDefaultChannel ? true : idsAreEqual(channel.id, ctx.channelId),
);
}
}
}

0 comments on commit 799f306

Please sign in to comment.