Browse Source

Only run the earnings validation for 2023 onwards

pull/2242/head
Kat 2 years ago
parent
commit
d0ad749429
  1. 2
      app/models/validations/financial_validations.rb
  2. 8
      lib/tasks/clear_invalidated_earnings.rake
  3. 43
      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)

8
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

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

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