Skip to content
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

In-memory order updater #5872

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d9914f2
Set tax adjustment amount without persistance
harmonymjb Jan 3, 2025
1d501dd
Mark adjustments for removal in OrderTaxation
Noah-Silvera Jan 10, 2025
001c16c
Ignore order adjustments marked for destruction
adammathys Jan 31, 2025
e590083
Add `db-query-matchers` gem
benjaminwil Oct 11, 2024
d1a3687
Copy existing `OrderUpdater` implementation
benjaminwil Oct 11, 2024
fe6839b
Add `persist` flag to `#recalculate`
benjaminwil Oct 11, 2024
e0ed371
Add describe block to Shipment#update_amounts test
Noah-Silvera Oct 25, 2024
a32dc65
Conditionally persist Shipment#update_amounts changes
Noah-Silvera Oct 25, 2024
bc61d6f
Preventing InMemoryOrderUpdater#update_shipment_amounts from making d…
Noah-Silvera Oct 25, 2024
66cb920
Rename method that recalculates shipment state
forkata Nov 8, 2024
0d335d2
Rename method that recalculates payment state
forkata Nov 8, 2024
e3d9cfb
Rename update_ private methods
AlistairNorman Nov 22, 2024
5a22afd
Rename recalculate_adjustments
AlistairNorman Nov 22, 2024
51765f8
Remove describe block for private method
AlistairNorman Nov 22, 2024
15f3a0f
Reorder private methods
AlistairNorman Nov 22, 2024
05bc3bf
Test that changes to item totals are respected
adammathys Jan 30, 2025
261aa22
Pass persist flag to legacy promotion recalculator
adammathys Jan 31, 2025
8fd0eb6
Pass persist to promotion.order_adjuster_class
sofiabesenski4 Dec 6, 2024
532a142
Support conditional persist in promotion chooser
AlistairNorman Feb 7, 2025
7abc583
[TODO] Add TODO so we know what to do
forkata Nov 8, 2024
b52d390
Rename order adjuster subject
Noah-Silvera Feb 14, 2025
5bbf0bd
Add missing Promotions::OrderAdjuster spec
Noah-Silvera Feb 14, 2025
c985cff
WIP: Improving the dry run functionality before implementing persist
Noah-Silvera Feb 14, 2025
7f7e255
WIP dealing with additional persistent in order_adjuster related classes
Noah-Silvera Feb 14, 2025
313454e
Set required line item attributes earlier
senemsoy Feb 28, 2025
07add2a
Don't persist line item on promotion application
senemsoy Feb 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Preventing InMemoryOrderUpdater#update_shipment_amounts from making d…
…atabase writes

We have prevented write calls to update the cost and `updated_at` of a
shipment, as well as allowed us to conditionally persist item totals, by
passing down the `persist` argument to that method.

Co-authored-by: Adam Mueller <adam@super.gd>
Co-authored-by: Andrew Stewart <andrew@super.gd>
Co-authored-by: Harmony Evangelina <harmony@super.gd>
Co-authored-by: Kendra Riga <kendra@super.gd>
Co-authored-by: Jared Norman <jared@super.gd>
Co-authored-by: Tom Van Manen <tom@super.gd>
Co-authored-by: Chris Todorov <chris@super.gd>
Co-authored-by: Sofia Besenski <sofia@super.gd>
Co-authored-by: Senem Soy <senem@super.gd>
Co-authored-by: Benjamin Willems <benjamin@super.gd>
  • Loading branch information
11 people committed Mar 7, 2025
commit bc61d6fa602af6b58d31a8a03b00eed4fd9d74da
24 changes: 12 additions & 12 deletions core/app/models/spree/in_memory_order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def initialize(order)
def recalculate(persist: true)
order.transaction do
update_item_count
update_shipment_amounts
update_totals
update_shipment_amounts(persist:)
update_totals(persist:)
if order.completed?
update_payment_state
update_shipments
Expand Down Expand Up @@ -107,15 +107,15 @@ def determine_shipment_state
# fields (promo_total, included_tax_total, additional_tax_total, and
# adjustment_total) on the item.
# @return [void]
def recalculate_adjustments
def recalculate_adjustments(persist:)
# Promotion adjustments must be applied first, then tax adjustments.
# This fits the criteria for VAT tax as outlined here:
# http://www.hmrc.gov.uk/vat/managing/charging/discounts-etc.htm#1
# It also fits the criteria for sales tax as outlined here:
# http://www.boe.ca.gov/formspubs/pub113/
update_promotions
update_tax_adjustments
update_item_totals
update_item_totals(persist:)
end

# Updates the following Order total values:
Expand All @@ -125,15 +125,15 @@ def recalculate_adjustments
# +adjustment_total+ The total value of all adjustments (promotions, credits, etc.)
# +promo_total+ The total value of all promotion adjustments
# +total+ The so-called "order total." This is equivalent to +item_total+ plus +adjustment_total+.
def update_totals
def update_totals(persist:)
update_payment_total
update_item_total
update_shipment_total
update_adjustment_total
update_adjustment_total(persist:)
end

def update_shipment_amounts
shipments.each(&:update_amounts)
def update_shipment_amounts(persist:)
shipments.each { _1.update_amounts(persist:) }
end

# give each of the shipments a chance to update themselves
Expand All @@ -154,8 +154,8 @@ def update_order_total
order.total = order.item_total + order.shipment_total + order.adjustment_total
end

def update_adjustment_total
recalculate_adjustments
def update_adjustment_total(persist:)
recalculate_adjustments(persist:)

all_items = line_items + shipments
# Ignore any adjustments that have been marked for destruction in our
Expand Down Expand Up @@ -210,11 +210,11 @@ def update_cancellations
end
deprecate :update_cancellations, deprecator: Spree.deprecator

def update_item_totals
def update_item_totals(persist:)
[*line_items, *shipments].each do |item|
Spree::Config.item_total_class.new(item).recalculate!

next unless item.changed?
next unless persist && item.changed?

item.update_columns(
promo_total: item.promo_total,
Expand Down
24 changes: 24 additions & 0 deletions core/spec/models/spree/in_memory_order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ module Spree
expect(order.item_count).to eq 1
expect(order.reload.item_count).to eq 0
end

context 'when a shipment is attached to the order' do
let(:shipment) { create(:shipment) }

before do
order.shipments << shipment
end

it 'does not make manipulative database queries' do
expect {
subject
}.not_to make_database_queries(manipulative: true)
end

context 'when the shipment has a selected shipping rate' do
let(:shipment) { create(:shipment, shipping_rates: [build(:shipping_rate, selected: true)]) }

it 'does not make manipulative database queries' do
expect {
subject
}.not_to make_database_queries(manipulative: true)
end
end
end
end

context "when the persist flag is set to 'true'" do
Expand Down