diff --git a/packages/api/src/platforms/vtex/clients/search/index.ts b/packages/api/src/platforms/vtex/clients/search/index.ts index 7d7e6627e0..e79ad28d5f 100644 --- a/packages/api/src/platforms/vtex/clients/search/index.ts +++ b/packages/api/src/platforms/vtex/clients/search/index.ts @@ -34,6 +34,7 @@ export interface SearchArgs { hideUnavailableItems?: boolean showInvisibleItems?: boolean showSponsored?: boolean + segment?: string } export interface ProductLocator { @@ -56,19 +57,6 @@ export const IntelligentSearch = ( const base = `https://${account}.${environment}.com.br/api/io` const storeCookies = getStoreCookie(ctx) - const getVtexSegment = (cookies?: string) => { - if (typeof cookies !== 'string') { - return null; - } - - const match = cookies.match(/vtex_segment=([^;]*)/); - - console.log("match", match) - return match ? match[1] : null; - }; - - const segmentCookie = ctx ? getVtexSegment(ctx.headers.cookie) : null - const getPolicyFacet = (): IStoreSelectedFacet | null => { const { salesChannel } = ctx.storage.channel @@ -128,6 +116,7 @@ export const IntelligentSearch = ( type, fuzzy = 'auto', showInvisibleItems, + segment = undefined, }: SearchArgs): Promise => { const params = new URLSearchParams({ page: (page + 1).toString(), @@ -158,12 +147,10 @@ export const IntelligentSearch = ( .map(({ key, value }) => `${key}/${value}`) .join('/') - console.log("search", segmentCookie) - - const headers = segmentCookie + const headers = segment ? { headers: { - 'Cookie': `vtex_segment=${segmentCookie}`, + 'Cookie': `vtex_segment=${segment}`, }, } : undefined diff --git a/packages/api/src/platforms/vtex/resolvers/query.ts b/packages/api/src/platforms/vtex/resolvers/query.ts index 6514bc904d..d000b61fca 100644 --- a/packages/api/src/platforms/vtex/resolvers/query.ts +++ b/packages/api/src/platforms/vtex/resolvers/query.ts @@ -9,6 +9,7 @@ import { findSkuId, findSlug, transformSelectedFacet, + findSegment } from '../utils/facets' import { SORT_MAP } from '../utils/sort' import { StoreCollection } from './collection' @@ -34,6 +35,7 @@ export const Query = { const locale = findLocale(locator) const id = findSkuId(locator) const slug = findSlug(locator) + const segment = findSegment(locator) if (channel) { mutateChannelContext(ctx, channel) @@ -94,6 +96,7 @@ export const Query = { page: 0, count: 1, query: `product:${route.id}`, + segment }) if (!product) { @@ -121,6 +124,9 @@ export const Query = { const channel = findChannel(selectedFacets) const locale = findLocale(selectedFacets) const crossSelling = findCrossSelling(selectedFacets) + const segment = findSegment(selectedFacets) + + console.log("search segment", segment) if (channel) { mutateChannelContext(ctx, channel) diff --git a/packages/api/src/platforms/vtex/utils/facets.ts b/packages/api/src/platforms/vtex/utils/facets.ts index 2291f5c2f2..2b9df39b24 100644 --- a/packages/api/src/platforms/vtex/utils/facets.ts +++ b/packages/api/src/platforms/vtex/utils/facets.ts @@ -88,3 +88,6 @@ export const findLocale = (facets?: Maybe) => export const findChannel = (facets?: Maybe) => facets?.find((facet) => facet.key === 'channel')?.value ?? null + +export const findSegment = (facets?: Maybe) => + facets?.find((facet) => facet.key === 'segment')?.value ?? undefined