From 3badaa07d20799b38d29f0309d3d9b922242f9d3 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 19 Aug 2020 12:52:40 +0100 Subject: [PATCH] Fix adjustment calculations; only "eligible" adjustments should be regarded as applied to an order. When an order is submitted and the payment fails, the failed payment's adjustments (payment fees) are set to `eligible: false` to indicate they do not apply. These should not be counted as being included in an order's adjustments. --- app/services/order_adjustments_fetcher.rb | 5 +++-- .../customer_totals_report_spec.rb | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/services/order_adjustments_fetcher.rb b/app/services/order_adjustments_fetcher.rb index 40d78cf6bc..560f04bdb1 100644 --- a/app/services/order_adjustments_fetcher.rb +++ b/app/services/order_adjustments_fetcher.rb @@ -57,11 +57,12 @@ class OrderAdjustmentsFetcher if adjustments_eager_loaded? adjustment_scope = public_send("#{scope}_scope") + # Adjustments are already loaded here, this block is using `Array#select` adjustments.select do |adjustment| - match_by_scope(adjustment, adjustment_scope) + match_by_scope(adjustment, adjustment_scope) && match_by_scope(adjustment, eligible_scope) end else - adjustments.where(nil).public_send scope + adjustments.where(nil).eligible.public_send scope end end diff --git a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb index 990f7e3cb7..4799c3e4ac 100644 --- a/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb +++ b/spec/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report_spec.rb @@ -95,8 +95,7 @@ RSpec.describe OpenFoodNetwork::OrdersAndFulfillmentsReport::CustomerTotalsRepor failed_payment.adjustment.update amount: 456.00, eligible: false, state: "finalized" end - xit "shows the correct payment fee amount for the order" do - # Fails; the sum of adjustments for both failed and complete payments is shown + it "shows the correct payment fee amount for the order" do payment_fee_field = report_table.last[12] expect(payment_fee_field).to eq completed_payment.adjustment.amount end