Skip to content

Commit a28d534

Browse files
authored
feat: Update Session with location info (#2749)
## What's the purpose of this pull request? Update the Session with postal code, country, and geo-coordinates. The Intelligent Search will use this for the Delivery Promise feature. FastStore updates the session with the location information and IS access it. ## How it works? The update is a PATCH request to the Session Manager API. The fields that IS will use from the session are `postalCode`, `geoCoordinates` and `country`, inside the `public` namespace. ## How to test it? Run this code (or use the preview link below) and test changing the postal code and/or using a default one. The fields inside public namespace of the session should be updated accordingly. To check it you can run this request: ```curl curl --location 'https://storeframework.vtexcommercestable.com.br/api/sessions?items=public.postalCode,public.geoCoordinates,public.country' \ --header 'Cookie: vtex_session=ADD_HERE' ``` ⚠️ Substitute the "ADD_HERE" with the vtex_session cookie that you can get in the browser #### Preview with Delivery Promise NOT enabled https://storeframework-cm652ufll028lmgv665a6xv0g-lqicw1ez2.b.vtex.app/ The session shouldn't update the public field. ![Screenshot 2025-03-28 at 11 47 29](https://github.com/user-attachments/assets/5796d143-becf-4141-8751-e7bbab93821e) ![Screenshot 2025-04-07 at 08 43 45](https://github.com/user-attachments/assets/6ab9e4ed-268a-437c-9cdb-2bc29628defc) #### Preview with Delivery Promise enabled https://storeframework-cm652ufll028lmgv665a6xv0g-58v0e6a7h.b.vtex.app/ ##### Without postal code set The session shouldn't update the public field. ![Screenshot 2025-03-28 at 12 08 36](https://github.com/user-attachments/assets/11ed56f9-1836-4da1-95f4-1b1b5bfdafc2) ![Screenshot 2025-04-07 at 08 45 10](https://github.com/user-attachments/assets/3c44b25c-73ac-4554-a7c1-a7455fe61492) ##### With postal code (and country and geo coordinates) The session should update the public field! ![Screenshot 2025-03-28 at 12 09 28](https://github.com/user-attachments/assets/7e85d0cc-c676-403b-89db-6e5bffcdbada) ![Screenshot 2025-04-07 at 08 45 57](https://github.com/user-attachments/assets/29ec6991-3733-439e-a783-8c8be9c1ee0e) ### Starters Deploy Preview https://github.com/vtex-sites/starter.store/pull/737 ## References - [Jira task](https://vtex-dev.atlassian.net/browse/SFS-2362) - [Slack thread](https://vtex.slack.com/archives/C069YM7R73P/p1741962308203469?thread_ts=1741782251.679469&cid=C069YM7R73P) - [Session Manager API - Edit session](https://developers.vtex.com/docs/api-reference/session-manager-api#patch-/api/sessions)
1 parent 9742ded commit a28d534

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

packages/api/src/platforms/vtex/clients/commerce/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,19 @@ export const VtexCommerce = (
357357

358358
params.set(
359359
'items',
360-
'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol,authentication.customerId,'
360+
'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol,authentication.customerId,checkout.regionId,'
361361
)
362362

363363
const headers: HeadersInit = withCookie({
364364
'content-type': 'application/json',
365365
})
366366

367+
const sessionCookie = parse(ctx?.headers?.cookie ?? '')?.vtex_session
368+
367369
return fetchAPI(
368370
`${base}/api/sessions?${params.toString()}`,
369371
{
370-
method: 'POST',
372+
method: sessionCookie ? 'PATCH' : 'POST',
371373
headers,
372374
body: '{}',
373375
},

packages/api/src/platforms/vtex/clients/commerce/types/Session.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface Profile {
3636

3737
export interface Checkout {
3838
orderFormId?: Value
39+
regionId?: Value
3940
}
4041

4142
export interface Public {

packages/api/src/platforms/vtex/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface Options {
4444

4545
interface FeatureFlags {
4646
enableOrderFormSync?: boolean
47+
enableDeliveryPromise?: boolean
4748
}
4849

4950
export interface Context {

packages/api/src/platforms/vtex/resolvers/validateSession.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import deepEquals from 'fast-deep-equal'
22

3-
import ChannelMarshal from '../utils/channel'
43
import type { Context } from '..'
54
import type {
65
MutationValidateSessionArgs,
76
StoreMarketingData,
87
StoreSession,
98
} from '../../../__generated__/schema'
9+
import ChannelMarshal from '../utils/channel'
1010

1111
async function getGeoCoordinates(
1212
clients: Context['clients'],
@@ -33,7 +33,12 @@ async function getGeoCoordinates(
3333
export const validateSession = async (
3434
_: any,
3535
{ session: oldSession, search }: MutationValidateSessionArgs,
36-
{ clients }: Context
36+
{
37+
clients,
38+
storage: {
39+
flags: { enableDeliveryPromise },
40+
},
41+
}: Context
3742
): Promise<StoreSession | null> => {
3843
const channel = ChannelMarshal.parse(oldSession.channel ?? '')
3944
const postalCode = String(oldSession.postalCode ?? '')
@@ -62,6 +67,20 @@ export const validateSession = async (
6267
utmiPart: params.get('utmi_pc') ?? oldMarketingData?.utmiPart ?? '',
6368
}
6469

70+
/**
71+
* The Session Manager API (https://developers.vtex.com/docs/api-reference/session-manager-api#patch-/api/sessions) adds the query params to the session public namespace.
72+
* But the session should be updated with the location data only if the Delivery Promise feature flag is enabled and if all required data is available,
73+
* otherwise there will be unnecessary requests and operations from FastStore and Intelligent Search.
74+
*/
75+
if (enableDeliveryPromise && !!postalCode && !!country && !!geoCoordinates) {
76+
params.set('postalCode', postalCode)
77+
params.set('country', country)
78+
params.set(
79+
'geoCoordinates',
80+
`${geoCoordinates.latitude},${geoCoordinates.longitude}`
81+
)
82+
}
83+
6584
const [regionData, sessionData] = await Promise.all([
6685
postalCode || geoCoordinates
6786
? clients.commerce.checkout.region({
@@ -77,8 +96,9 @@ export const validateSession = async (
7796
const profile = sessionData?.namespaces.profile ?? null
7897
const store = sessionData?.namespaces.store ?? null
7998
const authentication = sessionData?.namespaces.authentication ?? null
80-
const region = regionData?.[0]
99+
const checkout = sessionData?.namespaces.checkout ?? null
81100
// Set seller only if it's inside a region
101+
const region = regionData?.[0]
82102
const seller = region?.sellers.find((seller) => channel.seller === seller.id)
83103

84104
const newSession = {
@@ -90,7 +110,7 @@ export const validateSession = async (
90110
country: store?.countryCode?.value ?? oldSession.country,
91111
channel: ChannelMarshal.stringify({
92112
salesChannel: store?.channel?.value ?? channel.salesChannel,
93-
regionId: region?.id ?? channel.regionId,
113+
regionId: checkout?.regionId?.value ?? channel.regionId,
94114
seller: seller?.id,
95115
hasOnlyDefaultSalesChannel: !store?.channel?.value,
96116
}),

packages/core/src/server/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ export const apiOptions: APIOptions = {
1616
locale: storeConfig.session.locale,
1717
flags: {
1818
enableOrderFormSync: true,
19+
enableDeliveryPromise: storeConfig.deliveryPromise.enabled,
1920
},
2021
}

0 commit comments

Comments
 (0)