From d6eb053fd9332f211bf92d964e1f5afa86d35d3e Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 15 Feb 2024 11:27:02 +0000 Subject: [PATCH] CLDC-3249 Clear invalidated earnings values (#2242) * Clear invalidated earnings values * Only run the earnings validation for 2023 onwards --- .../validations/financial_validations.rb | 2 +- lib/tasks/clear_invalidated_earnings.rake | 11 ++ .../tasks/clear_invalidated_earnings_spec.rb | 110 ++++++++++++++++++ .../validations/financial_validations_spec.rb | 21 ++++ 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/clear_invalidated_earnings.rake create mode 100644 spec/lib/tasks/clear_invalidated_earnings_spec.rb 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 new file mode 100644 index 000000000..74dbd1aca --- /dev/null +++ b/lib/tasks/clear_invalidated_earnings.rake @@ -0,0 +1,11 @@ +desc "Clear earnings for lettings logs that fail validation" +task clear_invalidated_earnings: :environment do + LettingsLog.filter_by_year(2023).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) + end + end +end diff --git a/spec/lib/tasks/clear_invalidated_earnings_spec.rb b/spec/lib/tasks/clear_invalidated_earnings_spec.rb new file mode 100644 index 000000000..19e5d8f88 --- /dev/null +++ b/spec/lib/tasks/clear_invalidated_earnings_spec.rb @@ -0,0 +1,110 @@ +require "rails_helper" +require "rake" + +RSpec.describe "clear_invalidated_earnings" do + describe ":clear_invalidated_earnings", type: :task do + subject(:task) { Rake::Task["clear_invalidated_earnings"] } + + before do + Rake.application.rake_require("tasks/clear_invalidated_earnings") + Rake::Task.define_task(:environment) + task.reenable + FormHandler.instance.use_real_forms! + end + + 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, :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 = 20 + lettings_log.hhmemb = 1 + lettings_log.ecstat1 = 1 + lettings_log.save!(validate: false) + end + + it "clears earnings" do + initial_updated_at = lettings_log.updated_at + 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) + + task.invoke + lettings_log.reload + + expect(lettings_log.incfreq).to eq(nil) + expect(lettings_log.earnings).to eq(nil) + expect(lettings_log.hhmemb).to eq(1) + expect(lettings_log.ecstat1).to eq(1) + expect(lettings_log.updated_at).not_to eq(initial_updated_at) + end + end + + context "and there are valid 2023 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(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) + lettings_log.incfreq = 1 + lettings_log.earnings = 20 + lettings_log.hhmemb = 1 + lettings_log.ecstat1 = 1 + lettings_log.save!(validate: false) + 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(20) + 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(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) + end + end + end + end +end 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