Skip to content

Commit 96fab42

Browse files
committed
Extract order cleanup and total recalculation
Also removes the now-unnecessary `PersistDiscountedOrder` class.
1 parent 0ffc010 commit 96fab42

File tree

6 files changed

+81
-32
lines changed

6 files changed

+81
-32
lines changed

promotions/app/models/solidus_promotions/order_adjuster.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ def call
2020

2121
DiscountOrder.new(order, promotions, dry_run: dry_run).call
2222

23-
unless dry_run
24-
order.save!
25-
end
26-
order
23+
CleanDiscountedOrder.new(order).call
24+
25+
RecalculatePromoTotals.new(order).call
2726
end
2827
end
2928
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusPromotions
4+
class OrderAdjuster
5+
class CleanDiscountedOrder
6+
attr_reader :order
7+
8+
def initialize(order)
9+
@order = order
10+
end
11+
12+
def call
13+
order.line_items.each do |line_item|
14+
line_item.adjustments.select { _1.amount.zero? }.each(&:mark_for_destruction)
15+
end
16+
17+
order.shipments.each do |shipment|
18+
shipment.adjustments.select { _1.amount.zero? }.each(&:mark_for_destruction)
19+
shipment.shipping_rates.each do |shipping_rate|
20+
shipping_rate.discounts.select { _1.amount.zero? }.each(&:mark_for_destruction)
21+
end
22+
end
23+
24+
order
25+
end
26+
end
27+
end
28+
end

promotions/app/models/solidus_promotions/order_adjuster/discount_order.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,6 @@ def call
2525
end
2626
end
2727

28-
order.line_items.each do |line_item|
29-
line_item.adjustments.select { _1.amount.zero? }.each(&:mark_for_destruction)
30-
line_item.promo_total = line_item.adjustments.reject(&:marked_for_destruction?).sum(&:amount)
31-
line_item.adjustment_total = line_item.promo_total
32-
end
33-
34-
order.shipments.each do |shipment|
35-
shipment.adjustments.select { _1.amount.zero? }.each(&:mark_for_destruction)
36-
shipment.promo_total = shipment.adjustments.reject(&:marked_for_destruction?).sum(&:amount)
37-
shipment.shipping_rates.each do |shipping_rate|
38-
shipping_rate.discounts.select { _1.amount.zero? }.each(&:mark_for_destruction)
39-
end
40-
shipment.adjustment_total = shipment.promo_total
41-
end
42-
43-
line_items = order.line_items.reject(&:marked_for_destruction?)
44-
order.item_total = line_items.sum(&:amount)
45-
order.item_count = line_items.sum(&:quantity)
46-
order.promo_total = (line_items + order.shipments.reject(&:marked_for_destruction?)).sum(&:promo_total)
47-
order.adjustment_total = order.promo_total
48-
4928
order
5029
end
5130

@@ -85,7 +64,6 @@ def adjust_shipments(benefits)
8564
def adjust_shipping_rates(benefits)
8665
order.shipments.flat_map(&:shipping_rates).filter_map do |rate|
8766
next unless rate.cost
88-
8967
discounts = generate_discounts(benefits, rate)
9068
chosen_discounts = SolidusPromotions.config.discount_chooser_class.new(discounts).call
9169
(rate.current_lane_discounts - chosen_discounts).each(&:mark_for_destruction)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusPromotions
4+
class OrderAdjuster
5+
class RecalculatePromoTotals
6+
attr_reader :order
7+
8+
def initialize(order)
9+
@order = order
10+
end
11+
12+
def call
13+
order.line_items.each do |line_item|
14+
line_item.promo_total = calculate_promo_total_for_adjustable(line_item)
15+
end
16+
17+
order.shipments.each do |shipment|
18+
shipment.promo_total = calculate_promo_total_for_adjustable(shipment)
19+
end
20+
21+
line_items = order.line_items.reject(&:marked_for_destruction?)
22+
order.item_total = line_items.sum(&:amount)
23+
order.item_count = line_items.sum(&:quantity)
24+
order.promo_total = (line_items + order.shipments.reject(&:marked_for_destruction?)).sum(&:promo_total)
25+
order.adjustment_total = order.promo_total
26+
end
27+
28+
private
29+
30+
def calculate_promo_total_for_adjustable(adjustable)
31+
adjustable
32+
.adjustments
33+
.select(&:promotion?)
34+
.reject(&:marked_for_destruction?)
35+
.sum(&:amount)
36+
end
37+
end
38+
end
39+
end

promotions/spec/models/promotion/integration_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
it "creates a new discounted line item" do
4343
expect(order.adjustments).to be_empty
4444
expect(order.line_items.count).to eq(3)
45+
# 19.99 * 2
4546
expect(order.total).to eq(39.98)
47+
# 19.99 * 2 + 4 * 1
4648
expect(order.item_total).to eq(43.98)
4749
expect(order.item_total_before_tax).to eq(39.98)
4850
expect(order.line_items.flat_map(&:adjustments).length).to eq(1)
@@ -56,6 +58,7 @@
5658
it "creates a new discounted line item" do
5759
expect(order.adjustments).to be_empty
5860
expect(order.line_items.count).to eq(3)
61+
# 19.99 * 3
5962
expect(order.total).to eq(59.97)
6063
expect(order.item_total).to eq(67.97)
6164
expect(order.item_total_before_tax).to eq(59.97)

promotions/spec/models/solidus_promotions/order_adjuster_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
end
2323
let(:adjustable) { order }
2424

25-
context "when not doing a dry-run of a promotion", :pending do
25+
context "when not doing a dry-run of a promotion" do
2626
subject do
2727
order_adjuster.call
2828
end
@@ -117,6 +117,7 @@
117117
it " will not create the adjustment" do
118118
expect {
119119
subject
120+
order.save!
120121
}.not_to change { adjustable.adjustments.length }
121122
end
122123
end
@@ -133,14 +134,15 @@
133134
it "doesn't create an adjustment" do
134135
expect {
135136
subject
137+
order.save!
136138
}.to change { adjustable.adjustments.length }.by(0)
137139
end
138140

139141
context "for an line item that has an adjustment from the old promotion system" do
140142
let(:old_promotion_benefit) { create(:promotion, :with_adjustable_action, apply_automatically: false).actions.first }
141143
let!(:adjustment) { create(:adjustment, source: old_promotion_benefit, adjustable: line_item) }
142144

143-
it "marks the old adjustment for destruction", :pending do
145+
it "marks the old adjustment for destruction" do
144146
adjustable.reload
145147
expect {
146148
subject
@@ -199,7 +201,7 @@
199201
end
200202

201203
it "creates shipping rate discounts" do
202-
expect { subject }.to change { SolidusPromotions::ShippingRateDiscount.count }
204+
expect { subject }.to change { order.shipments.first.shipping_rates.first.discounts.length }.by(1)
203205
end
204206

205207
context "if the promo is eligible but the calculcator returns 0" do
@@ -209,7 +211,7 @@
209211
it "will not create an adjustment on the shipping rate" do
210212
expect do
211213
subject
212-
end.not_to change { order.shipments.first.shipping_rates.first.discounts.length }
214+
end.not_to change { order.shipments.first.shipping_rates.first.discounts.reject(&:marked_for_destruction?).length }
213215
end
214216
end
215217
end
@@ -224,7 +226,7 @@
224226
expect do
225227
promotion
226228
subject.call
227-
end.to change { order.shipments.first.adjustments.length }
229+
end.to change { order.shipments.first.adjustments.length }.from(0).to(1)
228230
end
229231

230232
context "if the promo is eligible but the calculcator returns 0" do
@@ -235,7 +237,7 @@
235237
expect do
236238
promotion
237239
subject.call
238-
end.not_to change { order.shipments.first.adjustments.length }
240+
end.not_to change { order.shipments.first.adjustments.reject(&:marked_for_destruction?).length }
239241
end
240242
end
241243
end

0 commit comments

Comments
 (0)