diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 274a6da52..2426ecc0e 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -180,6 +180,10 @@ class CaseLog < ApplicationRecord ALLOWED_INCOME_RANGES[ecstat1.to_sym] end + def first_time_property_let_as_social_housing? + first_time_property_let_as_social_housing == "Yes" + end + private PIO = Postcodes::IO.new diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index d520b6dae..828bceb81 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -2,6 +2,7 @@ module Validations::PropertyValidations # Validations methods need to be called 'validate_' to run on model save # or 'validate_' to run on submit as well include Constants::CaseLog + def validate_property_number_of_times_relet(record) if record.offered && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.offered.to_s) record.errors.add :offered, "Property number of times relet must be between 0 and 20" @@ -14,9 +15,22 @@ module Validations::PropertyValidations end end + FIRST_LET_VACANCY_REASONS = ["First let of newbuild property", + "First let of conversion/rehabilitation/acquired property", + "First let of leased property"].freeze + def validate_rsnvac(record) + if !record.first_time_property_let_as_social_housing? && FIRST_LET_VACANCY_REASONS.include?(record.rsnvac) + record.errors.add :rsnvac, "Reason for vacancy cannot be first let if unit has been previously let as social housing" + end + + if record.first_time_property_let_as_social_housing? && record.rsnvac.present? && !FIRST_LET_VACANCY_REASONS.include?(record.rsnvac) + record.errors.add :rsnvac, "Reason for vacancy must be first let if unit has been previously let as social housing" + end + end + def validate_unitletas(record) - if record.unitletas.present? && (record.rsnvac == "First let of newbuild property" || record.rsnvac == "First let of conversion/rehabilitation/acquired property" || record.rsnvac == "First let of leased property") - record.errors.add :unitletas, "Can not be completed if it is the first let of the property" + if record.first_time_property_let_as_social_housing? && record.unitletas.present? + record.errors.add :unitletas, "Property cannot have a previous let type if it is being let as social housing for the first time" end end end diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 4a78c4cf5..4aa72e261 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -162,19 +162,4 @@ RSpec.describe "validations" do end end end - - describe "Property Validations" do - context "first let of property and reason for vacancy completed" do - let(:rsnvac) { "First let of newbuild property" } - let(:rsnvac1) { "First let of conversion/rehabilitation/acquired property" } - let(:rsnvac2) { "First let of leased property" } - let(:unitletas) { "Social rent basis" } - - it "throws a validation error", js: true do - expect { case_log.update!(rsnvac: rsnvac, unitletas: unitletas) }.to raise_error(ActiveRecord::RecordInvalid) - expect { case_log.update!(rsnvac: rsnvac1, unitletas: unitletas) }.to raise_error(ActiveRecord::RecordInvalid) - expect { case_log.update!(rsnvac: rsnvac2, unitletas: unitletas) }.to raise_error(ActiveRecord::RecordInvalid) - end - end - end end diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index c7cc65314..4298aad27 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -139,7 +139,7 @@ "ppostc1": "w3", "ppostc2": "w3", "why_dont_you_know_la": "Forgot", - "first_time_property_let_as_social_housing": "Yes", + "first_time_property_let_as_social_housing": "No", "unitletas": "Affordable rent basis", "builtype": "Purpose built", "property_wheelchair_accessible": "Yes", diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index ae3d82b80..90056cc98 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -253,6 +253,86 @@ RSpec.describe Form, type: :model do end end + context "Property vacancy and let as validations" do + it "cannot have a previously let as type, if it hasn't been let before" do + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "No", + unitletas: "Social rent basis", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.not_to raise_error + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + unitletas: "Social rent basis", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.to raise_error(ActiveRecord::RecordInvalid) + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + unitletas: "Affordable rent basis", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.to raise_error(ActiveRecord::RecordInvalid) + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + unitletas: "Intermediate rent basis", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.to raise_error(ActiveRecord::RecordInvalid) + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + unitletas: "Do not know", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must have a first let reason for vacancy if it's being let as social housing for the first time" do + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + rsnvac: "First let of newbuild property", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.not_to raise_error + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + rsnvac: "First let of conversion/rehabilitation/acquired property", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.not_to raise_error + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + rsnvac: "First let of leased property", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.not_to raise_error + expect { + CaseLog.create!( + first_time_property_let_as_social_housing: "Yes", + rsnvac: "Tenant moved to care home", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.to raise_error(ActiveRecord::RecordInvalid) + end + end + context "Shared accomodation bedrooms validation" do it "you must have more than zero bedrooms" do expect {