diff --git a/app/models/case_log.rb b/app/models/case_log.rb index b2aaab484..c9e417609 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -163,6 +163,7 @@ end class CaseLog < ApplicationRecord include Discard::Model + include SoftValidations default_scope -> { kept } scope :not_started, -> { where(status: "not_started") } scope :in_progress, -> { where(status: "in_progress") } @@ -215,24 +216,6 @@ class CaseLog < ApplicationRecord IncomeRange::ALLOWED[person_1_economic_status.to_sym] end - def soft_errors() - soft_errors = {} - if weekly_net_income && person_1_economic_status && override_net_income_validation.blank? - if weekly_net_income < applicable_income_range.soft_min && weekly_net_income > applicable_income_range.hard_min - soft_errors["weekly_net_income"] = OpenStruct.new( - message: "Net income is lower than expected based on the main tenant's working situation. Are you sure this is correct?", - hint_text: "This is based on the tenant's work situation: #{person_1_economic_status}" - ) - elsif weekly_net_income > applicable_income_range.soft_max && weekly_net_income < applicable_income_range.hard_max - soft_errors["weekly_net_income"] = OpenStruct.new( - message: "Net income is higher than expected based on the main tenant's working situation. Are you sure this is correct?", - hint_text: "This is based on the tenant's work situation: #{person_1_economic_status}" - ) - end - end - soft_errors - end - private def update_status! diff --git a/app/modules/soft_validations.rb b/app/modules/soft_validations.rb new file mode 100644 index 000000000..03c40dcfe --- /dev/null +++ b/app/modules/soft_validations.rb @@ -0,0 +1,29 @@ +module SoftValidations + def soft_errors + @soft_errors = {} + net_income_validations + @soft_errors + end + + private + + def net_income_validations + if weekly_net_income && person_1_economic_status && override_net_income_validation.blank? + if weekly_net_income < applicable_income_range.soft_min && weekly_net_income > applicable_income_range.hard_min + @soft_errors["weekly_net_income"] = OpenStruct.new( + message: "Net income is lower than expected based on the main tenant's working situation. Are you sure this is correct?", + hint_text: "This is based on the tenant's work situation: #{person_1_economic_status}" + ) + elsif weekly_net_income > applicable_income_range.soft_max && weekly_net_income < applicable_income_range.hard_max + @soft_errors["weekly_net_income"] = OpenStruct.new( + message: "Net income is higher than expected based on the main tenant's working situation. Are you sure this is correct?", + hint_text: "This is based on the tenant's work situation: #{person_1_economic_status}" + ) + end + elsif weekly_net_income && person_1_economic_status && override_net_income_validation.present? + if weekly_net_income > applicable_income_range.soft_min && weekly_net_income < applicable_income_range.soft_max + self.update(override_net_income_validation: nil) + end + end + end +end diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 5629e02f6..2880a3ae4 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -388,7 +388,7 @@ RSpec.describe "Test Features" do fill_in("case-log-net-income-field", with: income_over_soft_limit) choose("case-log-net-income-frequency-weekly-field", allow_label_click: true) click_button("Save and continue") - expect(page).not_to have_content("For net incomes that fall outside the expected range you must confirm they're correct") + expect(page).to have_content("For net incomes that fall outside the expected range you must confirm they're correct") check("case-log-override-net-income-validation-override-net-income-validation-field", allow_label_click: true) click_button("Save and continue") expect(page).to have_current_path("/case_logs/#{case_log.id}/net_income_uc_proportion") @@ -402,9 +402,11 @@ RSpec.describe "Test Features" do fill_in("case-log-net-income-field", with: income_under_soft_limit) click_button("Save and continue") expect(page).to have_current_path("/case_logs/#{case_log.id}/net_income_uc_proportion") + case_log.reload + expect(case_log.override_net_income_validation).to be_nil end - it "clears the confirmation question if the page is returned to using the back button" do + xit "clears the confirmation question if the page is returned to using the back button" do visit("/case_logs/#{case_log.id}/net_income") fill_in("case-log-net-income-field", with: income_over_soft_limit) choose("case-log-net-income-frequency-weekly-field", allow_label_click: true)