From e84e0aebe6f79a17dd0b342345340c59401230df Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:58:07 +0200 Subject: [PATCH 1/3] Fix fatal error in reports helper for orders without payments --- app/helpers/spree/reports_helper.rb | 11 +++++--- .../spree/admin/reports_helper_spec.rb | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 spec/helpers/spree/admin/reports_helper_spec.rb diff --git a/app/helpers/spree/reports_helper.rb b/app/helpers/spree/reports_helper.rb index bc75cfb9e8..3f299c8250 100644 --- a/app/helpers/spree/reports_helper.rb +++ b/app/helpers/spree/reports_helper.rb @@ -11,10 +11,13 @@ module Spree end def report_payment_method_options(orders) - orders.map do |o| - pm = o.payments.first.payment_method - [pm.andand.name, pm.andand.id] - end.uniq + orders.map do |order| + payment_method = order.payments.first.andand.payment_method + + next unless payment_method + + [payment_method.name, payment_method.id] + end.compact.uniq end def report_shipping_method_options(orders) diff --git a/spec/helpers/spree/admin/reports_helper_spec.rb b/spec/helpers/spree/admin/reports_helper_spec.rb new file mode 100644 index 0000000000..9ef4976750 --- /dev/null +++ b/spec/helpers/spree/admin/reports_helper_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' + + +describe Spree::ReportsHelper, type: :helper do + describe "#report_payment_method_options" do + let(:order_with_payments) { create(:order_ready_to_ship) } + let(:order_without_payments) { create(:order_with_line_items) } + let(:orders) { [order_with_payments, order_without_payments] } + let(:payment_method) { order_with_payments.payments.first.payment_method } + + it "returns payment method select options for given orders" do + select_options = helper.report_payment_method_options([order_with_payments]) + + expect(select_options).to eq [[payment_method.name, payment_method.id]] + end + + it "handles orders that don't have payments, without error" do + select_options = helper.report_payment_method_options(orders) + + expect(select_options).to eq [[payment_method.name, payment_method.id]] + end + end +end From e725f33318989a320b15b9154332cacf2d498260 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 11 Jun 2020 12:37:51 +0200 Subject: [PATCH 2/3] Defend from an order w/o payments when building table --- lib/open_food_network/order_cycle_management_report.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/open_food_network/order_cycle_management_report.rb b/lib/open_food_network/order_cycle_management_report.rb index 49fb2e7365..73a0ea34db 100644 --- a/lib/open_food_network/order_cycle_management_report.rb +++ b/lib/open_food_network/order_cycle_management_report.rb @@ -77,7 +77,7 @@ module OpenFoodNetwork ba.phone, order.shipping_method.andand.name, order.payments.first.andand.payment_method.andand.name, - order.payments.first.amount, + order.payments.first.andand.amount, OpenFoodNetwork::UserBalanceCalculator.new(order.email, order.distributor).balance] end @@ -92,7 +92,7 @@ module OpenFoodNetwork sa.phone, order.shipping_method.andand.name, order.payments.first.andand.payment_method.andand.name, - order.payments.first.amount, + order.payments.first.andand.amount, OpenFoodNetwork::UserBalanceCalculator.new(order.email, order.distributor).balance, has_temperature_controlled_items?(order), order.special_instructions] From 29246c15feb2cef918e2d797897a70329decd1e4 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 12 Jun 2020 16:33:00 +0200 Subject: [PATCH 3/3] Defend from order without billing address This a data integrity issue that needs deeper investigation but while this happens, our users can still render their reports. --- lib/open_food_network/order_cycle_management_report.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/open_food_network/order_cycle_management_report.rb b/lib/open_food_network/order_cycle_management_report.rb index 73a0ea34db..e1f35eed6a 100644 --- a/lib/open_food_network/order_cycle_management_report.rb +++ b/lib/open_food_network/order_cycle_management_report.rb @@ -69,12 +69,12 @@ module OpenFoodNetwork def payment_method_row(order) ba = order.billing_address - [ba.firstname, - ba.lastname, + [ba.andand.firstname, + ba.andand.lastname, order.distributor.andand.name, customer_code(order.email), order.email, - ba.phone, + ba.andand.phone, order.shipping_method.andand.name, order.payments.first.andand.payment_method.andand.name, order.payments.first.andand.amount,