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

fix: Top searches #1321

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/api/mocks/AllProductsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ 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=&fuzzy=0&hideUnavailableItems=false',
'https://storeframework.vtexcommercestable.com.br/api/io/_v/api/intelligent-search/product_search/trade-policy/1?page=1&count=5&query=&sort=&fuzzy=0&locale=en-US&hideUnavailableItems=false',
init: undefined,
result: {
products: [
Expand Down
2 changes: 1 addition & 1 deletion packages/api/mocks/ProductQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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=&fuzzy=0&hideUnavailableItems=false',
'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=0&locale=en-US&hideUnavailableItems=false',
init: undefined,
result: {
products: [
Expand Down
6 changes: 3 additions & 3 deletions packages/api/mocks/SearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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=&fuzzy=0&hideUnavailableItems=false',
'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=0&locale=en-US&hideUnavailableItems=false',
init: undefined,
result: {
products: [
Expand Down Expand Up @@ -1383,7 +1383,7 @@ export const productSearchCategory1Fetch = {

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=&fuzzy=0&hideUnavailableItems=false',
'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=0&locale=en-US&hideUnavailableItems=false',
init: undefined,
result: {
facets: [
Expand Down Expand Up @@ -1412,7 +1412,7 @@ export const attributeSearchCategory1Fetch = {
},
],
type: 'PRICERANGE',
name: 'Pre�o',
name: 'Pre�o',
hidden: false,
key: 'price',
quantity: 3,
Expand Down
2 changes: 1 addition & 1 deletion packages/api/mocks/ValidateCartMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,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=&fuzzy=0&hideUnavailableItems=false',
'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=0&locale=en-US&hideUnavailableItems=false',
init: undefined,
result: {
products: [
Expand Down
36 changes: 28 additions & 8 deletions packages/api/src/platforms/vtex/clients/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const IntelligentSearch = (
query,
sort,
fuzzy,
locale: ctx.storage.locale,
})

if (hideUnavailableItems !== undefined) {
Expand All @@ -125,20 +126,39 @@ export const IntelligentSearch = (

const suggestedProducts = (
args: Omit<SearchArgs, 'type'>
): Promise<ProductSearchResult> =>
fetchAPI(
`${base}/_v/api/intelligent-search/product_search?query=${args.query}`
): Promise<ProductSearchResult> => {
const params = new URLSearchParams({
query: args.query?.toString() ?? '',
locale: ctx.storage.locale,
})

return fetchAPI(
`${base}/_v/api/intelligent-search/product_search?${params.toString()}`
)
}

const suggestedTerms = (
args: Omit<SearchArgs, 'type'>
): Promise<Suggestion> =>
fetchAPI(
`${base}/_v/api/intelligent-search/search_suggestions?query=${args.query}`
): Promise<Suggestion> => {
const params = new URLSearchParams({
query: args.query?.toString() ?? '',
locale: ctx.storage.locale,
})

return fetchAPI(
`${base}/_v/api/intelligent-search/search_suggestions?${params.toString()}`
)
}

const topSearches = (): Promise<Suggestion> => {
const params = new URLSearchParams({
locale: ctx.storage.locale,
})

const topSearches = (): Promise<Suggestion> =>
fetchAPI(`${base}/_v/api/intelligent-search/top_searches`)
return fetchAPI(
`${base}/_v/api/intelligent-search/top_searches?${params.toString()}`
)
}

const facets = (args: Omit<SearchArgs, 'type'>) =>
search<FacetSearchResult>({ ...args, type: 'facets' })
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/platforms/vtex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Options {
environment: 'vtexcommercestable' | 'vtexcommercebeta'
// Default sales channel to use for fetching products
channel: string
locale: string
hideUnavailableItems: boolean
flags?: FeatureFlags
}
Expand All @@ -45,6 +46,7 @@ export interface Context {
* */
storage: {
channel: Required<Channel>
locale: string
flags: FeatureFlags
}
headers: Record<string, string>
Expand Down Expand Up @@ -79,6 +81,7 @@ export const getContextFactory = (options: Options) => (ctx: any): Context => {
ctx.storage = {
channel: ChannelMarshal.parse(options.channel),
flags: options.flags ?? {},
locale: options.locale,
}
ctx.clients = getClients(options, ctx)
ctx.loaders = getLoaders(options, ctx)
Expand Down
33 changes: 22 additions & 11 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { mutateChannelContext, mutateLocaleContext } from '../utils/contex'
import { enhanceSku } from '../utils/enhanceSku'
import { transformSelectedFacet } from '../utils/facets'
import {
findChannel,
findLocale,
transformSelectedFacet,
} from '../utils/facets'
import { SORT_MAP } from '../utils/sort'
import { StoreCollection } from './collection'
import type {
Expand All @@ -11,16 +16,19 @@ import type {
} from '../../../__generated__/schema'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { Context } from '../index'
import { mutateChannelContext } from '../utils/channel'

export const Query = {
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
// Insert channel in context for later usage
const channelString = locator.find((facet) => facet.key === 'channel')
?.value
const channel = findChannel(locator)
const locale = findLocale(locator)

if (channelString) {
mutateChannelContext(ctx, channelString)
if (channel) {
mutateChannelContext(ctx, channel)
}

if (locale) {
mutateLocaleContext(ctx, locale)
}

const {
Expand All @@ -42,12 +50,15 @@ export const Query = {
ctx: Context
) => {
// Insert channel in context for later usage
const channelString = selectedFacets?.find(
(facet) => facet.key === 'channel'
)?.value
const channel = findChannel(selectedFacets)
const locale = findLocale(selectedFacets)

if (channel) {
mutateChannelContext(ctx, channel)
}

if (channelString) {
mutateChannelContext(ctx, channelString)
if (locale) {
mutateLocaleContext(ctx, locale)
}

const after = maybeAfter ? Number(maybeAfter) : 0
Expand Down
6 changes: 0 additions & 6 deletions packages/api/src/platforms/vtex/utils/channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Context } from '..'

export interface Channel {
regionId?: string
salesChannel?: string
Expand All @@ -25,7 +23,3 @@ export default class ChannelMarshal {
return JSON.stringify(channel)
}
}

export const mutateChannelContext = (ctx: Context, channelString: string) => {
ctx.storage.channel = ChannelMarshal.parse(channelString)
}
10 changes: 10 additions & 0 deletions packages/api/src/platforms/vtex/utils/contex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ChannelMarshal from './channel'
import type { Context } from '..'

export const mutateChannelContext = (ctx: Context, channelString: string) => {
ctx.storage.channel = ChannelMarshal.parse(channelString)
}

export const mutateLocaleContext = (ctx: Context, locale: string) => {
ctx.storage.locale = locale
}
11 changes: 11 additions & 0 deletions packages/api/src/platforms/vtex/utils/facets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ChannelMarshal from './channel'
import type { Maybe } from '../../../__generated__/schema'

export interface SelectedFacet {
key: string
Expand All @@ -24,7 +25,17 @@ export const transformSelectedFacet = ({ key, value }: SelectedFacet) => {
return channelFacets
}

case 'locale': {
return [] // remove this facet from search
}

default:
return { key, value }
}
}

export const findLocale = (facets?: Maybe<SelectedFacet[]>) =>
facets?.find((x) => x.key === 'locale')?.value ?? null

export const findChannel = (facets?: Maybe<SelectedFacet[]>) =>
facets?.find((facet) => facet.key === 'channel')?.value ?? null
1 change: 1 addition & 0 deletions packages/api/test/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const apiOptions = {
account: 'storeframework',
environment: 'vtexcommercestable',
channel: '{"salesChannel":"1"}',
locale: 'en-US',
hideUnavailableItems: false,
flags: {
enableOrderFormSync: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/api/test/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ const apiOptions = {
account: 'storeframework',
environment: 'vtexcommercestable',
channel: '{"salesChannel":"1"}',
locale: 'en-US',
hideUnavailableItems: false,
flags: {
enableOrderFormSync: true,
},
} as Options

const mockedFetch = jest.fn()
Expand Down
4 changes: 4 additions & 0 deletions packages/api/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ beforeAll(async () => {
account: 'storeframework',
environment: 'vtexcommercestable',
channel: '{"salesChannel":"1"}',
locale: 'en-US',
hideUnavailableItems: false,
flags: {
enableOrderFormSync: true,
},
})
})

Expand Down