Skip to content

Commit

Permalink
Merge pull request #33 from Relewise/bugfix/range-facets
Browse files Browse the repository at this point in the history
fix: bug with rangefacets when no values are selected
  • Loading branch information
mzanoni authored Jun 19, 2023
2 parents f396dd6 + a536f97 commit b08d52d
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions lib/src/builders/search/facetBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductCategoryAssortmentFacet, BrandFacet, CategoryFacet, ContentAssortmentFacet, ContentDataBooleanValueFacet, ContentDataDoubleRangeFacet, ContentDataDoubleRangesFacet, ContentDataDoubleValueFacet, ContentDataStringValueFacet, FacetSettings, PriceRangeFacet, PriceRangesFacet, ProductAssortmentFacet, ProductCategoryDataBooleanValueFacet, ProductCategoryDataDoubleRangeFacet, ProductCategoryDataDoubleRangesFacet, ProductCategoryDataDoubleValueFacet, ProductCategoryDataStringValueFacet, ProductDataBooleanValueFacet, ProductDataDoubleRangeFacet, ProductDataDoubleRangesFacet, ProductDataDoubleValueFacet, ProductDataStringValueFacet, ProductFacetQuery, VariantSpecificationFacet, ProductDataObjectFacet } from '../../models/data-contracts';
import { ProductCategoryAssortmentFacet, BrandFacet, CategoryFacet, ContentAssortmentFacet, ContentDataBooleanValueFacet, ContentDataDoubleRangeFacet, ContentDataDoubleRangesFacet, ContentDataDoubleValueFacet, ContentDataStringValueFacet, FacetSettings, PriceRangeFacet, PriceRangesFacet, ProductAssortmentFacet, ProductCategoryDataBooleanValueFacet, ProductCategoryDataDoubleRangeFacet, ProductCategoryDataDoubleRangesFacet, ProductCategoryDataDoubleValueFacet, ProductCategoryDataStringValueFacet, ProductDataBooleanValueFacet, ProductDataDoubleRangeFacet, ProductDataDoubleRangesFacet, ProductDataDoubleValueFacet, ProductDataStringValueFacet, ProductFacetQuery, VariantSpecificationFacet, ProductDataObjectFacet, DoubleNullableRange } from '../../models/data-contracts';
import { DataObjectFilterConditionBuilder } from '../dataObjectFilterConditionBuilder';
import { DataObjectFacetBuilder } from './dataObjectFacetBuilder';

Expand Down Expand Up @@ -84,15 +84,14 @@ export class FacetBuilder {
}

public addProductDataDoubleRangeFacet(key: string, selectionStrategy: 'Product' | 'Variant' | 'VariantWithFallbackToProduct' | 'ProductWithFallbackToVariant', lowerBound?: number, upperBound?: number, facetSettings?: FacetSettings): this {
const selected: DoubleNullableRange | null = this.mapSelectedDoubleRange(lowerBound, upperBound);

const facet: ProductDataDoubleRangeFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.ProductDataDoubleRangeFacet, Relewise.Client',
field: 'Data',
key: key,
dataSelectionStrategy: selectionStrategy,
selected: {
lowerBoundInclusive: lowerBound,
upperBoundInclusive: upperBound,
},
selected: selected,
settings: facetSettings,
};
this.facets.push(facet);
Expand Down Expand Up @@ -198,13 +197,11 @@ export class FacetBuilder {
upperBound?: number,
facetSettings?: FacetSettings): this {

const selected: DoubleNullableRange | null = this.mapSelectedDoubleRange(lowerBound, upperBound);
const facet: PriceRangeFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.PriceRangeFacet, Relewise.Client',
field: 'SalesPrice',
selected: {
lowerBoundInclusive: lowerBound,
upperBoundInclusive: upperBound,
},
selected: selected,
priceSelectionStrategy,
settings: facetSettings,
};
Expand All @@ -219,13 +216,11 @@ export class FacetBuilder {
upperBound?: number,
facetSettings?: FacetSettings): this {

const selected: DoubleNullableRange | null = this.mapSelectedDoubleRange(lowerBound, upperBound);
const facet: PriceRangeFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.PriceRangeFacet, Relewise.Client',
field: 'ListPrice',
selected: {
lowerBoundInclusive: lowerBound,
upperBoundInclusive: upperBound,
},
selected: selected,
priceSelectionStrategy,
settings: facetSettings,
};
Expand Down Expand Up @@ -268,7 +263,7 @@ export class FacetBuilder {
filter?: {
conditions?: (builder: DataObjectFilterConditionBuilder) => void,
skip?: number,
take?: number
take?: number
},
facetSettings?: FacetSettings): this {

Expand Down Expand Up @@ -316,10 +311,12 @@ export class FacetBuilder {
}

public addContentDataDoubleRangeFacet(key: string, lowerBound?: number | null, upperBound?: number | null, facetSettings?: FacetSettings): this {
const selected: DoubleNullableRange | null = this.mapSelectedDoubleRange(lowerBound, upperBound);

const facet: ContentDataDoubleRangeFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.ContentDataDoubleRangeFacet, Relewise.Client',
field: 'Data',
selected: { lowerBoundInclusive: lowerBound, upperBoundInclusive: upperBound },
selected: selected,
key: key,
settings: facetSettings,
};
Expand Down Expand Up @@ -413,10 +410,11 @@ export class FacetBuilder {
}

public addProductCategoryDataDoubleRangeFacet(key: string, lowerBound?: number | null, upperBound?: number | null, facetSettings?: FacetSettings): this {
const selected: DoubleNullableRange | null = this.mapSelectedDoubleRange(lowerBound, upperBound);
const facet: ProductCategoryDataDoubleRangeFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.ProductCategoryDataDoubleRangeFacet, Relewise.Client',
field: 'Data',
selected: { lowerBoundInclusive: lowerBound, upperBoundInclusive: upperBound },
selected: selected,
key: key,
settings: facetSettings,
};
Expand Down Expand Up @@ -498,10 +496,22 @@ export class FacetBuilder {
build(): ProductFacetQuery | null {
return this.facets.length === 0
? null
: {
items: this.facets,
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.FacetQuery, Relewise.Client',
: {
items: this.facets,
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.FacetQuery, Relewise.Client',
}
}

private mapSelectedDoubleRange(lowerBound: number | undefined | null, upperBound: number | undefined| null) {
let selected: DoubleNullableRange | null = null;
const lowerBoundHasValue = lowerBound !== null || lowerBound !== undefined;
const upperBoundHasValue = upperBound !== null || upperBound !== undefined;
if (lowerBoundHasValue || upperBoundHasValue) {
selected = {};
if (lowerBoundHasValue) selected.lowerBoundInclusive = lowerBound;
if (upperBoundHasValue) selected.upperBoundInclusive = upperBound;
}
return selected;
}
}

0 comments on commit b08d52d

Please sign in to comment.