Repository navigation
chore: sync storefront and checkout locale - #3210
Conversation
WalkthroughAdds a new checkout.clientPreferencesData method to the VTEX commerce client, extracts client preferences into a ClientPreferencesData interface, and updates validateCart to conditionally POST clientPreferencesData (locale) to the OrderForm when session and OrderForm locales differ. Changes
Sequence Diagram(s)sequenceDiagram
participant Resolver as Client/Resolver
participant Commerce as VtexCommerceClient
participant Cookies as StoreCookies
participant VTEX as VTEX API
Resolver->>Commerce: GET OrderForm (validateCart)
Commerce->>VTEX: GET /api/checkout/pub/orderForm/{id}
VTEX-->>Commerce: OrderForm (includes clientPreferencesData.locale)
Resolver->>Resolver: compare session.locale vs orderForm.clientPreferencesData.locale
alt locales differ
Resolver->>Commerce: checkout.clientPreferencesData({ id, clientPreferencesData })
Commerce->>Cookies: read cookies -> build headers
Cookies-->>Commerce: cookie & auth headers
Commerce->>VTEX: POST /api/checkout/pub/orderForm/{id}/attachments/clientPreferencesData
VTEX-->>Commerce: Updated OrderForm
Commerce-->>Resolver: return Updated OrderForm
else locales match
Resolver-->>Resolver: continue with existing OrderForm
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.
In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts`:
- Around line 379-386: The TypeScript error occurs because ClientPreferencesData
requires both locale and optinNewsLetter but the call to
commerce.checkout.clientPreferencesData only supplies locale; update the payload
to include the existing optinNewsLetter (or other existing clientPreferencesData
fields) when calling commerce.checkout.clientPreferencesData so the shape
matches the ClientPreferencesData interface—i.e., merge/spread
orderForm.clientPreferencesData (or explicitly pass optinNewsLetter:
orderForm.clientPreferencesData?.optinNewsLetter) alongside locale in the call
that updates orderForm, and alternatively consider making optinNewsLetter
optional in the ClientPreferencesData type if the API truly accepts partial
updates.
- Around line 378-386: The locale sync call inside validateCart should be made
non-blocking: wrap the call to commerce.checkout.clientPreferencesData (the
block that assigns orderForm when locale &&
orderForm.clientPreferencesData?.locale !== locale) in a try-catch so transient
errors don't bubble up and break the mutation; on failure catch the error, log a
warning (or processLogger.warn) including the error and the locale, and continue
using the existing orderForm so cart validation proceeds normally.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Before applying any fix, first verify the finding against the current code and decide whether a code change is actually needed. If the finding is not valid or no change is required, do not modify code for that item and briefly explain why it was skipped. In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts`: - Around line 378-386: The locale sync call inside validateCart should be made non-blocking: wrap the call to commerce.checkout.clientPreferencesData (the block that assigns orderForm when locale && orderForm.clientPreferencesData?.locale !== locale) in a try-catch so transient errors don't bubble up and break the mutation; on failure catch the error, log a warning (or processLogger.warn) including the error and the locale, and continue using the existing orderForm so cart validation proceeds normally.packages/api/src/platforms/vtex/resolvers/validateCart.ts (1)
378-386: Consider wrapping locale sync in a try-catch to avoid blocking cart validation.If the
clientPreferencesDatacall fails (e.g., transient network error), the entirevalidateCartmutation will throw, preventing the user from interacting with their cart. Locale sync is a non-critical enhancement — it shouldn't block the core cart flow.Proposed defensive handling
// Sync session locale to checkout so OrderForm has the same clientPreferencesData.locale if (locale && orderForm.clientPreferencesData?.locale !== locale) { - orderForm = await commerce.checkout.clientPreferencesData({ - id: orderForm.orderFormId, - clientPreferencesData: { - locale, - }, - }) + try { + orderForm = await commerce.checkout.clientPreferencesData({ + id: orderForm.orderFormId, + clientPreferencesData: { + locale, + }, + }) + } catch (err) { + console.error('Failed to sync locale to orderForm clientPreferencesData', err) + } }🤖 Prompt for AI Agents
Before applying any fix, first verify the finding against the current code and decide whether a code change is actually needed. If the finding is not valid or no change is required, do not modify code for that item and briefly explain why it was skipped. In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts` around lines 378 - 386, The locale sync call inside validateCart should be made non-blocking: wrap the call to commerce.checkout.clientPreferencesData (the block that assigns orderForm when locale && orderForm.clientPreferencesData?.locale !== locale) in a try-catch so transient errors don't bubble up and break the mutation; on failure catch the error, log a warning (or processLogger.warn) including the error and the locale, and continue using the existing orderForm so cart validation proceeds normally.
There was a problem hiding this comment.
🤖 Fix all issues with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.
In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts`:
- Around line 156-159: Change the optinNewsLetter property on the
ClientPreferencesData interface from "any | null" to "boolean | null" so it
matches the VTEX Checkout API and improves type safety; update the declaration
in the ClientPreferencesData interface (symbol: ClientPreferencesData, property:
optinNewsLetter) accordingly and run type checks to ensure no downstream type
errors.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Before applying any fix, first verify the finding against the current code and decide whether a code change is actually needed. If the finding is not valid or no change is required, do not modify code for that item and briefly explain why it was skipped. In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts`: - Around line 156-159: Change the optinNewsLetter property on the ClientPreferencesData interface from "any | null" to "boolean | null" so it matches the VTEX Checkout API and improves type safety; update the declaration in the ClientPreferencesData interface (symbol: ClientPreferencesData, property: optinNewsLetter) accordingly and run type checks to ensure no downstream type errors.packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts (1)
156-159: Useboolean | nullinstead ofany | null.The
anytype already subsumesnulland every other value in TypeScript, making the union redundant. More importantly, the VTEX Checkout API definesoptinNewsLetteras aboolean, so tightening this improves type safety and aligns with the actual API contract per the coding guidelines.♻️ Suggested diff
export interface ClientPreferencesData { locale: string - optinNewsLetter?: any | null + optinNewsLetter?: boolean | null }🤖 Prompt for AI Agents
Before applying any fix, first verify the finding against the current code and decide whether a code change is actually needed. If the finding is not valid or no change is required, do not modify code for that item and briefly explain why it was skipped. In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts` around lines 156 - 159, Change the optinNewsLetter property on the ClientPreferencesData interface from "any | null" to "boolean | null" so it matches the VTEX Checkout API and improves type safety; update the declaration in the ClientPreferencesData interface (symbol: ClientPreferencesData, property: optinNewsLetter) accordingly and run type checks to ensure no downstream type errors.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/api/src/platforms/vtex/clients/commerce/types/OrderForm.ts`:
- Line 123: The OrderForm type's clientPreferencesData field is non-nullable but
the VTEX GraphQL schema allows null; change the declaration of
clientPreferencesData in the OrderForm type from ClientPreferencesData to
ClientPreferencesData | null (matching the pattern used for clientProfileData,
shippingData, marketingData) and update any related type imports/usages to
accept null (e.g., places that access form.clientPreferencesData?.locale already
handle it). Ensure the symbol names referenced are clientPreferencesData and
ClientPreferencesData within the OrderForm type so the TypeScript definition
aligns with the API contract.
| if (locale && form.clientPreferencesData?.locale !== locale) { | ||
| return commerce.checkout.clientPreferencesData({ | ||
| id: form.orderFormId, | ||
| clientPreferencesData: { locale }, |
There was a problem hiding this comment.
question: have you checked if this overrides the whole clientPreferencesData in orderform or just overrides the locale? I ask because for marketingData for example we had to merge old and new info to not override the whole object
There was a problem hiding this comment.
niceee!
I've tried a few times, and it's only updating the locale (at least when I was trying)
There is only two fields in this objects (locale and optinNewsLetter - I can only see locale changing)
There was a problem hiding this comment.
you are right, I've tried using the API to set true to optinNewsLetter, and then update only the locale and is overriding 🙈 Lemme try to fix it!!
There was a problem hiding this comment.
locale-update-only.mov
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/api/src/platforms/vtex/resolvers/validateCart.ts (1)
468-471:⚠️ Potential issue | 🔴 CriticalEarly return bypasses locale sync when there are no item/shipping changes.
When
changes.length === 0 && !updateShippingat line 470, the function returnsnullbefore reaching theclientPreferencesDatalocale sync at lines 516–527. If a user switches locale without modifying the cart or shipping, the orderForm locale is never synced to the session locale.Add a locale mismatch check to the early-return guard:
Proposed fix
+ const localeNeedsUpdate = + locale && orderForm.clientPreferencesData?.locale !== locale + // If there are no item changes and no shipping data updates needed, return null - if (changes.length === 0 && !updateShipping) { + if (changes.length === 0 && !updateShipping && !localeNeedsUpdate) { return null }Reuse
localeNeedsUpdateat line 517 to keep the pattern consistent.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts` around lines 468 - 471, The early return checking "changes.length === 0 && !updateShipping" skips the later clientPreferencesData locale sync; update this guard so it also checks whether the session/orderForm locale needs updating before returning. Compute or reuse the existing localeNeedsUpdate (from clientPreferencesData check) and change the condition to only return early when changes.length === 0 && !updateShipping && !localeNeedsUpdate, ensuring the locale sync code in the validateCart flow (clientPreferencesData / localeNeedsUpdate logic) runs when a locale mismatch exists.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/api/src/platforms/vtex/resolvers/validateCart.ts`:
- Around line 468-471: The early return checking "changes.length === 0 &&
!updateShipping" skips the later clientPreferencesData locale sync; update this
guard so it also checks whether the session/orderForm locale needs updating
before returning. Compute or reuse the existing localeNeedsUpdate (from
clientPreferencesData check) and change the condition to only return early when
changes.length === 0 && !updateShipping && !localeNeedsUpdate, ensuring the
locale sync code in the validateCart flow (clientPreferencesData /
localeNeedsUpdate logic) runs when a locale mismatch exists.
What's the purpose of this pull request?
Includes the locale as client preferences information to the orderForm, so can be retrieved in checkout and enable the translation.
How it works?
storepreferencesdataref:This object contains data from the store's configuration. This is already updated with the session salesChannel/store info.
clientPreferencesDataref:Object containing preferences from the client who placed the order. Pass the current session locale to enable translation in checkout.
How to test it?
We will simulate the checkout update, run the project locally, enable localization and run the binding setup.
storePreferencesDataandclientPreferencesData.localefields form orderForm has been updated.locale_checkout_only_when_update_cart.mov
References
Jira Task
Summary by CodeRabbit