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
Show file tree
Hide file tree
Changes from 1 commit
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
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()

SWH-Relewise marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

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

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

public setResultMustHaveVariantConstraint(constaint: { exceptWhenProductHasNoVariants: boolean }): this {
this.resultConstraint = {
$type: 'Relewise.Client.Requests.Search.Settings.ResultMustHaveVariantConstraint, Relewise.Client',
...constaint,
} as ResultMustHaveVariantConstraint;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.resultConstraint = {
$type: 'Relewise.Client.Requests.Search.Settings.ResultMustHaveVariantConstraint, Relewise.Client',
...constaint,
} as ResultMustHaveVariantConstraint;
this.resultConstraint: ResultMustHaveVariantConstraint = {
$type: 'Relewise.Client.Requests.Search.Settings.ResultMustHaveVariantConstraint, Relewise.Client',
...constaint,
};

Using cast by parses a ton of the Typescript type-checks and should be avoided when possible, like it is here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax is invalid here!
image

Copy link
Collaborator

@mzanoni mzanoni Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just assign it to a local const variable before assigning to this.resultConstraint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do! 🫱🪖

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There you go!

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
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,
SWH-Relewise marked this conversation as resolved.
Show resolved Hide resolved
}))
.build();

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