Skip to content

Commit

Permalink
Revert "fix: Fuzzy & operator usage when handling Intelligent Search …
Browse files Browse the repository at this point in the history
…API requests (#2204)"

This reverts commit dd049cd.
  • Loading branch information
eduardoformiga committed Aug 29, 2024
1 parent 98c7c74 commit 1c76451
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 206 deletions.
22 changes: 11 additions & 11 deletions packages/api/mocks/AllProductsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ export const AllProductsQueryFirst5 = `query AllProducts {
`

export const productSearchPage1Count5Fetch = {
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=5&query=&sort=&locale=en-US&hideUnavailableItems=false',
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=5&query=&sort=&fuzzy=auto&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
products: [
{
cacheId: 'sp-99995946',
productId: '99995946',
description: '4k Philips Monitor 27"',
productName: '4k Philips Monitor 27"',
description: '4k Philips Monitor 27\"',
productName: '4k Philips Monitor 27\"',
linkText: '4k-philips-monitor',
brand: 'adidas',
brandId: 2000004,
Expand Down Expand Up @@ -209,7 +209,7 @@ export const productSearchPage1Count5Fetch = {
],
itemId: '99988213',
name: 'Monitor 27',
nameComplete: '4k Philips Monitor 27" Monitor 27',
nameComplete: '4k Philips Monitor 27\" Monitor 27',
complementName: '',
referenceId: [
{
Expand Down Expand Up @@ -831,36 +831,36 @@ export const productSearchPage1Count5Fetch = {
current: {
index: 1,
proxyUrl:
'search/trade-policy/1?page=1&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=1&count=5&query=&sort=&fuzzy=auto&operator=and',
},
before: [],
after: [
{
index: 2,
proxyUrl:
'search/trade-policy/1?page=2&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=2&count=5&query=&sort=&fuzzy=auto&operator=and',
},
{
index: 3,
proxyUrl:
'search/trade-policy/1?page=3&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=3&count=5&query=&sort=&fuzzy=auto&operator=and',
},
{
index: 4,
proxyUrl:
'search/trade-policy/1?page=4&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=4&count=5&query=&sort=&fuzzy=auto&operator=and',
},
{
index: 5,
proxyUrl:
'search/trade-policy/1?page=5&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=5&count=5&query=&sort=&fuzzy=auto&operator=and',
},
],
perPage: 5,
next: {
index: 2,
proxyUrl:
'search/trade-policy/1?page=2&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=2&count=5&query=&sort=&fuzzy=auto&operator=and',
},
previous: {
index: 0,
Expand All @@ -871,7 +871,7 @@ export const productSearchPage1Count5Fetch = {
last: {
index: 50,
proxyUrl:
'search/trade-policy/1?page=50&count=5&query=&sort=&operator=and&fuzzy=0',
'search/trade-policy/1?page=50&count=5&query=&sort=&fuzzy=auto&operator=and',
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions packages/api/mocks/ProductQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const ProductByIdQuery = `query ProductQuery {
`

export const productSearchFetch = {
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=1&query=sku%3A64953394&sort=&locale=en-US&hideUnavailableItems=false',
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=1&query=sku%3A64953394&sort=&fuzzy=auto&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
Expand Down Expand Up @@ -229,15 +229,15 @@ export const productSearchFetch = {
correction: {
misspelled: true,
},
fuzzy: '0',
fuzzy: 'auto',
operator: 'and',
translated: false,
pagination: {
count: 1,
current: {
index: 1,
proxyUrl:
'search/trade-policy/1?page=1&count=1&query=sku:64953394&sort=&locale=en-US&hide-unavailable-items=false&operator=and&fuzzy=0',
'search/trade-policy/1?page=1&count=1&query=sku:64953394&sort=&fuzzy=auto&operator=and',
},
before: [],
after: [],
Expand Down
62 changes: 31 additions & 31 deletions packages/api/mocks/RedirectQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ export const RedirectQueryTermTech = `query RedirectSearch {
`

export const redirectTermTechFetch = {
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=2&count=1&query=tech&sort=&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
products: [],
recordsFiltered: 0,
fuzzy: 'auto',
operator: 'and',
redirect: '/technology',
translated: false,
pagination: {
count: 1,
current: {
index: 0,
},
before: [],
after: [],
perPage: 0,
next: {
index: 0,
},
previous: {
index: 0,
},
first: {
index: 0,
},
last: {
index: 0,
},
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=2&count=1&query=tech&sort=&fuzzy=auto&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
products: [],
recordsFiltered: 0,
fuzzy: 'auto',
operator: 'and',
redirect: '/technology',
translated: false,
pagination: {
count: 1,
current: {
index: 0,
},
before: [],
after: [],
perPage: 0,
next: {
index: 0,
},
previous: {
index: 0,
},
first: {
index: 0,
},
last: {
index: 0,
},
},
},
},
}
18 changes: 9 additions & 9 deletions packages/api/mocks/SearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const SearchQueryFirst5Products = `query SearchQuery {
}`

export const productSearchCategory1Fetch = {
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/category-1/office/trade-policy/1?page=1&count=5&query=&sort=&locale=en-US&hideUnavailableItems=false',
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/category-1/office/trade-policy/1?page=1&count=5&query=&sort=&fuzzy=auto&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
Expand Down Expand Up @@ -1348,36 +1348,36 @@ export const productSearchCategory1Fetch = {
current: {
index: 1,
proxyUrl:
'search/category-1/office/trade-policy/1?page=1&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=1&count=5&query=&sort=&fuzzy=auto&operator=and',
},
before: [],
after: [
{
index: 2,
proxyUrl:
'search/category-1/office/trade-policy/1?page=2&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=2&count=5&query=&sort=&fuzzy=auto&operator=and',
},
{
index: 3,
proxyUrl:
'search/category-1/office/trade-policy/1?page=3&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=3&count=5&query=&sort=&fuzzy=auto&operator=and',
},
{
index: 4,
proxyUrl:
'search/category-1/office/trade-policy/1?page=4&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=4&count=5&query=&sort=&fuzzy=auto&operator=and',
},
{
index: 5,
proxyUrl:
'search/category-1/office/trade-policy/1?page=5&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=5&count=5&query=&sort=&fuzzy=auto&operator=and',
},
],
perPage: 5,
next: {
index: 2,
proxyUrl:
'search/category-1/office/trade-policy/1?page=2&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=2&count=5&query=&sort=&fuzzy=auto&operator=and',
},
previous: {
index: 0,
Expand All @@ -1388,14 +1388,14 @@ export const productSearchCategory1Fetch = {
last: {
index: 50,
proxyUrl:
'search/category-1/office/trade-policy/1?page=50&count=5&query=&sort=&operator=and&fuzzy=0',
'search/category-1/office/trade-policy/1?page=50&count=5&query=&sort=&fuzzy=auto&operator=and',
},
},
},
}

export const attributeSearchCategory1Fetch = {
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/facets/category-1/office/trade-policy/1?page=1&count=5&query=&sort=&locale=en-US&hideUnavailableItems=false',
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/facets/category-1/office/trade-policy/1?page=1&count=5&query=&sort=&fuzzy=auto&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
Expand Down
4 changes: 2 additions & 2 deletions packages/api/mocks/ValidateCartMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export const checkoutOrderFormCustomDataInvalidFetch = {
}

export const productSearchPage1Count1Fetch = {
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=1&query=sku%3A2737806&sort=&locale=en-US&hideUnavailableItems=false',
info: 'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=1&query=sku%3A2737806&sort=&fuzzy=auto&locale=en-US&hideUnavailableItems=false',
init: undefined,
options: { storeCookies: expect.any(Function) },
result: {
Expand Down Expand Up @@ -523,7 +523,7 @@ export const productSearchPage1Count1Fetch = {
current: {
index: 1,
proxyUrl:
'search/trade-policy/1?page=1&count=1&query=sku:2737806&sort=&locale=en-US&hide-unavailable-items=false&operator=and&fuzzy=0',
'search/trade-policy/1?page=1&count=1&query=sku:2737806&sort=&fuzzy=auto&operator=and',
},
before: [],
after: [],
Expand Down
2 changes: 0 additions & 2 deletions packages/api/src/__generated__/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 5 additions & 51 deletions packages/api/src/platforms/vtex/clients/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { fetchAPI } from '../fetch'
import type { IStoreSelectedFacet } from '../../../../__generated__/schema'
import type { Context, Options } from '../../'
import type {
SelectedFacet,
FuzzyFacet,
OperatorFacet,
} from '../../utils/facets'
import type { SelectedFacet } from '../../utils/facets'
import type {
Facet,
FacetValueBoolean,
Expand Down Expand Up @@ -43,38 +39,14 @@ export interface ProductLocator {
value: string
}

// channel keys
const POLICY_KEY = 'trade-policy'
const REGION_KEY = 'region-id'

// search parameters
const FUZZY_KEY = 'fuzzy'
const OPERATOR_KEY = 'operator'

const EXTRA_FACETS_KEYS = new Set([
POLICY_KEY,
REGION_KEY,
FUZZY_KEY,
OPERATOR_KEY,
])
const CHANNEL_KEYS = new Set([POLICY_KEY, REGION_KEY])

export const isFacetBoolean = (
facet: Facet
): facet is Facet<FacetValueBoolean> => facet.type === 'TEXT'

const isFuzzyFacet = (facet: SelectedFacet): facet is FuzzyFacet => {
return (
facet.key === 'fuzzy' &&
(facet.value === '0' || facet.value === '1' || facet.value === 'auto')
)
}

const isOperatorFacet = (facet: SelectedFacet): facet is OperatorFacet => {
return (
facet.key === 'operator' && (facet.value === 'and' || facet.value === 'or')
)
}

export const IntelligentSearch = (
{ account, environment, hideUnavailableItems }: Options,
ctx: Context
Expand Down Expand Up @@ -113,9 +85,7 @@ export const IntelligentSearch = (
}

const addDefaultFacets = (facets: SelectedFacet[]) => {
const withDefaultFacets = facets.filter(
({ key }) => !EXTRA_FACETS_KEYS.has(key)
)
const withDefaultFacets = facets.filter(({ key }) => !CHANNEL_KEYS.has(key))

const policyFacet =
facets.find(({ key }) => key === POLICY_KEY) ?? getPolicyFacet()
Expand All @@ -134,35 +104,21 @@ export const IntelligentSearch = (
return withDefaultFacets
}

const addSearchParamsFacets = (
facets: SelectedFacet[],
params: URLSearchParams
) => {
const fuzzyFacet = facets.find(({ key }) => key === FUZZY_KEY) ?? null
const operatorFacet = facets.find(({ key }) => key === OPERATOR_KEY) ?? null

if (fuzzyFacet && isFuzzyFacet(fuzzyFacet)) {
params.append(FUZZY_KEY, fuzzyFacet.value)
}

if (operatorFacet && isOperatorFacet(operatorFacet)) {
params.append(OPERATOR_KEY, operatorFacet.value)
}
}

const search = <T>({
query = '',
page,
count,
sort = '',
selectedFacets = [],
type,
fuzzy = 'auto',
}: SearchArgs): Promise<T> => {
const params = new URLSearchParams({
page: (page + 1).toString(),
count: count.toString(),
query,
sort,
fuzzy,
locale: ctx.storage.locale,
})

Expand All @@ -174,8 +130,6 @@ export const IntelligentSearch = (
.map(({ key, value }) => `${key}/${value}`)
.join('/')

addSearchParamsFacets(selectedFacets, params)

return fetchAPI(
`${base}/_v/api/intelligent-search/${type}/${pathname}?${params.toString()}`,
undefined,
Expand Down
Loading

0 comments on commit 1c76451

Please sign in to comment.