Skip to content

Commit

Permalink
Call IS to get autocomplete suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
thalytafabrine committed Jan 9, 2024
1 parent 28e837a commit 5a2d684
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@types/node": "^12.0.0",
"@types/qs": "^6.5.1",
"@types/ramda": "^0.26.21",
"@vtex/api": "6.45.22",
"@vtex/api": "6.46.0",
"@vtex/tsconfig": "^0.6.0",
"eslint": "^5.15.3",
"eslint-config-vtex": "^10.1.0",
Expand Down
22 changes: 19 additions & 3 deletions node/resolvers/search/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ const extractSlug = (item: SearchAutocompleteUnit) => {
return item.criteria ? `${href[3]}/${href[4]}` : href[3]
}

const extractThumb = (item: SearchItem) => {
const imageId = `${item.images[0].imageId}-25-25`
const imagePaths = item.images[0].imageUrl.split(`/${item.images[0].imageId}/`)
return `<img src="${imagePaths[0]}/${imageId}/${imagePaths[1]}" width="25" height="25" alt="${imagePaths[1].split('.')[0]}" id="" />`
}

export const resolvers = {
Items: {
slug: (root: SearchAutocompleteUnit) => extractSlug(root),
slug: (root: any) => root.linkText ?? extractSlug(root),

productId: ({ items, productId }: any) => {
if (productId) {
return productId
}
return items ? path([0, 'productId'], items) : null
},

name: (root: any) => root.name ?? "",

href: (root: any) => root.href ?? `https://portal.vtexcommercestable.com.br/${root.linkText}/p`,

productId: ({ items }: SearchAutocompleteUnit) =>
items ? path([0, 'productId'], items) : null,
thumb: (root: any) => root.items.length ? extractThumb(root.items[0] as SearchItem) : root.thumb,
},
}
53 changes: 47 additions & 6 deletions node/resolvers/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,24 +315,65 @@ export const queries = {
ctx: Context
) => {
const {
clients: { search },
clients: { intelligentSearchApi },
} = ctx

if (!args.searchTerm) {
throw new UserInputError('No search term provided')
}

// do we still need to translate it? not sure.
const translatedTerm = await translateToStoreDefaultLanguage(
ctx,
args.searchTerm
)
const { itemsReturned } = await search.autocomplete({
maxRows: args.maxRows,
searchTerm: translatedTerm,
})

const workspaceSearchParams = await getWorkspaceSearchParamsFromStorage(ctx)

const biggyArgs : {[key: string] : any} = {
query: translatedTerm,
from: 0,
to: args.maxRows ? args.maxRows - 1 : 4,
sort: '',
allowRedirect: false, // When there is a redirect, no product is returned.
...workspaceSearchParams,
}

const isItems: (SearchAutocompleteUnit | { items: any[], linkText: string, productId: string, name: string })[] = []

const [isResult, searchSuggestions] = await Promise.all([
intelligentSearchApi.productSearch(biggyArgs, ''),
intelligentSearchApi.autocompleteSearchSuggestions({query: translatedTerm})
])

const criterias = searchSuggestions.searches.filter((searchItem: any) => searchItem.term === translatedTerm)
if (criterias.length) {
criterias[0].attributes.map((att: any) => {
isItems.push({
items: [],
thumb: "",
thumbUrl: null,
name: `${translatedTerm} ${att.labelValue}`,
href: `https://portal.vtexcommercestable.com.br/${att.value}/${translatedTerm}`,
criteria: ${translatedTerm} ${att.labelValue}¢/${att.value}/${translatedTerm}`, // validate with catalog
})
})
}

if (isResult.products.length) {
isResult.products.map((product: any) => {
isItems.push({
name: product.productName,
items: product.items,
productId: product.productId,
linkText: product.linkText,
})
})
}

return {
cacheId: args.searchTerm,
itemsReturned,
itemsReturned: isItems,
}
},
facets: async (_: any, args: FacetsInput, ctx: any) => {
Expand Down
8 changes: 4 additions & 4 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,10 @@
lodash.unescape "4.0.1"
semver "5.5.0"

"@vtex/api@6.45.22":
version "6.45.22"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.22.tgz#fa9bbfde1a4d4fbbaf6cce9f6dbc9bb9ee929ba3"
integrity sha512-g5cGUDhF4FADgSMpQmce/bnIZumwGlPG2cabwbQKIQ+cCFMZqOEM/n+YQb1+S8bCyHkzW3u/ZABoyCKi5/nxxg==
"@vtex/api@6.46.0":
version "6.46.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.46.0.tgz#208d14b96cbc8fd5eb6bd18fbd0c8424886e6154"
integrity sha512-XAvJlD1FG1GynhPXiMcayunahFCL2r3ilO5MHAWKxYvB/ljyxi4+U+rVpweeaQGpxHfhKHdfPe7qNEEh2oa2lw==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down

0 comments on commit 5a2d684

Please sign in to comment.