diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 910ec5adb..5a4342128 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -43,14 +43,26 @@ class CaseLogValidator < ActiveModel::Validator return unless record.tenant_economic_status && record.weekly_net_income applicable_income_range = IncomeRange.find_by(economic_status: record.tenant_economic_status) + if record.weekly_net_income > applicable_income_range.hard_max record.errors.add :net_income, "Net income cannot be greater than #{applicable_income_range.hard_max} given the tenant's working situation" end + if record.weekly_net_income < applicable_income_range.hard_max record.errors.add :net_income, "Net income cannot be less than #{applicable_income_range.hard_min} given the tenant's working situation" end end + def validate_armed_forces_injured(record) + if (record.armed_forces == "Yes - a regular" || record.armed_forces == "Yes - a reserve") && record.armed_forces_injured.blank? + record.errors.add :armed_forces_injured, "You must answer the armed forces injury question if the tenant has served in the armed forces" + end + + if (record.armed_forces == "No" || record.armed_forces == "Prefer not to say") && record.armed_forces_injured.present? + record.errors.add :armed_forces_injured, "You must not answer the armed forces injury question if the tenant has not served in the armed forces or prefer not to say was chosen" + end + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload @@ -114,8 +126,6 @@ class CaseLog < ApplicationRecord ((net_income * 12) / 52.0).round(0) when "Yearly" (net_income / 12.0).round(0) - else - nil end end diff --git a/db/seeds.rb b/db/seeds.rb index 28a19fad3..7267776e0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -11,14 +11,14 @@ income_ranges = [ { economic_status: "Full-time - 30 hours or more", soft_min: 143, soft_max: 730, hard_min: 90, hard_max: 1230 }, { economic_status: "Part-time - Less than 30 hours", soft_min: 67, soft_max: 620, hard_min: 50, hard_max: 950 }, { economic_status: "In government training into work, such as New Deal", soft_min: 80, soft_max: 480, hard_min: 40, hard_max: 990 }, - { economic_status: "Jobseeker", soft_min: 50, soft_max: 370, hard_min: 10, hard_max: 450 }, - { economic_status: "Retired", soft_min: 50, soft_max: 380, hard_min: 10, hard_max: 690 }, - { economic_status: "Not seeking work", soft_min: 53, soft_max: 540, hard_min: 10, hard_max: 890 }, - { economic_status: "Full-time student", soft_min: 47, soft_max: 460, hard_min: 10, hard_max: 1300 }, + { economic_status: "Jobseeker", soft_min: 50, soft_max: 370, hard_min: 10, hard_max: 450 }, + { economic_status: "Retired", soft_min: 50, soft_max: 380, hard_min: 10, hard_max: 690 }, + { economic_status: "Not seeking work", soft_min: 53, soft_max: 540, hard_min: 10, hard_max: 890 }, + { economic_status: "Full-time student", soft_min: 47, soft_max: 460, hard_min: 10, hard_max: 1300 }, { economic_status: "Unable to work because of long term sick or disability", soft_min: 54, soft_max: 460, hard_min: 10, hard_max: 820 }, - { economic_status: "Child under 16", soft_min: 50, soft_max: 450, hard_min: 10, hard_max: 750 }, - { economic_status: "Other", soft_min: 50, soft_max: 580, hard_min: 10, hard_max: 1040 }, - { economic_status: "Prefer not to say", soft_min: 47, soft_max: 730, hard_min: 10, hard_max: 1300 }, + { economic_status: "Child under 16", soft_min: 50, soft_max: 450, hard_min: 10, hard_max: 750 }, + { economic_status: "Other", soft_min: 50, soft_max: 580, hard_min: 10, hard_max: 1040 }, + { economic_status: "Prefer not to say", soft_min: 47, soft_max: 730, hard_min: 10, hard_max: 1300 }, ] income_ranges.each do |income_range| diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 120c7947c..3967bc221 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -11,7 +11,7 @@ RSpec.describe CheckAnswersHelper do ) end let(:case_log_with_met_radio_condition) do - FactoryBot.create(:case_log, armed_forces: "Yes - a regular") + FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No") end let(:subsection) { "income_and_benefits" } let(:subsection_with_numeric_conditionals) { "household_characteristics" } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index b51ec37f7..bb217fa75 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -27,15 +27,16 @@ RSpec.describe Form, type: :model do end context "income ranges" do - let!(:income_range){ FactoryBot.create(:income_range, :full_time) } + let!(:income_range) { FactoryBot.create(:income_range, :full_time) } it "validates net income maximum" do expect { CaseLog.create!( tenant_economic_status: "Full-time - 30 hours or more", net_income: 5000, - net_income_frequency: "Weekly" - ) }.to raise_error(ActiveRecord::RecordInvalid) + net_income_frequency: "Weekly", + ) + }.to raise_error(ActiveRecord::RecordInvalid) end it "validates net income minimum" do @@ -43,12 +44,13 @@ RSpec.describe Form, type: :model do CaseLog.create!( tenant_economic_status: "Full-time - 30 hours or more", net_income: 1, - net_income_frequency: "Weekly" - ) }.to raise_error(ActiveRecord::RecordInvalid) + net_income_frequency: "Weekly", + ) + }.to raise_error(ActiveRecord::RecordInvalid) end end - describe "reasonable preference validation" do + context "reasonable preference validation" do it "if given reasonable preference is yes a reason must be selected" do expect { CaseLog.create!(reasonable_preference: "Yes", @@ -79,6 +81,7 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + context "other reason for leaving last settled home validation" do it "must be provided if main reason for leaving last settled home was given as other" do expect { @@ -94,6 +97,22 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "armed forces injured validation" do + it "must be anwered if tenant was a regular or reserve in armed forces" do + expect { + CaseLog.create!(armed_forces: "Yes - a regular", + armed_forces_injured: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must be anwered if tenant was not a regular or reserve in armed forces" do + expect { + CaseLog.create!(armed_forces: "No", + armed_forces_injured: "Yes") + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do