Skip to content

Commit 76ca962

Browse files
authored
Fix segment enconding/handling (#502)
* Fix segment encoding * Update changelog * Improve segment handling * Fix lint
1 parent 5cce9eb commit 76ca962

File tree

6 files changed

+63
-183
lines changed

6 files changed

+63
-183
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10-
## [1.85.0] - 2025-10-13
10+
### Fixed
11+
12+
- Segment enconding while making PDP requests.
13+
14+
## [1.85.0] - 2025-10-13 [YANKED]
1115

1216
### Changed
1317

node/clients/intelligent-search-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ExternalClient } from '@vtex/api'
33

44
import { parseState } from '../utils/searchState'
55
import type { FetchBannersArgs, IIntelligentSearchClient, FetchProductArgs, FetchProductResponse } from './intsch/types'
6+
import { Options, SearchResultArgs } from '../typings/Search'
67

78
const isPathTraversal = (str: string) => str.indexOf('..') >= 0
89

node/resolvers/search/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
fetchCorrection,
4747
} from '../../services/autocomplete'
4848
import { fetchBanners } from '../../services/banners'
49+
import { AdvertisementOptions, FacetsInput, ProductSearchInput, ProductsInput, SegmentData, SuggestionProductsArgs } from '../../typings/Search'
4950

5051
enum CrossSellingInput {
5152
view = 'view',

node/services/product.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetchProduct } from './product'
1+
import { fetchProduct, buildVtexSegment } from './product'
22
import { createContext } from '../mocks/contextFactory'
33

44
describe('fetchProduct service', () => {
@@ -12,6 +12,44 @@ describe('fetchProduct service', () => {
1212
jest.clearAllMocks()
1313
})
1414

15+
it('should build vtex segment correctly', () => {
16+
const vtexSegment = buildVtexSegment({
17+
vtexSegment: {
18+
campaigns: null,
19+
channel: '1',
20+
priceTables: null,
21+
regionId: 'U1cjMQ==',
22+
utm_campaign: null,
23+
utm_source: null,
24+
utmi_campaign: null,
25+
currencyCode: 'ARS',
26+
currencySymbol: '$',
27+
countryCode: 'ARG',
28+
cultureInfo: 'es-AR',
29+
admin_cultureInfo: 'es-AR',
30+
channelPrivacy: 'public',
31+
},
32+
salesChannel: '2',
33+
regionId: '3',
34+
})
35+
36+
const result = JSON.parse(Buffer.from(vtexSegment, 'base64').toString())
37+
38+
expect(result.regionId).toBe('3')
39+
expect(result.countryCode).toBe('ARG')
40+
expect(result.currencySymbol).toBe('$')
41+
expect(result.currencyCode).toBe('ARS')
42+
expect(result.channel).toBe('2')
43+
expect(result.cultureInfo).toBe('es-AR')
44+
expect(result.admin_cultureInfo).toBe('es-AR')
45+
expect(result.channelPrivacy).toBe('public')
46+
expect(result.campaigns).toBeNull()
47+
expect(result.priceTables).toBeNull()
48+
expect(result.utm_campaign).toBeNull()
49+
expect(result.utm_source).toBeNull()
50+
expect(result.utmi_campaign).toBeNull()
51+
})
52+
1553
it('should use intsch directly for b2bstoreqa account', async () => {
1654
const ctx = createContext({
1755
accountName: 'b2bstoreqa',

node/services/product.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { fetchAppSettings } from './settings'
2+
import type { SegmentData } from '../typings/Search'
23

34
export type ProductIdentifier = {
45
field: 'id' | 'slug' | 'ean' | 'reference' | 'sku'
@@ -127,18 +128,12 @@ export function buildVtexSegment({
127128
regionId?: string
128129
}): string {
129130
const cookie = {
131+
...vtexSegment,
130132
regionId: regionId ?? vtexSegment?.regionId,
131-
channel: salesChannel || vtexSegment?.channel,
132-
utm_campaign: vtexSegment?.utm_campaign || '',
133-
utm_source: vtexSegment?.utm_source || '',
134-
utmi_campaign: vtexSegment?.utmi_campaign || '',
135-
currencyCode: vtexSegment?.currencyCode || '',
136-
currencySymbol: vtexSegment?.currencySymbol || '',
137-
countryCode: vtexSegment?.countryCode || '',
138-
cultureInfo: vtexSegment?.cultureInfo || '',
133+
channel: salesChannel ?? vtexSegment?.channel,
139134
}
140135

141-
return Buffer.from(JSON.stringify(cookie), 'base64').toString()
136+
return Buffer.from(JSON.stringify(cookie)).toString('base64')
142137
}
143138

144139
export async function fetchProduct(

node/typings/Search.ts

Lines changed: 13 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
interface SegmentData {
1+
export type SegmentData = {
22
campaigns?: any
33
channel: string
44
priceTables?: any
5-
utm_campaign: string
5+
utm_campaign: string | null
66
regionId?: string
7-
utm_source: string
8-
utmi_campaign: string
7+
utm_source: string | null
8+
utmi_campaign: string | null
99
currencyCode: string
1010
currencySymbol: string
1111
countryCode: string
1212
cultureInfo: string
1313
[key: string]: any
1414
}
1515

16-
interface ElasticImage {
17-
name: string
18-
value: string
19-
}
20-
21-
enum IndexingType {
22-
API = 'API',
23-
XML = 'XML',
24-
}
16+
export type IndexingType = 'API' | 'XML'
2517

26-
interface SearchResultArgs extends AdvertisementOptions {
18+
export interface SearchResultArgs extends AdvertisementOptions {
2719
attributePath?: string
2820
query?: string
2921
page?: number
@@ -48,17 +40,12 @@ interface SearchResultArgs extends AdvertisementOptions {
4840
showSponsored?: boolean
4941
}
5042

51-
interface BannersArgs {
52-
fullText: string
53-
attributePath: string
54-
}
55-
5643
interface RegionSeller {
5744
id: string
5845
name: string
5946
}
6047

61-
interface SuggestionProductsArgs {
48+
export interface SuggestionProductsArgs {
6249
fullText: string
6350
facetKey?: string
6451
facetValue?: string
@@ -77,27 +64,23 @@ interface SuggestionProductsArgs {
7764
advertisementOptions: AdvertisementOptions
7865
}
7966

80-
interface SuggestionSearchesArgs {
81-
term: string
82-
}
83-
8467
interface SelectedFacet {
8568
value: string
8669
key: string
8770
}
8871

89-
interface Options {
72+
export interface Options {
9073
allowRedirect?: boolean
9174
}
9275

93-
interface AdvertisementOptions {
76+
export interface AdvertisementOptions {
9477
showSponsored?: boolean
9578
sponsoredCount?: number
9679
repeatSponsoredProducts?: boolean
9780
advertisementPlacement?: string
9881
}
9982

100-
interface FacetsInput {
83+
export interface FacetsInput {
10184
map: string
10285
selectedFacets: SelectedFacet[]
10386
fullText: string
@@ -109,11 +92,11 @@ interface FacetsInput {
10992
categoryTreeBehavior: 'default' | 'show' | 'hide'
11093
}
11194

112-
interface ProductsInput extends SearchArgs {
95+
export interface ProductsInput extends SearchArgs {
11396
advertisementOptions?: AdvertisementOptions
11497
}
11598

116-
interface ProductSearchInput {
99+
export interface ProductSearchInput {
117100
query: string
118101
from: number
119102
to: number
@@ -130,146 +113,4 @@ interface ProductSearchInput {
130113
options?: Options
131114
showSponsored?: boolean
132115
advertisementOptions?: AdvertisementOptions
133-
}
134-
135-
interface ElasticAttribute {
136-
visible: boolean
137-
active: boolean
138-
key: string
139-
label: string
140-
type: string
141-
values: ElasticAttributeValue[]
142-
originalKey: string
143-
originalLabel: string
144-
minValue?: number
145-
maxValue?: number
146-
}
147-
148-
interface ElasticAttributeValue {
149-
count: number
150-
active: boolean
151-
key: string
152-
label: string
153-
id: string
154-
originalKey?: string
155-
originalLabel?: string
156-
}
157-
158-
interface Breadcrumb {
159-
href: string
160-
name: string
161-
}
162-
163-
interface BiggySearchProduct {
164-
name: string
165-
id: string
166-
timestamp: number
167-
product: string
168-
description: string
169-
reference: string
170-
url: string
171-
link: string
172-
oldPrice: number
173-
price: number
174-
stock: number
175-
brand: string
176-
brandId: string
177-
installment?: BiggyInstallment
178-
measurementUnit: string
179-
unitMultiplier: number
180-
tax: number
181-
images: BiggyProductImage[]
182-
skus: BiggySearchSKU[]
183-
categories: string[]
184-
categoryIds: string[]
185-
extraData: BiggyProductExtraData[]
186-
productSpecifications: string[]
187-
specificationGroups: string
188-
textAttributes: BiggyTextAttribute[]
189-
numberAttributes: BiggyTextAttribute[]
190-
split: BiggySplit
191-
categoryTrees: BiggyCategoryTree[]
192-
clusterHighlights: Record<string, string>
193-
}
194-
195-
interface BiggySplit {
196-
labelKey: string
197-
labelValue: string
198-
}
199-
200-
interface BiggyProductImage {
201-
name: string
202-
value: string
203-
}
204-
205-
interface BiggyProductExtraData {
206-
key: string
207-
value: string
208-
}
209-
210-
interface BiggySearchSKU {
211-
name: string
212-
nameComplete: string
213-
complementName?: string
214-
id: string
215-
ean?: string
216-
reference: string
217-
image: string
218-
images: BiggyProductImage[]
219-
videos?: string[]
220-
stock: number
221-
oldPrice: number
222-
price: number
223-
measurementUnit: string
224-
unitMultiplier: number
225-
link: string
226-
attributes: BiggySKUAttribute[]
227-
sellers: BiggySeller[]
228-
policies: BiggyPolicy[]
229-
}
230-
231-
interface BiggySKUAttribute {
232-
key: string
233-
value: string
234-
}
235-
236-
interface BiggySeller {
237-
id: string
238-
name: string
239-
oldPrice: number
240-
price: number
241-
stock: number
242-
tax: number
243-
default: boolean
244-
teasers?: object[]
245-
installment?: BiggyInstallment
246-
}
247-
248-
interface BiggyInstallment {
249-
value: number
250-
count: number
251-
interest: boolean
252-
}
253-
254-
interface BiggyPolicy {
255-
id: string
256-
sellers: BiggySeller[]
257-
}
258-
259-
interface BiggyTextAttribute {
260-
labelKey: string
261-
labelValue: string
262-
key: string
263-
value: string
264-
isFilter: boolean
265-
id: string
266-
valueId: string
267-
isSku: boolean
268-
joinedKey: string
269-
joinedValue: string
270-
}
271-
272-
interface BiggyCategoryTree {
273-
categoryNames: string[]
274-
categoryIds: string[]
275-
}
116+
}

0 commit comments

Comments
 (0)