From 3548cee25cfe246d819530233c6aecf4ddcd3658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Feij=C3=B3?= Date: Wed, 26 Mar 2025 12:17:29 -0300 Subject: [PATCH 1/4] chore: Ensure session with location data (#2745) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR intends to ensure all location data (`postalCode`, `geoCoordinates` and `country`) is being synchronized when needed. It updates geo coordinates when users set a postal code and ensure location data won't be overwritten if users grant geo location consent late. - Enable Delivery Promise on discovery.config; - Open the localhost store on anon tab (to ensure no stale data); - First, consent geo location data from popup and look to the IDB `fs::session` value, it should fill `geoCoordinates` data after `useGeolocation` hook execution; - Then try to set a postal code, the `postalCode` should be set and `geoCoordinates` should be different now (based on the postal code); - Another test: open another anon tab and, while the popup asking for geo location consent set a postal code. Then, after the session is updated with the new location (based on the postal code), grant the geo location consent and nothing should happen since user have already set the postal code. vtex-sites/starter.store#734 --------- Co-authored-by: Fanny Chien Co-authored-by: LarĂ­cia Mota --- .../vtex/resolvers/validateSession.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/api/src/platforms/vtex/resolvers/validateSession.ts b/packages/api/src/platforms/vtex/resolvers/validateSession.ts index 661df7e385..aa727a696c 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateSession.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateSession.ts @@ -30,6 +30,28 @@ async function getPreciseLocationData( } } +async function getGeoCoordinates( + clients: Context['clients'], + country: string, + postalCode: string +) { + try { + const address = await clients.commerce.checkout.address({ + postalCode, + country, + }) + + const [longitude, latitude] = address.geoCoordinates + return { latitude, longitude } + } catch (err) { + console.error( + `Error while getting geo coordinates for the current postal code (${postalCode}) and country (${country}).\n` + ) + + throw err + } +} + export const validateSession = async ( _: any, { session: oldSession, search }: MutationValidateSessionArgs, From fbcf557c402be22cbc1b5f0279886adaf1081b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Feij=C3=B3?= Date: Wed, 2 Apr 2025 14:56:14 -0300 Subject: [PATCH 2/4] chore: Handle B2B users --- .../api/src/platforms/vtex/resolvers/validateSession.ts | 8 +++++--- packages/core/src/sdk/session/index.ts | 7 +++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/api/src/platforms/vtex/resolvers/validateSession.ts b/packages/api/src/platforms/vtex/resolvers/validateSession.ts index aa727a696c..2b60ae0b35 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateSession.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateSession.ts @@ -145,9 +145,11 @@ export const validateSession = async ( seller: seller?.id, hasOnlyDefaultSalesChannel: !store?.channel?.value, }), - b2b: { - customerId: authentication?.customerId?.value ?? '', - }, + b2b: authentication?.customerId?.value + ? { + customerId: authentication?.customerId?.value ?? '', + } + : null, marketingData, person: profile?.id ? { diff --git a/packages/core/src/sdk/session/index.ts b/packages/core/src/sdk/session/index.ts index d074f25998..4b43597e8b 100644 --- a/packages/core/src/sdk/session/index.ts +++ b/packages/core/src/sdk/session/index.ts @@ -62,11 +62,10 @@ export const mutation = gql(` export const validateSession = async (session: Session) => { // If deliveryPromise is enabled and there is no postalCode in the session if (storeConfig.deliveryPromise?.enabled && !session.postalCode) { - const isLoggedIn = !!session.person?.id + const userId = session.b2b?.customerId ?? session.person?.id - // If user is logged try to get the location (postalCode / geoCoordinates / country) from the user's address - if (isLoggedIn) { - const userId = session.person?.id + // If user is logged in try to get the location (postalCode, geoCoordinates and country) from the user's address + if (userId) { const address = await getSavedAddress(userId) // Save the location in the session From 6f8f9b3135e3748afc306360bbffddc007200d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Feij=C3=B3?= Date: Wed, 2 Apr 2025 15:23:56 -0300 Subject: [PATCH 3/4] fix: Remove unnecessary fallback value --- packages/api/src/platforms/vtex/resolvers/validateSession.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/api/src/platforms/vtex/resolvers/validateSession.ts b/packages/api/src/platforms/vtex/resolvers/validateSession.ts index 2b60ae0b35..568cfe5cae 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateSession.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateSession.ts @@ -146,9 +146,7 @@ export const validateSession = async ( hasOnlyDefaultSalesChannel: !store?.channel?.value, }), b2b: authentication?.customerId?.value - ? { - customerId: authentication?.customerId?.value ?? '', - } + ? { customerId: authentication.customerId.value } : null, marketingData, person: profile?.id From cdb12e7ae512b2cd050bc0fb4e145e82ff07f767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Feij=C3=B3?= Date: Fri, 16 May 2025 13:50:40 -0300 Subject: [PATCH 4/4] chore: Remove `getGeoCoordinates` function --- .../vtex/resolvers/validateSession.ts | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/packages/api/src/platforms/vtex/resolvers/validateSession.ts b/packages/api/src/platforms/vtex/resolvers/validateSession.ts index 568cfe5cae..1a624d2634 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateSession.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateSession.ts @@ -30,28 +30,6 @@ async function getPreciseLocationData( } } -async function getGeoCoordinates( - clients: Context['clients'], - country: string, - postalCode: string -) { - try { - const address = await clients.commerce.checkout.address({ - postalCode, - country, - }) - - const [longitude, latitude] = address.geoCoordinates - return { latitude, longitude } - } catch (err) { - console.error( - `Error while getting geo coordinates for the current postal code (${postalCode}) and country (${country}).\n` - ) - - throw err - } -} - export const validateSession = async ( _: any, { session: oldSession, search }: MutationValidateSessionArgs,