Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: implements product search constraints #74

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/client/src/builders/search/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
@@ -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 {
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;
SWH-Relewise marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
SWH-Relewise marked this conversation as resolved.
Show resolved Hide resolved
}))
.build();

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