Skip to content

Commit

Permalink
change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
evertonvargasvtex committed Oct 29, 2024
1 parent bdd6266 commit 9b09e59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
24 changes: 19 additions & 5 deletions packages/api/src/platforms/vtex/clients/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export const IntelligentSearch = (
return withDefaultFacets
}

const getVtexSegment = (cookies?: string) => {
if (typeof cookies !== 'string') {
return null;
}

const match = cookies.match(/fs_segment=([^;]*)/);
return match ? match[1] : null;
};

const search = <T>({
query = '',
page,
Expand Down Expand Up @@ -148,18 +157,23 @@ export const IntelligentSearch = (
.join('/')

console.log("search IS", segment)
const headers = segment
? {

const segmentCookie = ctx ? getVtexSegment(ctx.headers.cookie) : null;

console.log("search segmentCookie", segmentCookie)

const headers = segmentCookie || segment
? {
headers: {
'Cookie': `vtex_segment=${segment}`,
'Cookie': `vtex_segment=${segmentCookie || segment}`,
},
}
: undefined
: undefined;

return fetchAPI(
`${base}/_v/api/intelligent-search/${type}/${pathname}?${params.toString()}`,
headers,
{ storeCookies}
{ storeCookies }
)
}

Expand Down
7 changes: 2 additions & 5 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { SearchArgs } from '../clients/search'
export const Query = {
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
// Insert channel in context for later usage
console.log("ctx product query", ctx)
const channel = findChannel(locator)
const locale = findLocale(locator)
const id = findSkuId(locator)
Expand Down Expand Up @@ -124,9 +125,6 @@ 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)
Expand Down Expand Up @@ -167,8 +165,7 @@ export const Query = {
count: first,
query: query ?? undefined,
sort: SORT_MAP[sort ?? 'score_desc'],
selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? [],
segment
selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? []
}

const productSearchPromise = ctx.clients.search.products(searchArgs)
Expand Down

0 comments on commit 9b09e59

Please sign in to comment.