From d84bd800a3e73a0e8678199492a55b3fc9cb59c7 Mon Sep 17 00:00:00 2001 From: cintamani Date: Wed, 30 Oct 2019 13:13:09 +0000 Subject: [PATCH 1/2] Add method to access amount paid by user From: https://eaflood.atlassian.net/browse/RUBY-711 Add method to registration objects to access the total amount paid by a user for a rergistration --- .../can_have_registration_attributes.rb | 6 ++++++ .../can_have_registration_attributes.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/models/concerns/waste_carriers_engine/can_have_registration_attributes.rb b/app/models/concerns/waste_carriers_engine/can_have_registration_attributes.rb index fe196202e..19e10a04c 100644 --- a/app/models/concerns/waste_carriers_engine/can_have_registration_attributes.rb +++ b/app/models/concerns/waste_carriers_engine/can_have_registration_attributes.rb @@ -163,6 +163,12 @@ def unpaid_balance? false end + + def amount_paid + (finance_details.presence&.payments || []).inject(0) do |tot, payment| + tot + payment.amount + end + end end # rubocop:enable Metrics/BlockLength end diff --git a/spec/support/shared_examples/can_have_registration_attributes.rb b/spec/support/shared_examples/can_have_registration_attributes.rb index e2beb8e5e..e42c2c857 100644 --- a/spec/support/shared_examples/can_have_registration_attributes.rb +++ b/spec/support/shared_examples/can_have_registration_attributes.rb @@ -28,6 +28,24 @@ end end + describe "#amount_paid" do + it "returns the total amount paid by the user" do + finance_detail_1 = double(:finance_detail_1, amount: 23) + finance_detail_2 = double(:finance_detail_2, amount: 30) + finance_details = double(:finance_details, payments: [finance_detail_1, finance_detail_2]) + + expect(resource).to receive(:finance_details).and_return(finance_details) + expect(resource.amount_paid).to eq(53) + end + + context "when there are no finance details" do + it "return 0" do + expect(resource).to receive(:finance_details) + expect(resource.amount_paid).to eq(0) + end + end + end + describe "#contact_address" do let(:contact_address) { build(:address, :contact) } let(:resource) { build(factory, addresses: [contact_address]) } From 27e51f506702642d12f1a02e4723b4bb482baced Mon Sep 17 00:00:00 2001 From: cintamani Date: Wed, 30 Oct 2019 13:22:20 +0000 Subject: [PATCH 2/2] Apply suggestions from code review Fix rubocop --- .../shared_examples/can_have_registration_attributes.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/support/shared_examples/can_have_registration_attributes.rb b/spec/support/shared_examples/can_have_registration_attributes.rb index e42c2c857..b23155312 100644 --- a/spec/support/shared_examples/can_have_registration_attributes.rb +++ b/spec/support/shared_examples/can_have_registration_attributes.rb @@ -30,9 +30,9 @@ describe "#amount_paid" do it "returns the total amount paid by the user" do - finance_detail_1 = double(:finance_detail_1, amount: 23) - finance_detail_2 = double(:finance_detail_2, amount: 30) - finance_details = double(:finance_details, payments: [finance_detail_1, finance_detail_2]) + finance_detail1 = double(:finance_detail1, amount: 23) + finance_detail2 = double(:finance_detail2, amount: 30) + finance_details = double(:finance_details, payments: [finance_detail1, finance_detail2]) expect(resource).to receive(:finance_details).and_return(finance_details) expect(resource.amount_paid).to eq(53)