Skip to content

Commit

Permalink
feat(core): Update search index on Product assigned/removed from Channel
Browse files Browse the repository at this point in the history
Relates to #12
  • Loading branch information
michaelbromley committed Nov 8, 2019
1 parent 280a38b commit 3a6c277
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 71 deletions.
25 changes: 2 additions & 23 deletions packages/core/e2e/channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import path from 'path';

import { dataDir, TEST_SETUP_TIMEOUT_MS, testConfig } from './config/test-config';
import { initialData } from './fixtures/e2e-initial-data';
import { PRODUCT_WITH_VARIANTS_FRAGMENT } from './graphql/fragments';
import {
AssignProductsToChannel,
CreateAdministrator,
CreateChannel,
CreateProduct,
CreateRole,
CurrencyCode,
DeleteChannel,
Expand All @@ -23,17 +21,16 @@ import {
Me,
Permission,
RemoveProductsFromChannel,
UpdateProduct,
} from './graphql/generated-e2e-admin-types';
import {
ASSIGN_PRODUCT_TO_CHANNEL,
CREATE_ADMINISTRATOR,
CREATE_CHANNEL,
CREATE_PRODUCT,
CREATE_ROLE,
GET_CUSTOMER_LIST,
GET_PRODUCT_WITH_VARIANTS,
ME,
UPDATE_PRODUCT,
REMOVE_PRODUCT_FROM_CHANNEL,
} from './graphql/shared-definitions';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';

Expand Down Expand Up @@ -401,24 +398,6 @@ const GET_CHANNELS = gql`
}
`;

const ASSIGN_PRODUCT_TO_CHANNEL = gql`
mutation AssignProductsToChannel($input: AssignProductsToChannelInput!) {
assignProductsToChannel(input: $input) {
...ProductWithVariants
}
}
${PRODUCT_WITH_VARIANTS_FRAGMENT}
`;

const REMOVE_PRODUCT_FROM_CHANNEL = gql`
mutation RemoveProductsFromChannel($input: RemoveProductsFromChannelInput!) {
removeProductsFromChannel(input: $input) {
...ProductWithVariants
}
}
${PRODUCT_WITH_VARIANTS_FRAGMENT}
`;

const DELETE_CHANNEL = gql`
mutation DeleteChannel($id: ID!) {
deleteChannel(id: $id) {
Expand Down
63 changes: 62 additions & 1 deletion packages/core/e2e/default-search-plugin.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { pick } from '@vendure/common/lib/pick';
import { DefaultSearchPlugin, mergeConfig } from '@vendure/core';
import { facetValueCollectionFilter } from '@vendure/core/dist/config/collection/default-collection-filters';
import { createTestEnvironment, SimpleGraphQLClient } from '@vendure/testing';
import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN, SimpleGraphQLClient } from '@vendure/testing';
import gql from 'graphql-tag';
import path from 'path';

import { dataDir, TEST_SETUP_TIMEOUT_MS, testConfig } from './config/test-config';
import { initialData } from './fixtures/e2e-initial-data';
import {
AssignProductsToChannel,
CreateChannel,
CreateCollection,
CreateFacet,
CurrencyCode,
DeleteProduct,
DeleteProductVariant,
LanguageCode,
RemoveProductsFromChannel,
SearchFacetValues,
SearchGetPrices,
SearchInput,
Expand All @@ -23,10 +27,13 @@ import {
} from './graphql/generated-e2e-admin-types';
import { SearchProductsShop } from './graphql/generated-e2e-shop-types';
import {
ASSIGN_PRODUCT_TO_CHANNEL,
CREATE_CHANNEL,
CREATE_COLLECTION,
CREATE_FACET,
DELETE_PRODUCT,
DELETE_PRODUCT_VARIANT,
REMOVE_PRODUCT_FROM_CHANNEL,
UPDATE_COLLECTION,
UPDATE_PRODUCT,
UPDATE_PRODUCT_VARIANTS,
Expand Down Expand Up @@ -616,6 +623,60 @@ describe('Default search plugin', () => {
]);
});
});

describe('channel handling', () => {
const SECOND_CHANNEL_TOKEN = 'second-channel-token';
let secondChannel: CreateChannel.CreateChannel;

beforeAll(async () => {
const { createChannel } = await adminClient.query<
CreateChannel.Mutation,
CreateChannel.Variables
>(CREATE_CHANNEL, {
input: {
code: 'second-channel',
token: SECOND_CHANNEL_TOKEN,
defaultLanguageCode: LanguageCode.en,
currencyCode: CurrencyCode.GBP,
pricesIncludeTax: true,
},
});
secondChannel = createChannel;
});

it('adding product to channel', async () => {
adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
await adminClient.query<AssignProductsToChannel.Mutation, AssignProductsToChannel.Variables>(
ASSIGN_PRODUCT_TO_CHANNEL,
{
input: { channelId: secondChannel.id, productIds: ['T_1', 'T_2'] },
},
);
await awaitRunningJobs(adminClient);

adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
const { search } = await doAdminSearchQuery({ groupByProduct: true });
expect(search.items.map(i => i.productId)).toEqual(['T_1', 'T_2']);
}, 10000);

it('removing product from channel', async () => {
adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
const { removeProductsFromChannel } = await adminClient.query<
RemoveProductsFromChannel.Mutation,
RemoveProductsFromChannel.Variables
>(REMOVE_PRODUCT_FROM_CHANNEL, {
input: {
productIds: ['T_2'],
channelId: secondChannel.id,
},
});
await awaitRunningJobs(adminClient);

adminClient.setChannelToken(SECOND_CHANNEL_TOKEN);
const { search } = await doAdminSearchQuery({ groupByProduct: true });
expect(search.items.map(i => i.productId)).toEqual(['T_1']);
}, 10000);
});
});
});

Expand Down
18 changes: 18 additions & 0 deletions packages/core/e2e/graphql/shared-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,21 @@ export const DELETE_PRODUCT_VARIANT = gql`
}
}
`;

export const ASSIGN_PRODUCT_TO_CHANNEL = gql`
mutation AssignProductsToChannel($input: AssignProductsToChannelInput!) {
assignProductsToChannel(input: $input) {
...ProductWithVariants
}
}
${PRODUCT_WITH_VARIANTS_FRAGMENT}
`;

export const REMOVE_PRODUCT_FROM_CHANNEL = gql`
mutation RemoveProductsFromChannel($input: RemoveProductsFromChannelInput!) {
removeProductsFromChannel(input: $input) {
...ProductWithVariants
}
}
${PRODUCT_WITH_VARIANTS_FRAGMENT}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { buffer, debounceTime, filter, map } from 'rxjs/operators';
import { idsAreEqual } from '../../common/utils';
import { EventBus } from '../../event-bus/event-bus';
import { CollectionModificationEvent } from '../../event-bus/events/collection-modification-event';
import { ProductChannelEvent } from '../../event-bus/events/product-channel-event';
import { ProductEvent } from '../../event-bus/events/product-event';
import { ProductVariantEvent } from '../../event-bus/events/product-variant-event';
import { TaxRateModificationEvent } from '../../event-bus/events/tax-rate-modification-event';
Expand Down Expand Up @@ -79,6 +80,17 @@ export class DefaultSearchPlugin implements OnVendureBootstrap {
return this.searchIndexService.updateVariants(event.ctx, event.variants).start();
}
});
this.eventBus.ofType(ProductChannelEvent).subscribe(event => {
if (event.type === 'assigned') {
return this.searchIndexService
.assignProductToChannel(event.ctx, event.product.id, event.channelId)
.start();
} else {
return this.searchIndexService
.removeProductFromChannel(event.ctx, event.product.id, event.channelId)
.start();
}
});

const collectionModification$ = this.eventBus.ofType(CollectionModificationEvent);
const closingNotifier$ = collectionModification$.pipe(debounceTime(50));
Expand Down

0 comments on commit 3a6c277

Please sign in to comment.