diff --git a/packages/api/src/__generated__/schema.ts b/packages/api/src/__generated__/schema.ts index 65741eb602..52c0695539 100644 --- a/packages/api/src/__generated__/schema.ts +++ b/packages/api/src/__generated__/schema.ts @@ -173,6 +173,7 @@ export type IStoreB2B = { firstName?: Maybe; isRepresentative?: Maybe; lastName?: Maybe; + savedPostalCode?: Maybe; unitId?: Maybe; unitName?: Maybe; userEmail?: Maybe; @@ -801,6 +802,7 @@ export type StoreB2B = { firstName?: Maybe; isRepresentative?: Maybe; lastName?: Maybe; + savedPostalCode?: Maybe; unitId?: Maybe; unitName?: Maybe; userEmail?: Maybe; diff --git a/packages/api/src/platforms/vtex/clients/commerce/index.ts b/packages/api/src/platforms/vtex/clients/commerce/index.ts index dd97bd42b3..004e3df1e2 100644 --- a/packages/api/src/platforms/vtex/clients/commerce/index.ts +++ b/packages/api/src/platforms/vtex/clients/commerce/index.ts @@ -395,7 +395,7 @@ export const VtexCommerce = ( params.set( 'items', - 'profile.id,profile.email,profile.firstName,profile.lastName,shopper.firstName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol,authentication.customerId,authentication.storeUserId,authentication.storeUserEmail,authentication.unitId,authentication.unitName,checkout.regionId' + 'profile.id,profile.email,profile.firstName,profile.lastName,shopper.firstName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol,authentication.customerId,authentication.storeUserId,authentication.storeUserEmail,authentication.unitId,authentication.unitName,checkout.regionId,public.postalCode' ) const headers: HeadersInit = withCookie({ diff --git a/packages/api/src/platforms/vtex/clients/commerce/types/Session.ts b/packages/api/src/platforms/vtex/clients/commerce/types/Session.ts index d7ce84ad53..3471901c72 100644 --- a/packages/api/src/platforms/vtex/clients/commerce/types/Session.ts +++ b/packages/api/src/platforms/vtex/clients/commerce/types/Session.ts @@ -51,4 +51,5 @@ export interface Checkout { export interface Public { orderFormId?: Value items?: Value + postalCode?: Value } diff --git a/packages/api/src/platforms/vtex/resolvers/validateSession.ts b/packages/api/src/platforms/vtex/resolvers/validateSession.ts index ff3a69fbdb..aadf45f4d7 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateSession.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateSession.ts @@ -104,6 +104,7 @@ export const validateSession = async ( const store = sessionData?.namespaces.store ?? null const authentication = sessionData?.namespaces.authentication ?? null const checkout = sessionData?.namespaces.checkout ?? null + const publicData = sessionData?.namespaces.public ?? null // Set seller only if it's inside a region let seller @@ -124,7 +125,7 @@ export const validateSession = async ( code: store?.currencyCode?.value ?? oldSession.currency.code, symbol: store?.currencySymbol?.value ?? oldSession.currency.symbol, }, - country: store?.countryCode?.value ?? oldSession.country, + country: store?.countryCode?.value ?? country, channel: ChannelMarshal.stringify({ salesChannel: store?.channel?.value ?? channel.salesChannel, regionId: checkout?.regionId?.value ?? channel.regionId, @@ -141,6 +142,7 @@ export const validateSession = async ( lastName: profile?.lastName?.value ?? '', userName: shopper?.firstName?.value ?? '', // shopper userEmail: authentication?.storeUserEmail.value ?? '', + savedPostalCode: publicData?.postalCode?.value ?? '', } : null, marketingData, diff --git a/packages/api/src/typeDefs/session.graphql b/packages/api/src/typeDefs/session.graphql index 4aec0daa4a..53c9e2276f 100644 --- a/packages/api/src/typeDefs/session.graphql +++ b/packages/api/src/typeDefs/session.graphql @@ -193,6 +193,7 @@ type StoreB2B { lastName: String userName: String userEmail: String + savedPostalCode: String } input IStoreB2B { @@ -204,6 +205,7 @@ input IStoreB2B { lastName: String userName: String userEmail: String + savedPostalCode: String } """ diff --git a/packages/core/@generated/gql.ts b/packages/core/@generated/gql.ts index f1ccfe51d5..29a50f3d84 100644 --- a/packages/core/@generated/gql.ts +++ b/packages/core/@generated/gql.ts @@ -78,7 +78,7 @@ const documents = { types.ClientSearchSuggestionsQueryDocument, '\n query ClientTopSearchSuggestionsQuery(\n $term: String!\n $selectedFacets: [IStoreSelectedFacet!]\n ) {\n ...ClientTopSearchSuggestions\n search(first: 5, term: $term, selectedFacets: $selectedFacets) {\n suggestions {\n terms {\n value\n }\n }\n }\n }\n': types.ClientTopSearchSuggestionsQueryDocument, - '\n mutation ValidateSession($session: IStoreSession!, $search: String!) {\n validateSession(session: $session, search: $search) {\n locale\n channel\n country\n addressType\n postalCode\n city\n deliveryMode {\n deliveryChannel\n deliveryMethod\n deliveryWindow {\n startDate\n endDate\n }\n }\n geoCoordinates {\n latitude\n longitude\n }\n currency {\n code\n symbol\n }\n person {\n id\n email\n givenName\n familyName\n }\n b2b {\n customerId\n isRepresentative\n unitName\n unitId\n firstName\n lastName\n userName\n userEmail\n }\n marketingData {\n utmCampaign\n utmMedium\n utmSource\n utmiCampaign\n utmiPage\n utmiPart\n }\n }\n }\n': + '\n mutation ValidateSession($session: IStoreSession!, $search: String!) {\n validateSession(session: $session, search: $search) {\n locale\n channel\n country\n addressType\n postalCode\n city\n deliveryMode {\n deliveryChannel\n deliveryMethod\n deliveryWindow {\n startDate\n endDate\n }\n }\n geoCoordinates {\n latitude\n longitude\n }\n currency {\n code\n symbol\n }\n person {\n id\n email\n givenName\n familyName\n }\n b2b {\n customerId\n isRepresentative\n unitName\n unitId\n firstName\n lastName\n userName\n userEmail\n savedPostalCode\n }\n marketingData {\n utmCampaign\n utmMedium\n utmSource\n utmiCampaign\n utmiPage\n utmiPart\n }\n }\n }\n': types.ValidateSessionDocument, '\n query ClientShippingSimulationQuery(\n $postalCode: String!\n $country: String!\n $items: [IShippingItem!]!\n ) {\n ...ClientShippingSimulation\n shipping(items: $items, postalCode: $postalCode, country: $country) {\n logisticsInfo {\n slas {\n carrier\n price\n availableDeliveryWindows {\n startDateUtc\n endDateUtc\n price\n listPrice\n }\n shippingEstimate\n localizedEstimates\n deliveryChannel\n }\n }\n address {\n city\n neighborhood\n state\n }\n }\n }\n': types.ClientShippingSimulationQueryDocument, @@ -288,7 +288,7 @@ export function gql( * The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ export function gql( - source: '\n mutation ValidateSession($session: IStoreSession!, $search: String!) {\n validateSession(session: $session, search: $search) {\n locale\n channel\n country\n addressType\n postalCode\n city\n deliveryMode {\n deliveryChannel\n deliveryMethod\n deliveryWindow {\n startDate\n endDate\n }\n }\n geoCoordinates {\n latitude\n longitude\n }\n currency {\n code\n symbol\n }\n person {\n id\n email\n givenName\n familyName\n }\n b2b {\n customerId\n isRepresentative\n unitName\n unitId\n firstName\n lastName\n userName\n userEmail\n }\n marketingData {\n utmCampaign\n utmMedium\n utmSource\n utmiCampaign\n utmiPage\n utmiPart\n }\n }\n }\n' + source: '\n mutation ValidateSession($session: IStoreSession!, $search: String!) {\n validateSession(session: $session, search: $search) {\n locale\n channel\n country\n addressType\n postalCode\n city\n deliveryMode {\n deliveryChannel\n deliveryMethod\n deliveryWindow {\n startDate\n endDate\n }\n }\n geoCoordinates {\n latitude\n longitude\n }\n currency {\n code\n symbol\n }\n person {\n id\n email\n givenName\n familyName\n }\n b2b {\n customerId\n isRepresentative\n unitName\n unitId\n firstName\n lastName\n userName\n userEmail\n savedPostalCode\n }\n marketingData {\n utmCampaign\n utmMedium\n utmSource\n utmiCampaign\n utmiPage\n utmiPart\n }\n }\n }\n' ): typeof import('./graphql').ValidateSessionDocument /** * The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. diff --git a/packages/core/@generated/graphql.ts b/packages/core/@generated/graphql.ts index 7ea4f0cfd7..42d8a29ba1 100644 --- a/packages/core/@generated/graphql.ts +++ b/packages/core/@generated/graphql.ts @@ -187,6 +187,7 @@ export type IStoreB2B = { firstName: InputMaybe isRepresentative: InputMaybe lastName: InputMaybe + savedPostalCode: InputMaybe unitId: InputMaybe unitName: InputMaybe userEmail: InputMaybe @@ -775,6 +776,7 @@ export type StoreB2B = { firstName: Maybe isRepresentative: Maybe lastName: Maybe + savedPostalCode: Maybe unitId: Maybe unitName: Maybe userEmail: Maybe @@ -2909,6 +2911,7 @@ export type ValidateSessionMutation = { lastName: string | null userName: string | null userEmail: string | null + savedPostalCode: string | null } | null marketingData: { utmCampaign: string | null @@ -3662,7 +3665,7 @@ export const ClientTopSearchSuggestionsQueryDocument = { export const ValidateSessionDocument = { __meta__: { operationName: 'ValidateSession', - operationHash: '6f6d66826c836c3633a8dc3d2fe8220c386584d6', + operationHash: '259dd10b1c65ce4b20c9181feb7bec85ecb402e6', }, } as unknown as TypedDocumentString< ValidateSessionMutation, diff --git a/packages/core/src/sdk/session/index.ts b/packages/core/src/sdk/session/index.ts index 4d717f45f3..d7df0d0f37 100644 --- a/packages/core/src/sdk/session/index.ts +++ b/packages/core/src/sdk/session/index.ts @@ -53,6 +53,7 @@ export const mutation = gql(` lastName userName userEmail + savedPostalCode } marketingData { utmCampaign @@ -69,12 +70,17 @@ 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 + // Case B2B: If a B2B shopper is logged in and a saved address is available, the postalCode field is automatically updated with the postal code from that address by the B2B session apps (shopper-session and profile-session). + if (session.b2b && session.b2b?.savedPostalCode) { + sessionStore.set({ + ...session, + postalCode: session.b2b.savedPostalCode, + }) + } - // If user is logged try to get the location (postalCode / geoCoordinates / country) from the user's address - if (isLoggedIn) { - const userId = session.person?.id - const address = await getSavedAddress(userId) + // Case B2C: If a B2C shopper is logged in, try to get the location (postalCode, geoCoordinates, and country) from their saved address + else if (session.person?.id) { + const address = await getSavedAddress(session.person?.id) // Save the location in the session if (address) { @@ -93,12 +99,11 @@ export const validateSession = async (session: Session) => { }) } } else { - // Use the initial postalCode defined in discovery.config.js + // Fallback: use the initial postalCode defined in discovery.config.js const initialPostalCode = defaultStore.readInitial().postalCode - if (!!initialPostalCode) { + !!initialPostalCode && sessionStore.set({ ...session, postalCode: initialPostalCode }) - } } } diff --git a/packages/sdk/src/session/index.ts b/packages/sdk/src/session/index.ts index 9bd31e307b..00f8295ecd 100644 --- a/packages/sdk/src/session/index.ts +++ b/packages/sdk/src/session/index.ts @@ -46,6 +46,7 @@ export interface B2bInfo { lastName: string userName: string userEmail: string + savedPostalCode: string } export interface Session {