Skip to content

Commit

Permalink
feat: product data object facets (#28)
Browse files Browse the repository at this point in the history
* feat: product data object facets

* add last facets, clean up tests

* add missing exports

Co-authored-by: Martin Zanoni <[email protected]>
  • Loading branch information
mzanoni and mzanoni authored Jan 16, 2023
1 parent 34badaf commit 4daa16b
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 46 deletions.
110 changes: 110 additions & 0 deletions lib/src/builders/dataObjectFilterConditionBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { DoubleRange, DataValueBase, ObjectValueMinByCondition, ObjectValueMaxByCondition, ObjectValueContainsCondition, ObjectValueEqualsCondition, ObjectValueGreaterThanCondition, ObjectValueLessThanCondition, ObjectValueInRangeCondition, ObjectValueCondition } from '..';

export type DataObjectFilterConditions =
| ObjectValueContainsCondition
| ObjectValueEqualsCondition
| ObjectValueGreaterThanCondition
| ObjectValueInRangeCondition
| ObjectValueLessThanCondition
| ObjectValueMaxByCondition
| ObjectValueMinByCondition;

export class DataObjectFilterConditionBuilder {
conditions: DataObjectFilterConditions[] = [];

public addContainsCondition<T>(key: string, value: DataValueBase<T>, mode: 'All' | 'Any' = 'All', objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueContainsCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueContainsCondition, Relewise.Client',
key: key,
value: value,
objectPath: objectPath,
mode: mode,
negated: negated,
};
this.conditions.push(condition);

return this;
}

public addEqualsCondition<T>(key: string, value: DataValueBase<T>, objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueEqualsCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueEqualsCondition, Relewise.Client',
value: value,
objectPath: objectPath,
negated: negated,
key: key,
};
this.conditions.push(condition);

return this;
}

public addInRangeCondition(key: string, range: DoubleRange, objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueInRangeCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueInRangeCondition, Relewise.Client',
range: range,
key: key,
objectPath: objectPath,
negated: negated,
};
this.conditions.push(condition);

return this;
}

public addGreaterThanCondition(key: string, value: number, objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueGreaterThanCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueGreaterThanCondition, Relewise.Client',
value: value,
negated: negated,
key: key,
objectPath: objectPath,
};
this.conditions.push(condition);

return this;
}

public addLessThanCondition(key: string, value: number, objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueLessThanCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueLessThanCondition, Relewise.Client',
value: value,
negated: negated,
key: key,
objectPath: objectPath,
};
this.conditions.push(condition);

return this;
}

public addMinByCondition(key: string, objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueMinByCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueMinByCondition, Relewise.Client',
negated: negated,
key: key,
objectPath: objectPath,
};
this.conditions.push(condition);

return this;
}

public addMaxByCondition(key: string, objectPath?: string[], negated: boolean = false): this {
const condition: ObjectValueMaxByCondition = {
$type: 'Relewise.Client.Requests.Filters.DataObjects.Conditions.ObjectValueMaxByCondition, Relewise.Client',
negated: negated,
key: key,
objectPath: objectPath,
};
this.conditions.push(condition);

return this;
}

public build(): DataObjectFilterConditions[] | null {
return this.conditions.length === 0
? null
: this.conditions
}
}
1 change: 1 addition & 0 deletions lib/src/builders/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './settings';
export * from './dataObjectFilterConditionBuilder';
export * from './filterBuilder';
export * from './paginationBuilder';
export * from './conditionBuilder';
Expand Down
122 changes: 122 additions & 0 deletions lib/src/builders/search/dataObjectFacetBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { DataObjectBooleanValueFacet, DataObjectDoubleRangeFacet, DataObjectDoubleRangesFacet, DataObjectDoubleValueFacet, DataObjectStringValueFacet, FacetSettings } from '../../models/data-contracts';


export class DataObjectFacetBuilder {
private facets: (
DataObjectDoubleRangeFacet |
DataObjectDoubleRangesFacet |
DataObjectStringValueFacet |
DataObjectBooleanValueFacet |
DataObjectDoubleValueFacet)[] = [];

public addStringFacet(
key: string,
selectedValues: string[] | null = null,
collectionFilterType?: 'Or' | 'And',
facetSettings?: FacetSettings): this {

const facet: DataObjectStringValueFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.DataObjectStringValueFacet, Relewise.Client',
field: 'Data',
key: key,
selected: selectedValues,
collectionFilterType: collectionFilterType,
settings: facetSettings,
};
this.facets.push(facet);

return this;
}

public addBooleanFacet(
key: string,
selectedValues: boolean[] | null = null,
collectionFilterType?: 'Or' | 'And',
facetSettings?: FacetSettings): this {

const facet: DataObjectBooleanValueFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.DataObjectBooleanValueFacet, Relewise.Client',
field: 'Data',
key: key,
selected: selectedValues,
collectionFilterType: collectionFilterType,
settings: facetSettings,
};
this.facets.push(facet);

return this;
}

public addNumberFacet(
key: string,
selectedValues: number[] | null = null,
collectionFilterType?: 'Or' | 'And',
facetSettings?: FacetSettings): this {

const facet: DataObjectDoubleValueFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.DataObjectDoubleValueFacet, Relewise.Client',
field: 'Data',
key: key,
selected: selectedValues,
collectionFilterType: collectionFilterType,
settings: facetSettings,
};
this.facets.push(facet);

return this;
}

public addNumberRangeFacet(
key: string,
lowerBound?: number | null,
upperBound?: number | null,
facetSettings?: FacetSettings): this {

const facet: DataObjectDoubleRangeFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.DataObjectDoubleRangeFacet, Relewise.Client',
field: 'Data',
key: key,
selected: {
lowerBoundInclusive: lowerBound,
upperBoundInclusive: upperBound,
},
settings: facetSettings,
};
this.facets.push(facet);

return this;
}

public addNumberRangesFacet(
key: string,
predefinedRanges?: {
lowerBound?: number,
upperBound?: number
}[] | null,
expandedRangeSize?: number | null,
selectedValues: {
lowerBound?: number,
upperBound?: number
}[] | null = null,
facetSettings?: FacetSettings): this {

const facet: DataObjectDoubleRangesFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.DataObjectDoubleRangesFacet, Relewise.Client',
field: 'Data',
key: key,
settings: facetSettings,
predefinedRanges: predefinedRanges?.map(x => ({ lowerBoundInclusive: x.lowerBound, upperBoundExclusive: x.upperBound })),
expandedRangeSize: expandedRangeSize,
selected: selectedValues?.map(x => ({ lowerBoundInclusive: x.lowerBound, upperBoundExclusive: x.upperBound })),
};
this.facets.push(facet);

return this;
}

build() {
return this.facets.length === 0
? null
: this.facets;
}
}
50 changes: 46 additions & 4 deletions lib/src/builders/search/facetBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 } 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 } from '../../models/data-contracts';
import { DataObjectFilterConditionBuilder } from '../dataObjectFilterConditionBuilder';
import { DataObjectFacetBuilder } from './dataObjectFacetBuilder';

export class FacetBuilder {
private facets: (
Expand All @@ -25,6 +27,7 @@ export class FacetBuilder {
| ProductCategoryDataBooleanValueFacet
| ProductCategoryDataDoubleValueFacet
| ProductCategoryDataDoubleRangesFacet
| ProductDataObjectFacet
)[] = [];

//#region Product
Expand Down Expand Up @@ -257,6 +260,44 @@ export class FacetBuilder {

return this;
}

public addProductDataObjectFacet(
key: string,
selectionStrategy: 'Product' | 'Variant' | 'VariantWithFallbackToProduct' | 'ProductWithFallbackToVariant',
builder?: (facets: DataObjectFacetBuilder) => void,
filter?: {
conditions?: (builder: DataObjectFilterConditionBuilder) => void,
skip?: number,
take?: number},
facetSettings?: FacetSettings): this {

const facetBuilder = new DataObjectFacetBuilder();
if (builder) {
builder(facetBuilder);
}

const conditionsBuilder = new DataObjectFilterConditionBuilder();
if (filter?.conditions) {
filter?.conditions(conditionsBuilder);
}

const facet: ProductDataObjectFacet = {
$type: 'Relewise.Client.DataTypes.Search.Facets.Queries.ProductDataObjectFacet, Relewise.Client',
field: 'Data',
items: facetBuilder.build() ?? [],
filter: {
conditions: conditionsBuilder.build() ?? [],
take: filter?.take,
skip: filter?.skip,
},
dataSelectionStrategy: selectionStrategy,
settings: facetSettings,
key: key,
};
this.facets.push(facet);

return this;
}
//#endregion

//#region Content
Expand Down Expand Up @@ -296,7 +337,7 @@ export class FacetBuilder {
selectedValues: {
lowerBound?: number,
upperBound?: number
}[] | null = null,
}[] | null = null,
facetSettings?: FacetSettings): this {

const facet: ContentDataDoubleRangesFacet = {
Expand Down Expand Up @@ -393,7 +434,7 @@ export class FacetBuilder {
selectedValues: {
lowerBound?: number,
upperBound?: number
}[] | null = null,
}[] | null = null,
facetSettings?: FacetSettings): this {

const facet: ProductCategoryDataDoubleRangesFacet = {
Expand Down Expand Up @@ -458,4 +499,5 @@ export class FacetBuilder {
? null
: { items: this.facets }
}
}
}

1 change: 1 addition & 0 deletions lib/src/builders/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './dataObjectFacetBuilder';
export * from './facetBuilder';
export * from './searchBuilder';
export * from './productSortingBuilder';
Expand Down
Loading

0 comments on commit 4daa16b

Please sign in to comment.