-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add search-products method #1004
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
Changes from all commits
d7fb174
250c138
4ac9425
c550b5d
864a4d6
012ada5
187203a
34f0295
d9b6fb9
69179ba
fd38c55
13e086d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * as subgraph from "./subgraph"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { Web3LibAdapter } from "@bosonprotocol/common"; | ||
levalleux-ludo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { BaseCoreSDK } from "../mixins/base-core-sdk"; | ||
| import { searchProducts } from "./subgraph"; | ||
| import * as subgraph from "../subgraph"; | ||
|
|
||
| export class SearchMixin<T extends Web3LibAdapter> extends BaseCoreSDK<T> { | ||
levalleux-ludo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Search for products matching the given keywords. | ||
| * | ||
| * By default, only products that are currently valid (based on their validity dates) are returned. | ||
| * This behavior can be controlled via the `includeInvalidOffers` option in the queryVars parameter. | ||
| * | ||
| * @param keywords - List of keywords to match against product title, description, and tags. | ||
| * @param queryVars - Optional query variables including pagination (productsSkip, productsFirst), | ||
| * ordering (productsOrderBy, productsOrderDirection), filtering (productsFilter), | ||
| * and includeInvalidOffers flag to control validity filtering. | ||
| * @returns A promise that resolves to an array of product search result fragments. | ||
| */ | ||
levalleux-ludo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public async searchProducts( | ||
| keywords: string[], | ||
| queryVars?: subgraph.SearchProductsQueryQueryVariables & { | ||
| includeInvalidOffers?: boolean; | ||
| } | ||
| ): Promise<subgraph.ProductSearchResultFieldsFragment[]> { | ||
levalleux-ludo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!keywords || keywords.length === 0) { | ||
| return []; | ||
| } | ||
| const now = Math.floor(Date.now() / 1000); | ||
| const validOffersFilter: subgraph.ProductV1Product_Filter = { | ||
| allVariantsVoided_not: true, | ||
| minValidFromDate_lte: now + 60 + "", // Add 1 minute to ensure we include offers not valid yet, but valid in a very little time | ||
| maxValidUntilDate_gte: now + "" | ||
| }; | ||
| const productsSearchFilter: subgraph.ProductV1Product_Filter = { | ||
| or: keywords | ||
| .map((keyword) => [ | ||
| { title_contains_nocase: keyword }, | ||
| { description_contains_nocase: keyword }, | ||
| { details_tags_contains_nocase: [keyword] } | ||
| ]) | ||
| .flat() | ||
| }; | ||
| const productsFilter: subgraph.ProductV1Product_Filter = | ||
| queryVars?.includeInvalidOffers | ||
| ? queryVars?.productsFilter | ||
| ? { | ||
| and: [ | ||
| { | ||
| ...queryVars.productsFilter | ||
| }, | ||
| productsSearchFilter | ||
| ] | ||
| } | ||
| : productsSearchFilter | ||
| : { | ||
| and: [ | ||
| { | ||
| ...validOffersFilter, | ||
| ...queryVars?.productsFilter | ||
| }, | ||
| productsSearchFilter | ||
| ] | ||
levalleux-ludo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| return searchProducts(this._subgraphUrl, { | ||
| ...queryVars, | ||
| productsFilter | ||
| }); | ||
|
Comment on lines
+64
to
+67
|
||
| } | ||
levalleux-ludo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.