Skip to content

Commit

Permalink
Add ProductInCartFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
SWH-Relewise committed Aug 21, 2024
1 parent 4efd9c4 commit 8295683
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/client/src/builders/filterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,17 @@ export class FilterBuilder {
return this;
}

/**
* Adds a filter to only return products in the user's cart.
* @param negated - If true, negates the filter (default is false).
* @param options - Optional settings for the filter.
* @returns The ProductFilterBuilder instance for chaining.
*/
public addProductInCartFilter(negated: boolean = false, options?: FilterOptions): this {
this.productFilterBuilder.addProductInCartFilter(negated, options);
return this;
}

/**
* Resets all filters and filter builders.
* @returns The FilterBuilder instance for chaining.
Expand Down
23 changes: 22 additions & 1 deletion packages/client/src/builders/filters/productFilterBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductAndVariantIdFilter, ProductAssortmentFilter, ProductCategoryAssortmentFilter, ProductCategoryHasAncestorFilter, ProductCategoryHasChildFilter, ProductCategoryHasParentFilter, ProductCategoryHasProductsFilter, ProductCategoryIdFilter, ProductCategoryLevelFilter, ProductDataFilter, ProductDisplayNameFilter, ProductHasVariantsFilter, ProductIdFilter, ProductListPriceFilter, ProductRecentlyPurchasedByUserFilter, ProductRecentlyViewedByUserFilter, ProductSalesPriceFilter, ProductAndVariantId, ProductCategoryDataFilter, ProductCategoryDataHasKeyFilter, ProductCategoryDisabledFilter, ProductCategoryRecentlyViewedByUserFilter, ProductDataHasKeyFilter, ProductDisabledFilter, ProductHasCategoriesFilter, ProductRecentlyPurchasedByCompanyFilter, ProductRecentlyPurchasedByUserCompanyFilter, ProductRecentlyPurchasedByUserParentCompanyFilter, ProductRecentlyViewedByCompanyFilter, ProductRecentlyViewedByUserCompanyFilter, ProductRecentlyViewedByUserParentCompanyFilter } from '../../models/data-contracts';
import { ProductAndVariantIdFilter, ProductAssortmentFilter, ProductCategoryAssortmentFilter, ProductCategoryHasAncestorFilter, ProductCategoryHasChildFilter, ProductCategoryHasParentFilter, ProductCategoryHasProductsFilter, ProductCategoryIdFilter, ProductCategoryLevelFilter, ProductDataFilter, ProductDisplayNameFilter, ProductHasVariantsFilter, ProductIdFilter, ProductListPriceFilter, ProductRecentlyPurchasedByUserFilter, ProductRecentlyViewedByUserFilter, ProductSalesPriceFilter, ProductAndVariantId, ProductCategoryDataFilter, ProductCategoryDataHasKeyFilter, ProductCategoryDisabledFilter, ProductCategoryRecentlyViewedByUserFilter, ProductDataHasKeyFilter, ProductDisabledFilter, ProductHasCategoriesFilter, ProductRecentlyPurchasedByCompanyFilter, ProductRecentlyPurchasedByUserCompanyFilter, ProductRecentlyPurchasedByUserParentCompanyFilter, ProductRecentlyViewedByCompanyFilter, ProductRecentlyViewedByUserCompanyFilter, ProductRecentlyViewedByUserParentCompanyFilter, ProductInCartFilter } from '../../models/data-contracts';
import { ConditionBuilder } from '../conditionBuilder';
import { EntityDataFilterOptions, FilterOptions } from './filters.types.shared';
import { FilterSettingsBuilder } from '../filterSettingsBuilder';
Expand Down Expand Up @@ -727,4 +727,25 @@ export class ProductFilterBuilder extends FilterBuilderBase<ProductFilterBuilder

return this;
}

/**
* Adds a filter to only return products in the user's cart.
* @param negated - If true, negates the filter (default is false).
* @param options - Optional settings for the filter.
* @returns The ProductFilterBuilder instance for chaining.
*/
public addProductInCartFilter(negated: boolean = false, options?: FilterOptions): this {
const internalSettingsBuilder = new FilterSettingsBuilder();
options?.filterSettings?.(internalSettingsBuilder);

const filter: ProductInCartFilter = {
$type: 'Relewise.Client.Requests.Filters.ProductInCartFilter, Relewise.Client',
negated: negated,
settings: internalSettingsBuilder.build(),
};

this.filters.push(filter);

return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomUUID } from 'crypto';
import { DataValueFactory, PopularProductsBuilder, ProductRecommendationResponse, ProductsViewedAfterViewingProductBuilder, PurchasedWithProductBuilder, Recommender, UserFactory } from '../../src';
import { test, expect } from '@jest/globals'

Expand Down Expand Up @@ -59,6 +60,28 @@ test('ProductsViewedAfterViewingProduct with all conditions', async() => {

const result: ProductRecommendationResponse | undefined = await recommender.recommendPopularProducts(recommendationBuilder.build());

expect(result).not.toBe(undefined);
expect(result!.recommendations?.length).toBeGreaterThan(0);
});

test('Filter on products in cart', async() => {
settings.user = UserFactory.byTemporaryId(randomUUID());
const recommendationBuilder = new PopularProductsBuilder(settings)
.filters(f => f.addProductInCartFilter());

const result: ProductRecommendationResponse | undefined = await recommender.recommendPopularProducts(recommendationBuilder.build());

expect(result).not.toBe(undefined);
expect(result!.recommendations?.length).toEqual(0);
});

test('Filter on products in cart negated', async() => {
settings.user = UserFactory.byTemporaryId(randomUUID());
const recommendationBuilder = new PopularProductsBuilder(settings)
.filters(f => f.addProductInCartFilter(true));

const result: ProductRecommendationResponse | undefined = await recommender.recommendPopularProducts(recommendationBuilder.build());

expect(result).not.toBe(undefined);
expect(result!.recommendations?.length).toBeGreaterThan(0);
});

0 comments on commit 8295683

Please sign in to comment.