-
Notifications
You must be signed in to change notification settings - Fork 84
fix(api): preserve orderForm sales channel in validateCart #3435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2336cda
fix(api): preserve orderForm sales channel in validateCart
eduardoformiga 28509c3
test(api): colocate commerce client tests under commerce/index.test.ts
eduardoformiga 3211231
fix(api): address Sonar findings in validateCart and orderForm URL
eduardoformiga 2dbe295
test(core): cover waitForSessionValidated for Sonar new-code coverage
eduardoformiga d5be27c
docs(api): add before/after screenshots for validateCart PR
eduardoformiga eaaf032
Revert "docs(api): add before/after screenshots for validateCart PR"
eduardoformiga 09b051b
fix(api): keep orderForm sales channel after stale validateCart sync
eduardoformiga 8943da7
fix(api): sync adopted orderForm sales channel into session
eduardoformiga c825e8c
Merge branch 'dev' into fix/validate-cart-stale-sales-channel
eduardoformiga 5f6e8e9
fix(api): address Sonar and harden session sales-channel sync
eduardoformiga 5726ef5
test(api): cover validateSession helpers for Sonar new-code gate
eduardoformiga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { Channel } from './channel' | ||
| import ChannelMarshal from './channel' | ||
|
|
||
| /** | ||
| * When another system changed the orderForm (stale cartEtag), the orderForm's | ||
| * sales channel is authoritative. Returns a channel string to apply via | ||
| * `mutateChannelContext`, or `null` when no adoption is needed. | ||
| */ | ||
| export function channelAfterExternalOrderFormSync( | ||
| currentChannel: Required<Channel>, | ||
| orderFormSalesChannel: string | null | undefined, | ||
| isOrderFormStale: boolean | ||
| ): string | null { | ||
| if (!isOrderFormStale) { | ||
| return null | ||
| } | ||
|
|
||
| if (orderFormSalesChannel == null || orderFormSalesChannel === '') { | ||
| return null | ||
| } | ||
|
|
||
| if ( | ||
| String(orderFormSalesChannel) === String(currentChannel.salesChannel ?? '') | ||
| ) { | ||
| return null | ||
| } | ||
|
|
||
| return ChannelMarshal.stringify({ | ||
| ...currentChannel, | ||
| salesChannel: String(orderFormSalesChannel), | ||
| hasOnlyDefaultSalesChannel: false, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * After a non-stale validation, the session owns the channel. If session SC | ||
| * differs from the orderForm's SC (e.g. locale/binding switch), the cart must | ||
| * be re-fetched with the session SC so Checkout recalculates under the new | ||
| * trade policy. | ||
| */ | ||
| export function shouldRefetchOrderFormWithSessionSalesChannel( | ||
| sessionSalesChannel: string | undefined, | ||
| orderFormSalesChannel: string | null | undefined, | ||
| isOrderFormStale: boolean | ||
| ): boolean { | ||
| if (isOrderFormStale) { | ||
| return false | ||
| } | ||
|
|
||
| if (!sessionSalesChannel || !orderFormSalesChannel) { | ||
| return false | ||
| } | ||
|
|
||
| return String(sessionSalesChannel) !== String(orderFormSalesChannel) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/api/test/unit/platforms/vtex/utils/cartSalesChannel.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| channelAfterExternalOrderFormSync, | ||
| shouldRefetchOrderFormWithSessionSalesChannel, | ||
| } from '../../../../../src/platforms/vtex/utils/cartSalesChannel' | ||
| import type { Channel } from '../../../../../src/platforms/vtex/utils/channel' | ||
|
|
||
| const baseChannel = (salesChannel: string): Required<Channel> => ({ | ||
| salesChannel, | ||
| regionId: '', | ||
| seller: '', | ||
| hasOnlyDefaultSalesChannel: true, | ||
| }) | ||
|
|
||
| describe('channelAfterExternalOrderFormSync', () => { | ||
| it('returns null when the orderForm is not stale', () => { | ||
| expect( | ||
| channelAfterExternalOrderFormSync(baseChannel('1'), '4', false) | ||
| ).toBeNull() | ||
| }) | ||
|
|
||
| it('returns null when orderForm SC matches session SC', () => { | ||
| expect( | ||
| channelAfterExternalOrderFormSync(baseChannel('4'), '4', true) | ||
| ).toBeNull() | ||
| }) | ||
|
|
||
| it('returns null when orderForm SC is missing', () => { | ||
| expect( | ||
| channelAfterExternalOrderFormSync(baseChannel('1'), null, true) | ||
| ).toBeNull() | ||
| expect( | ||
| channelAfterExternalOrderFormSync(baseChannel('1'), '', true) | ||
| ).toBeNull() | ||
| }) | ||
|
|
||
| it('adopts orderForm SC when stale and divergent (Quick Order case)', () => { | ||
| const result = channelAfterExternalOrderFormSync( | ||
| baseChannel('1'), | ||
| '4', | ||
| true | ||
| ) | ||
|
|
||
| if (result === null) { | ||
| throw new Error('expected channel string after adopting orderForm SC') | ||
| } | ||
|
|
||
| expect(JSON.parse(result)).toMatchObject({ | ||
| salesChannel: '4', | ||
| hasOnlyDefaultSalesChannel: false, | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('shouldRefetchOrderFormWithSessionSalesChannel', () => { | ||
| it('does not refetch when the orderForm is externally stale', () => { | ||
| expect(shouldRefetchOrderFormWithSessionSalesChannel('1', '4', true)).toBe( | ||
| false | ||
| ) | ||
| }) | ||
|
|
||
| it('does not refetch when SCs already match', () => { | ||
| expect(shouldRefetchOrderFormWithSessionSalesChannel('1', '1', false)).toBe( | ||
| false | ||
| ) | ||
| }) | ||
|
|
||
| it('refetches when session SC diverges and cart is not stale (locale switch)', () => { | ||
| expect(shouldRefetchOrderFormWithSessionSalesChannel('2', '1', false)).toBe( | ||
| true | ||
| ) | ||
| }) | ||
|
|
||
| it('does not refetch when either SC is missing', () => { | ||
| expect( | ||
| shouldRefetchOrderFormWithSessionSalesChannel(undefined, '1', false) | ||
| ).toBe(false) | ||
| expect( | ||
| shouldRefetchOrderFormWithSessionSalesChannel('1', null, false) | ||
| ).toBe(false) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.