From b2dfc9b494d6981c29006e1c8cc7505348636c79 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 25 Oct 2021 10:25:53 +0100 Subject: [PATCH] Add validate_net_income_uc_proportion validation/ --- app/models/case_log.rb | 17 +++++++++++++++++ spec/models/case_log_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 1f8743b9f..4e64f3144 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -49,6 +49,15 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_net_income_uc_proportion(record) + if ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].include?(record.tenant_economic_status) && record.net_income_uc_proportion == "All" + record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if person works part or full time" + end + check_partner_net_income_uc_proportion(record.person_2_economic_status, record.person_2_relationship, record) + check_partner_net_income_uc_proportion(record.person_3_economic_status, record.person_3_relationship, record) + check_partner_net_income_uc_proportion(record.person_4_economic_status, record.person_4_relationship, record) + 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 @@ -65,6 +74,14 @@ class CaseLogValidator < ActiveModel::Validator validation_methods.each { |meth| public_send(meth, record) } end end + +private + + def check_partner_net_income_uc_proportion(person_field, relationship_field, record) + if ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].include?(person_field) && relationship_field == "Partner" && record.net_income_uc_proportion == "All" + record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if the partner works part or full time" + end + end end class CaseLog < ApplicationRecord diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 927b153d1..ae2dc4308 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -89,6 +89,26 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "tenant’s income is from Universal Credit, state pensions or benefits" do + it "Cannot be All if person 1 works full time" do + expect { + CaseLog.create!(net_income_uc_proportion: "All", tenant_economic_status: "Full-time - 30 hours or more") + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "Cannot be All if person 1 works part time" do + expect { + CaseLog.create!(net_income_uc_proportion: "All", tenant_economic_status: "Part-time - Less than 30 hours") + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "Cannot be 1 All if any of persons 2-4 are person 1's partner and work part or full time" do + expect { + CaseLog.create!(net_income_uc_proportion: "All", person_2_relationship: "Partner", person_2_economic_status: "Part-time - Less than 30 hours") + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do