Skip to content

Commit

Permalink
feat(core): Collection preview handles filter inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 18, 2022
1 parent fd67ac2 commit 3d2c0fb
Show file tree
Hide file tree
Showing 9 changed files with 10,417 additions and 10,558 deletions.
6,902 changes: 3,403 additions & 3,499 deletions packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3729,6 +3729,7 @@ export type PermissionDefinition = {

export type PreviewCollectionVariantsInput = {
filters: Array<ConfigurableOperationInput>;
inheritFilters: Scalars['Boolean'];
parentId?: InputMaybe<Scalars['ID']>;
};

Expand Down
109 changes: 76 additions & 33 deletions packages/core/e2e/collection.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ import { productIdCollectionFilter, variantIdCollectionFilter } from '../src/ind
import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
import {
CollectionFragment,
CreateCollectionInput,
CreateCollectionMutation,
DeletionResult,
FacetValueFragment,
GetAssetListQuery,
GetProductsWithVariantIdsQuery,
LanguageCode,
SortOrder,
} from './graphql/generated-e2e-admin-types';
Expand All @@ -47,7 +43,7 @@ describe('Collection resolver', () => {
plugins: [DefaultJobQueuePlugin],
});

let assets: GetAssetListQuery['assets']['items'];
let assets: Codegen.GetAssetListQuery['assets']['items'];
let facetValues: FacetValueFragment[];
let electronicsCollection: CollectionFragment;
let computersCollection: CollectionFragment;
Expand Down Expand Up @@ -874,8 +870,8 @@ describe('Collection resolver', () => {
});

describe('deleteCollection', () => {
let collectionToDeleteParent: CreateCollectionMutation['createCollection'];
let collectionToDeleteChild: CreateCollectionMutation['createCollection'];
let collectionToDeleteParent: Codegen.CreateCollectionMutation['createCollection'];
let collectionToDeleteChild: Codegen.CreateCollectionMutation['createCollection'];
let laptopProductId: string;

beforeAll(async () => {
Expand Down Expand Up @@ -1075,7 +1071,7 @@ describe('Collection resolver', () => {
{ languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
],
filters: [],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});
expect(result.createCollection.productVariants.totalItems).toBe(0);
});
Expand Down Expand Up @@ -1172,7 +1168,7 @@ describe('Collection resolver', () => {
],
},
],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});

await awaitRunningJobs(adminClient, 5000);
Expand Down Expand Up @@ -1217,7 +1213,7 @@ describe('Collection resolver', () => {
],
},
],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});

await awaitRunningJobs(adminClient, 5000);
Expand Down Expand Up @@ -1271,7 +1267,7 @@ describe('Collection resolver', () => {
],
},
],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});

await awaitRunningJobs(adminClient, 5000);
Expand Down Expand Up @@ -1449,8 +1445,8 @@ describe('Collection resolver', () => {
describe('variantId filter', () => {
it('contains expects variants', async () => {
const { createCollection } = await adminClient.query<
CreateCollection.Mutation,
CreateCollection.Variables
Codegen.CreateCollectionMutation,
Codegen.CreateCollectionMutationVariables
>(CREATE_COLLECTION, {
input: {
translations: [
Expand All @@ -1477,8 +1473,8 @@ describe('Collection resolver', () => {
await awaitRunningJobs(adminClient, 5000);

const result = await adminClient.query<
GetCollectionProducts.Query,
GetCollectionProducts.Variables
Codegen.GetCollectionProductsQuery,
Codegen.GetCollectionProductsQueryVariables
>(GET_COLLECTION_PRODUCT_VARIANTS, {
id: createCollection.id,
});
Expand All @@ -1492,8 +1488,8 @@ describe('Collection resolver', () => {
describe('productId filter', () => {
it('contains expects variants', async () => {
const { createCollection } = await adminClient.query<
CreateCollection.Mutation,
CreateCollection.Variables
Codegen.CreateCollectionMutation,
Codegen.CreateCollectionMutationVariables
>(CREATE_COLLECTION, {
input: {
translations: [
Expand All @@ -1520,8 +1516,8 @@ describe('Collection resolver', () => {
await awaitRunningJobs(adminClient, 5000);

const result = await adminClient.query<
GetCollectionProducts.Query,
GetCollectionProducts.Variables
Codegen.GetCollectionProductsQuery,
Codegen.GetCollectionProductsQueryVariables
>(GET_COLLECTION_PRODUCT_VARIANTS, {
id: createCollection.id,
});
Expand All @@ -1533,7 +1529,7 @@ describe('Collection resolver', () => {
});

describe('re-evaluation of contents on changes', () => {
let products: GetProductsWithVariantIdsQuery['products']['items'];
let products: Codegen.GetProductsWithVariantIdsQuery['products']['items'];

beforeAll(async () => {
const result = await adminClient.query<Codegen.GetProductsWithVariantIdsQuery>(gql`
Expand Down Expand Up @@ -1687,7 +1683,7 @@ describe('Collection resolver', () => {
],
},
],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});

await awaitRunningJobs(adminClient, 5000);
Expand Down Expand Up @@ -1740,7 +1736,7 @@ describe('Collection resolver', () => {
],
},
],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});

await awaitRunningJobs(adminClient, 5000);
Expand Down Expand Up @@ -1785,7 +1781,7 @@ describe('Collection resolver', () => {
],
},
],
} as CreateCollectionInput,
} as Codegen.CreateCollectionInput,
});

await awaitRunningJobs(adminClient, 5000);
Expand All @@ -1800,10 +1796,13 @@ describe('Collection resolver', () => {

describe('previewCollectionVariants', () => {
it('returns correct contents', async () => {
const {previewCollectionVariants} = await adminClient.query<Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables>(PREVIEW_COLLECTION_VARIANTS, {
const { previewCollectionVariants } = await adminClient.query<
Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables
>(PREVIEW_COLLECTION_VARIANTS, {
input: {
parentId: electronicsCollection.parent?.id,
inheritFilters: true,
filters: [
{
code: facetValueCollectionFilter.code,
Expand Down Expand Up @@ -1836,10 +1835,13 @@ describe('Collection resolver', () => {
});

it('works with list options', async () => {
const {previewCollectionVariants} = await adminClient.query<Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables>(PREVIEW_COLLECTION_VARIANTS, {
const { previewCollectionVariants } = await adminClient.query<
Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables
>(PREVIEW_COLLECTION_VARIANTS, {
input: {
parentId: electronicsCollection.parent?.id,
inheritFilters: true,
filters: [
{
code: facetValueCollectionFilter.code,
Expand Down Expand Up @@ -1869,16 +1871,53 @@ describe('Collection resolver', () => {
},
});
expect(previewCollectionVariants.items).toEqual([
{id: 'T_5', name: 'Curvy Monitor 24 inch'},
{id: 'T_6', name: 'Curvy Monitor 27 inch'},
{ id: 'T_5', name: 'Curvy Monitor 24 inch' },
{ id: 'T_6', name: 'Curvy Monitor 27 inch' },
]);
});

it('takes parent filters into account', async () => {
const {previewCollectionVariants} = await adminClient.query<Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables>(PREVIEW_COLLECTION_VARIANTS, {
const { previewCollectionVariants } = await adminClient.query<
Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables
>(PREVIEW_COLLECTION_VARIANTS, {
input: {
parentId: electronicsCollection.id,
inheritFilters: true,
filters: [
{
code: variantNameCollectionFilter.code,
arguments: [
{
name: 'operator',
value: 'startsWith',
},
{
name: 'term',
value: 'h',
},
],
},
],
},
});
expect(previewCollectionVariants.items.map(i => i.name).sort()).toEqual([
'Hard Drive 1TB',
'Hard Drive 2TB',
'Hard Drive 3TB',
'Hard Drive 4TB',
'Hard Drive 6TB',
]);
});

it('ignores parent filters id inheritFilters set to false', async () => {
const { previewCollectionVariants } = await adminClient.query<
Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables
>(PREVIEW_COLLECTION_VARIANTS, {
input: {
parentId: electronicsCollection.id,
inheritFilters: false,
filters: [
{
code: variantNameCollectionFilter.code,
Expand All @@ -1902,13 +1941,17 @@ describe('Collection resolver', () => {
'Hard Drive 3TB',
'Hard Drive 4TB',
'Hard Drive 6TB',
'Hat',
]);
});

it('with no parentId, operates at the root level', async () => {
const {previewCollectionVariants} = await adminClient.query<Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables>(PREVIEW_COLLECTION_VARIANTS, {
const { previewCollectionVariants } = await adminClient.query<
Codegen.PreviewCollectionVariantsQuery,
Codegen.PreviewCollectionVariantsQueryVariables
>(PREVIEW_COLLECTION_VARIANTS, {
input: {
inheritFilters: true,
filters: [
{
code: variantNameCollectionFilter.code,
Expand Down
1 change: 1 addition & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3556,6 +3556,7 @@ export type PermissionDefinition = {

export type PreviewCollectionVariantsInput = {
filters: Array<ConfigurableOperationInput>;
inheritFilters: Scalars['Boolean'];
parentId?: InputMaybe<Scalars['ID']>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ input CreateCollectionInput {

input PreviewCollectionVariantsInput {
parentId: ID
inheritFilters: Boolean!
filters: [ConfigurableOperationInput!]!
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/services/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export class CollectionService implements OnModuleInit {
relations?: RelationPaths<Collection>,
): Promise<PaginatedList<ProductVariant>> {
const applicableFilters = this.getCollectionFiltersFromInput(input);
if (input.parentId) {
if (input.parentId && input.inheritFilters) {
const parentFilters = (await this.findOne(ctx, input.parentId, []))?.filters ?? [];
const ancestorFilters = await this.getAncestors(input.parentId).then(ancestors =>
ancestors.reduce(
Expand Down
Loading

0 comments on commit 3d2c0fb

Please sign in to comment.