From d0ad7494299e847811a26e2a5c95a41d66ddeeaa Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 15 Feb 2024 11:09:22 +0000 Subject: [PATCH] Only run the earnings validation for 2023 onwards --- .../validations/financial_validations.rb | 2 +- lib/tasks/clear_invalidated_earnings.rake | 8 ---- .../tasks/clear_invalidated_earnings_spec.rb | 43 ++++++++++++++++--- .../validations/financial_validations_spec.rb | 21 +++++++++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index a1d799624..890c28284 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -24,7 +24,7 @@ module Validations::FinancialValidations end def validate_net_income(record) - if record.ecstat1 && record.hhmemb && record.weekly_net_income + if record.ecstat1 && record.hhmemb && record.weekly_net_income && record.startdate && record.form.start_date.year >= 2023 if record.weekly_net_income > record.applicable_income_range.hard_max frequency = record.form.get_question("incfreq", record).label_from_value(record.incfreq).downcase hard_max = format_as_currency(record.applicable_income_range.hard_max) diff --git a/lib/tasks/clear_invalidated_earnings.rake b/lib/tasks/clear_invalidated_earnings.rake index 0be55bd71..74dbd1aca 100644 --- a/lib/tasks/clear_invalidated_earnings.rake +++ b/lib/tasks/clear_invalidated_earnings.rake @@ -8,12 +8,4 @@ task clear_invalidated_earnings: :environment do lettings_log.save!(validate: false) end end - LettingsLog.filter_by_year(2022).find_each do |lettings_log| - lettings_log.validate_net_income(lettings_log) - if lettings_log.errors[:earnings].present? - lettings_log.earnings = nil - lettings_log.incfreq = nil - lettings_log.save!(validate: false, touch: false) - end - end end diff --git a/spec/lib/tasks/clear_invalidated_earnings_spec.rb b/spec/lib/tasks/clear_invalidated_earnings_spec.rb index b02f7561a..19e5d8f88 100644 --- a/spec/lib/tasks/clear_invalidated_earnings_spec.rb +++ b/spec/lib/tasks/clear_invalidated_earnings_spec.rb @@ -15,7 +15,7 @@ RSpec.describe "clear_invalidated_earnings" do context "when the rake task is run" do context "and there are 2023 logs with invalid earnings" do let(:user) { create(:user) } - let!(:lettings_log) { create(:lettings_log, created_by: user) } + let!(:lettings_log) { create(:lettings_log, :completed, created_by: user, voiddate: nil, mrcdate: nil) } before do lettings_log.startdate = Time.zone.local(2023, 4, 4) @@ -44,9 +44,40 @@ RSpec.describe "clear_invalidated_earnings" do end end - context "and there are 2022 logs with invalid earnings" do + context "and there are valid 2023 logs" do let(:user) { create(:user) } - let!(:lettings_log) { create(:lettings_log, created_by: user) } + let!(:lettings_log) { create(:lettings_log, :completed, created_by: user, voiddate: nil, mrcdate: nil) } + + before do + lettings_log.startdate = Time.zone.local(2023, 4, 4) + lettings_log.incfreq = 1 + lettings_log.earnings = 95 + lettings_log.hhmemb = 1 + lettings_log.ecstat1 = 1 + lettings_log.save! + end + + it "does not update the logs" do + initial_updated_at = lettings_log.updated_at + expect(lettings_log.incfreq).to eq(1) + expect(lettings_log.earnings).to eq(95) + expect(lettings_log.hhmemb).to eq(1) + expect(lettings_log.ecstat1).to eq(1) + + task.invoke + lettings_log.reload + + expect(lettings_log.incfreq).to eq(1) + expect(lettings_log.earnings).to eq(95) + expect(lettings_log.hhmemb).to eq(1) + expect(lettings_log.ecstat1).to eq(1) + expect(lettings_log.updated_at).to eq(initial_updated_at) + end + end + + context "and there are 2022 logs" do + let(:user) { create(:user) } + let!(:lettings_log) { create(:lettings_log, :completed, created_by: user, voiddate: nil, mrcdate: nil) } before do lettings_log.startdate = Time.zone.local(2022, 4, 4) @@ -57,7 +88,7 @@ RSpec.describe "clear_invalidated_earnings" do lettings_log.save!(validate: false) end - it "does not export the log" do + it "does not update the logs" do initial_updated_at = lettings_log.updated_at expect(lettings_log.incfreq).to eq(1) expect(lettings_log.earnings).to eq(20) @@ -67,8 +98,8 @@ RSpec.describe "clear_invalidated_earnings" do task.invoke lettings_log.reload - expect(lettings_log.incfreq).to eq(nil) - expect(lettings_log.earnings).to eq(nil) + expect(lettings_log.incfreq).to eq(1) + expect(lettings_log.earnings).to eq(20) expect(lettings_log.hhmemb).to eq(1) expect(lettings_log.ecstat1).to eq(1) expect(lettings_log.updated_at).to eq(initial_updated_at) diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 1a6672e08..c68a3b7b2 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -188,6 +188,7 @@ RSpec.describe Validations::FinancialValidations do describe "net income validations" do it "validates that the net income is within the expected range for the household’s employment status" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 200 record.incfreq = 1 record.hhmemb = 1 @@ -198,6 +199,7 @@ RSpec.describe Validations::FinancialValidations do context "when the net income is higher than the hard max for their employment status" do it "adds an error" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 5000 record.incfreq = 1 record.hhmemb = 1 @@ -214,6 +216,7 @@ RSpec.describe Validations::FinancialValidations do context "when the net income is lower than the hard min for their employment status" do it "adds an error" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 50 record.incfreq = 1 record.hhmemb = 1 @@ -230,6 +233,7 @@ RSpec.describe Validations::FinancialValidations do context "when there is more than one household member" do it "allows income levels based on all working situations combined" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 5000 record.incfreq = 1 record.hhmemb = 4 @@ -242,6 +246,7 @@ RSpec.describe Validations::FinancialValidations do end it "uses the combined value in error messages" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 100 record.incfreq = 1 record.hhmemb = 3 @@ -254,6 +259,7 @@ RSpec.describe Validations::FinancialValidations do end it "adds errors to relevant fields for each tenant when income is too high" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 5000 record.incfreq = 1 record.hhmemb = 3 @@ -277,6 +283,7 @@ RSpec.describe Validations::FinancialValidations do end it "adds errors to relevant fields for each tenant when income is too low" do + record.startdate = Time.zone.local(2023, 5, 1) record.earnings = 50 record.incfreq = 1 record.hhmemb = 3 @@ -293,6 +300,20 @@ RSpec.describe Validations::FinancialValidations do expect(record.errors["ecstat#{n}"]).to be_empty end end + + context "when the net income is lower than the hard min for their employment status for 22/23 collection" do + it "does not add an error" do + record.startdate = Time.zone.local(2022, 5, 1) + record.earnings = 50 + record.incfreq = 1 + record.hhmemb = 1 + record.ecstat1 = 1 + financial_validator.validate_net_income(record) + expect(record.errors["earnings"]).to be_empty + expect(record.errors["ecstat1"]).to be_empty + expect(record.errors["hhmemb"]).to be_empty + end + end end end