-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(core): Recalculate active orders for current pricing, promotions & shipping eligibility #4928
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
Open
grolmus
wants to merge
21
commits into
vendurehq:minor
Choose a base branch
from
grolmus:mgrolmus/oss-274-cart-does-not-update-discounts-shipping-pricing-or
base: minor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
da6a4e7
docs: Add OSS-274 order recalculation design spec
grolmus e83b2bf
docs: Add OSS-274 order recalculation implementation plan
grolmus 5405a63
feat(core): Add OrderRecalculationStrategy with TTL built-in
grolmus 52abb64
feat(core): Add orderRecalculationStrategy to OrderOptions
grolmus 708bb69
feat(core): Add applyPriceAdjustmentsIfStale and pricingUpdatedAt bump
grolmus a32568f
feat(core): Recalculate stale active order on read
grolmus 688a96c
feat(core): Recalculate and verify shipping eligibility at checkout
grolmus 90a7217
fix(core): Exclude shipping from read-time order recalculation
grolmus 6af0e24
Merge remote-tracking branch 'upstream/master' into mgrolmus/oss-274-…
grolmus 3d5a714
fix(core): Register orderRecalculationStrategy for injectable lifecycle
grolmus a86ee61
fix(core): Make read-time order recalculation transactional and locked
grolmus 37b4b2e
fix(core): Recalculate at checkout only after all guards pass
grolmus 67404e2
docs(core): Correct order recalculation shipping/docs annotations
grolmus 935b5df
test(core): Tighten order recalculation e2e assertions
grolmus b4835bc
docs(core): Clarify read-time recalc excludes shipping in applyPriceA…
grolmus 6e32e49
chore: Remove OSS-274 planning docs from branch
grolmus b08d211
fix(core): Re-test shipping promotions on read and gate checkout elig…
grolmus ff22971
fix(core): Recalculate prices at checkout regardless of shipping pres…
grolmus 22d88c3
fix(core): Store pricingUpdatedAt with fractional-second precision to…
grolmus 7dab28d
docs(core): Correct OrderRecalculationStrategy docs for shipping-prom…
grolmus 594245e
Merge remote-tracking branch 'origin/minor' into mgrolmus/oss-274-car…
grolmus 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
17 changes: 17 additions & 0 deletions
17
packages/core/src/config/order/no-order-recalculation-strategy.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,17 @@ | ||
| import { OrderRecalculationStrategy } from './order-recalculation-strategy'; | ||
|
|
||
| /** | ||
| * @description | ||
| * The default {@link OrderRecalculationStrategy} which never triggers a read-time recalculation. | ||
| * This preserves the behaviour of Vendure prior to v3.8.0, where an active Order's prices are only | ||
| * re-calculated on write mutations. | ||
| * | ||
| * @docsCategory orders | ||
| * @docsPage OrderRecalculationStrategy | ||
| * @since 3.8.0 | ||
| */ | ||
| export class NoOrderRecalculationStrategy implements OrderRecalculationStrategy { | ||
| shouldRecalculate(): boolean { | ||
| return false; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
packages/core/src/config/order/order-recalculation-strategy.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,42 @@ | ||
| import { RequestContext } from '../../api/common/request-context'; | ||
| import { InjectableStrategy } from '../../common/types/injectable-strategy'; | ||
| import { Order } from '../../entity/order/order.entity'; | ||
|
|
||
| /** | ||
| * @description | ||
| * This strategy determines whether an active Order's prices, promotions, taxes and shipping | ||
| * promotions should be re-calculated when the Order is read (e.g. via the `activeOrder` query). | ||
| * Recalculation is only ever attempted for Orders in the `AddingItems` state. | ||
| * | ||
| * **Note:** the shipping *method* and *rate* are NOT re-evaluated on read, so a read never silently | ||
| * swaps the customer's chosen method — those are re-evaluated when the Order transitions to | ||
| * `ArrangingPayment` (checkout). Shipping *promotions* are re-tested, so a now-inactive shipping | ||
| * promotion's discount is cleared rather than surviving on the Order. | ||
| * | ||
| * The default {@link NoOrderRecalculationStrategy} never triggers a recalculation, preserving the | ||
| * historical behaviour whereby an active Order's prices are only updated on write mutations. Use | ||
| * {@link TtlOrderRecalculationStrategy} (or a custom implementation) to keep long-lived carts in | ||
| * sync with changing product prices and promotions. | ||
| * | ||
| * :::info | ||
| * | ||
| * This is configured via the `orderOptions.orderRecalculationStrategy` property of your VendureConfig. | ||
| * | ||
| * ::: | ||
| * | ||
| * @docsCategory orders | ||
| * @docsPage OrderRecalculationStrategy | ||
| * @since 3.8.0 | ||
| */ | ||
| export interface OrderRecalculationStrategy extends InjectableStrategy { | ||
| /** | ||
| * @description | ||
| * Return `true` to trigger a recalculation of the Order's prices, promotions and taxes before | ||
| * it is returned from the active-order read path. | ||
| * | ||
| * **Note:** Implementations should rely only on fields that are always present on the Order | ||
| * (e.g. `pricingUpdatedAt`, `state`, `active`). Deep relations (lines, promotions, etc.) may | ||
| * not be loaded on the Order passed to this method. | ||
| */ | ||
| shouldRecalculate(ctx: RequestContext, order: Order): boolean | Promise<boolean>; | ||
| } |
31 changes: 31 additions & 0 deletions
31
packages/core/src/config/order/ttl-order-recalculation-strategy.spec.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,31 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { Order } from '../../entity/order/order.entity'; | ||
|
|
||
| import { TtlOrderRecalculationStrategy } from './ttl-order-recalculation-strategy'; | ||
|
|
||
| describe('TtlOrderRecalculationStrategy', () => { | ||
| const ctx = {} as any; | ||
| const strategy = new TtlOrderRecalculationStrategy({ ttlMs: 60_000 }); | ||
|
|
||
| function orderWith(pricingUpdatedAt: Date | undefined): Order { | ||
| return { pricingUpdatedAt } as Order; | ||
| } | ||
|
|
||
| it('is stale when pricingUpdatedAt is null/undefined', () => { | ||
| expect(strategy.shouldRecalculate(ctx, orderWith(undefined))).toBe(true); | ||
| }); | ||
|
|
||
| it('is not stale within the TTL window', () => { | ||
| expect(strategy.shouldRecalculate(ctx, orderWith(new Date(Date.now() - 1_000)))).toBe(false); | ||
| }); | ||
|
|
||
| it('is stale past the TTL window', () => { | ||
| expect(strategy.shouldRecalculate(ctx, orderWith(new Date(Date.now() - 120_000)))).toBe(true); | ||
| }); | ||
|
|
||
| it('is stale at exactly the TTL boundary (>= comparison)', () => { | ||
| // At exactly Date.now() - ttlMs the order is considered stale (>= comparison). | ||
| expect(strategy.shouldRecalculate(ctx, orderWith(new Date(Date.now() - 60_000)))).toBe(true); | ||
| }); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
packages/core/src/config/order/ttl-order-recalculation-strategy.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,38 @@ | ||
| import { RequestContext } from '../../api/common/request-context'; | ||
| import { Order } from '../../entity/order/order.entity'; | ||
|
|
||
| import { OrderRecalculationStrategy } from './order-recalculation-strategy'; | ||
|
|
||
| /** | ||
| * @description | ||
| * An {@link OrderRecalculationStrategy} which recalculates an active Order when the time since its | ||
| * last recalculation exceeds the configured `ttlMs`. This mirrors the "price freeze period" model | ||
| * used by other e-commerce platforms: quoted prices remain stable for a period, then refresh on the | ||
| * next access. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { TtlOrderRecalculationStrategy, VendureConfig } from '@vendure/core'; | ||
| * | ||
| * export const config: VendureConfig = { | ||
| * // ... | ||
| * orderOptions: { | ||
| * orderRecalculationStrategy: new TtlOrderRecalculationStrategy({ ttlMs: 5 * 60 * 1000 }), | ||
| * }, | ||
| * }; | ||
| * ``` | ||
| * | ||
| * @docsCategory orders | ||
| * @docsPage OrderRecalculationStrategy | ||
| * @since 3.8.0 | ||
| */ | ||
| export class TtlOrderRecalculationStrategy implements OrderRecalculationStrategy { | ||
| constructor(private options: { ttlMs: number }) {} | ||
|
|
||
| shouldRecalculate(ctx: RequestContext, order: Order): boolean { | ||
| if (order.pricingUpdatedAt == null) { | ||
| return true; | ||
| } | ||
| return Date.now() - order.pricingUpdatedAt.getTime() >= this.options.ttlMs; | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking (CodeRabbit flagged this too):
recalculateShippingPromotionshas a thorough doc block whose meaning depends on this flag, but this one has none.