1
1
import deepEquals from 'fast-deep-equal'
2
2
3
- import ChannelMarshal from '../utils/channel'
4
3
import type { Context } from '..'
5
4
import type {
6
5
MutationValidateSessionArgs ,
7
6
StoreMarketingData ,
8
7
StoreSession ,
9
8
} from '../../../__generated__/schema'
9
+ import ChannelMarshal from '../utils/channel'
10
10
11
11
async function getGeoCoordinates (
12
12
clients : Context [ 'clients' ] ,
@@ -33,7 +33,12 @@ async function getGeoCoordinates(
33
33
export const validateSession = async (
34
34
_ : any ,
35
35
{ session : oldSession , search } : MutationValidateSessionArgs ,
36
- { clients } : Context
36
+ {
37
+ clients,
38
+ storage : {
39
+ flags : { enableDeliveryPromise } ,
40
+ } ,
41
+ } : Context
37
42
) : Promise < StoreSession | null > => {
38
43
const channel = ChannelMarshal . parse ( oldSession . channel ?? '' )
39
44
const postalCode = String ( oldSession . postalCode ?? '' )
@@ -62,6 +67,20 @@ export const validateSession = async (
62
67
utmiPart : params . get ( 'utmi_pc' ) ?? oldMarketingData ?. utmiPart ?? '' ,
63
68
}
64
69
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
+
65
84
const [ regionData , sessionData ] = await Promise . all ( [
66
85
postalCode || geoCoordinates
67
86
? clients . commerce . checkout . region ( {
@@ -77,8 +96,9 @@ export const validateSession = async (
77
96
const profile = sessionData ?. namespaces . profile ?? null
78
97
const store = sessionData ?. namespaces . store ?? null
79
98
const authentication = sessionData ?. namespaces . authentication ?? null
80
- const region = regionData ?. [ 0 ]
99
+ const checkout = sessionData ?. namespaces . checkout ?? null
81
100
// Set seller only if it's inside a region
101
+ const region = regionData ?. [ 0 ]
82
102
const seller = region ?. sellers . find ( ( seller ) => channel . seller === seller . id )
83
103
84
104
const newSession = {
@@ -90,7 +110,7 @@ export const validateSession = async (
90
110
country : store ?. countryCode ?. value ?? oldSession . country ,
91
111
channel : ChannelMarshal . stringify ( {
92
112
salesChannel : store ?. channel ?. value ?? channel . salesChannel ,
93
- regionId : region ?. id ?? channel . regionId ,
113
+ regionId : checkout ?. regionId ?. value ?? channel . regionId ,
94
114
seller : seller ?. id ,
95
115
hasOnlyDefaultSalesChannel : ! store ?. channel ?. value ,
96
116
} ) ,
0 commit comments