Browse Source

CLDC-3249 Clear invalidated earnings values (#2242)

* Clear invalidated earnings values

* Only run the earnings validation for 2023 onwards
pull/2244/head
kosiakkatrina 10 months ago committed by GitHub
parent
commit
d6eb053fd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      app/models/validations/financial_validations.rb
  2. 11
      lib/tasks/clear_invalidated_earnings.rake
  3. 110
      spec/lib/tasks/clear_invalidated_earnings_spec.rb
  4. 21
      spec/models/validations/financial_validations_spec.rb

2
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)

11
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

110
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

21
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

Loading…
Cancel
Save