Skip to content

Commit

Permalink
Merge pull request #74 from Relewise/feat/product-search-constraints
Browse files Browse the repository at this point in the history
Feat: implements product search constraints
  • Loading branch information
mzanoni authored Aug 12, 2024
2 parents 63f536b + c526467 commit 80479fb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/client/src/builders/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export * from './searchTermPredictionBuilder';
export * from './dataObjectValueSelectorBuilder';
export * from './getProductFacet';
export * from './getContentFacet';
export * from './getProductCategoryFacet';
export * from './getProductCategoryFacet';
export * from './searchConstraintBuilder';
12 changes: 11 additions & 1 deletion packages/client/src/builders/search/productSearchBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { ProductSearchRequest, ProductSearchSettings, RecommendationSettings, RetailMediaQuery, SelectedBrandPropertiesSettings, SelectedProductPropertiesSettings, SelectedVariantPropertiesSettings, VariantSearchSettings } from '../../models/data-contracts';
import { ProductSearchRequest, ProductSearchSettings, RecommendationSettings, ResultMustHaveVariantConstraint, RetailMediaQuery, SelectedBrandPropertiesSettings, SelectedProductPropertiesSettings, SelectedVariantPropertiesSettings, VariantSearchSettings } from '../../models/data-contracts';
import { PaginationBuilder } from '../paginationBuilder';
import { Settings } from '../settings';
import { FacetBuilder } from './facetBuilder';
import { ProductSortingBuilder } from './productSortingBuilder';
import { SearchBuilder } from './searchBuilder';
import { SearchConstraintBuilder } from './searchConstraintBuilder';
import { SearchRequestBuilder } from './searchRequestBuilder';

export class ProductSearchBuilder extends SearchRequestBuilder implements SearchBuilder {
private facetBuilder: FacetBuilder = new FacetBuilder();
private retailMediaQuery: RetailMediaQuery | null = null;
private paginationBuilder: PaginationBuilder = new PaginationBuilder();
private sortingBuilder: ProductSortingBuilder = new ProductSortingBuilder();
private searchConstraintBuilder: SearchConstraintBuilder = new SearchConstraintBuilder();
private term: string | null | undefined;

private searchSettings: ProductSearchSettings = {
Expand Down Expand Up @@ -104,6 +106,14 @@ export class ProductSearchBuilder extends SearchRequestBuilder implements Search
return this;
}

public searchConstraints(searchConstraintbuilder: (searchConstraintBuilder: SearchConstraintBuilder) => void): this {
searchConstraintbuilder(this.searchConstraintBuilder);

this.searchSettings.resultConstraint = this.searchConstraintBuilder.build();

return this;
}

public build(): ProductSearchRequest {
const { take, skip } = this.paginationBuilder.build();
return {
Expand Down
21 changes: 21 additions & 0 deletions packages/client/src/builders/search/searchConstraintBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ProductSearchResultConstraint, ResultMustHaveVariantConstraint } from '../../models/data-contracts';

export class SearchConstraintBuilder {
private resultConstraint: ProductSearchResultConstraint | null = null;

public setResultMustHaveVariantConstraint(constaint: { exceptWhenProductHasNoVariants: boolean }): this {
const constraint: ResultMustHaveVariantConstraint = {
$type: 'Relewise.Client.Requests.Search.Settings.ResultMustHaveVariantConstraint, Relewise.Client',
...constaint,
};

this.resultConstraint = constraint;
return this;
}

build(): ResultMustHaveVariantConstraint | null {
// Do to how the data contracts are generated, resultsConstraints on searchSettings expect this specific type
// Once more types are added it will expect the generic type and this cast should be removed and the generic type should be returned
return this.resultConstraint as ResultMustHaveVariantConstraint | null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ test('Facet result', async() => {
}

expect(result?.hits).toBeGreaterThan(0);
});
});

test('ProductSearch with search constraint', async() => {

const request: ProductSearchRequest = baseProductBuilder()
.searchConstraints(constraints => constraints.setResultMustHaveVariantConstraint({ exceptWhenProductHasNoVariants: true }))
.build();

const result = await searcher.searchProducts(request);

expect(result?.hits).toBeGreaterThan(0);
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ test('variantSearchSettings', () => {
.build();

expect(subject.settings?.variantSettings?.excludeResultsWithoutVariant).toBe(true);
});

test('resultMustHaveVariantConstraint', () => {
const subject: ProductSearchRequest = baseBuilder()
.searchConstraints(s => s.setResultMustHaveVariantConstraint({
exceptWhenProductHasNoVariants: true,
}))
.build();

expect(subject.settings?.resultConstraint?.exceptWhenProductHasNoVariants).toBe(true);
});

0 comments on commit 80479fb

Please sign in to comment.