From 22653022911d12f85b257065623350dee6cd1f2d Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:22:30 +0000 Subject: [PATCH] Update date validations for 2025 --- app/models/validations/date_validations.rb | 46 ++++++-- .../locales/validations/lettings/date.en.yml | 4 + .../validations/date_validations_spec.rb | 106 +++++++++++++----- 3 files changed, 116 insertions(+), 40 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 7462290b6..b4d3bf10d 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -11,14 +11,26 @@ module Validations::DateValidations record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.not_first_let") end - if record["mrcdate"].present? && record["startdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 3650 + return unless record["mrcdate"].present? && record["startdate"].present? + + if record.form.start_year_2025_or_later? + if record["startdate"].to_date - record["mrcdate"].to_date > 7300 + record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.twenty_years_before_tenancy_start") + end + elsif record["startdate"].to_date - record["mrcdate"].to_date > 3650 record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start") end end def validate_property_void_date(record) - if record["voiddate"].present? && record["startdate"].present? && record["startdate"].to_date - record["voiddate"].to_date > 3650 - record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start") + if record["voiddate"].present? && record["startdate"].present? + if record.form.start_year_2025_or_later? + if record["startdate"].to_date - record["voiddate"].to_date > 7300 + record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.twenty_years_before_tenancy_start") + end + elsif record["startdate"].to_date - record["voiddate"].to_date > 3650 + record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start") + end end if record["voiddate"].present? && record["startdate"].present? && record["startdate"].to_date < record["voiddate"].to_date @@ -42,13 +54,7 @@ module Validations::DateValidations record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.after_major_repair_date") end - if record["voiddate"].present? && record["startdate"].to_date - record["voiddate"].to_date > 3650 - record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_void_date") - end - - if record["mrcdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 3650 - record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date") - end + validate_startdate_against_mrc_and_void_dates(record) end private @@ -56,4 +62,24 @@ private def is_rsnvac_first_let?(record) [15, 16, 17].include?(record["rsnvac"]) end + + def validate_startdate_against_mrc_and_void_dates(record) + if record.form.start_year_2025_or_later? + if record["voiddate"].present? && record["startdate"].to_date - record["voiddate"].to_date > 7300 + record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.twenty_years_after_void_date") + end + + if record["mrcdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 7300 + record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.twenty_years_after_mrc_date") + end + else + if record["voiddate"].present? && record["startdate"].to_date - record["voiddate"].to_date > 3650 + record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_void_date") + end + + if record["mrcdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 3650 + record.errors.add :startdate, I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date") + end + end + end end diff --git a/config/locales/validations/lettings/date.en.yml b/config/locales/validations/lettings/date.en.yml index 54c53996f..1e46dba7b 100644 --- a/config/locales/validations/lettings/date.en.yml +++ b/config/locales/validations/lettings/date.en.yml @@ -7,14 +7,18 @@ en: after_major_repair_date: "Enter a tenancy start date that is after the major repair date." ten_years_after_void_date: "Enter a tenancy start date that is no more than 10 years after the void date." ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date." + twenty_years_after_void_date: "Enter a tenancy start date that is no more than 20 years after the void date." + twenty_years_after_mrc_date: "Enter a tenancy start date that is no more than 20 years after the major repairs completion date." mrcdate: before_tenancy_start: "Enter a major repairs date that is before the tenancy start date." not_first_let: "Major repairs date must not be completed if the tenancy is a first let." ten_years_before_tenancy_start: "Enter a major repairs completion date that is no more than 10 years before the tenancy start date." + twenty_years_before_tenancy_start: "Enter a major repairs completion date that is no more than 20 years before the tenancy start date." before_void_date: "Major repairs date must be after the void date if provided." void_date: ten_years_before_tenancy_start: "Enter a void date no more than 10 years before the tenancy start date." + twenty_years_before_tenancy_start: "Enter a void date no more than 20 years before the tenancy start date." before_tenancy_start: "Enter a void date that is before the tenancy start date." after_mrcdate: "Void date must be before the major repairs date if provided." diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 3ca259b16..65d3139bd 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -68,23 +68,46 @@ RSpec.describe Validations::DateValidations do expect(record.errors["mrcdate"]).to be_empty end - it "cannot be more than 10 years before the tenancy start date" do - record.startdate = Time.zone.local(2022, 2, 1) - record.mrcdate = Time.zone.local(2012, 1, 1) - date_validator.validate_property_major_repairs(record) - date_validator.validate_startdate(record) - expect(record.errors["mrcdate"]) - .to include(match I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start")) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date")) + context "with 2024 logs or earlier" do + it "cannot be more than 10 years before the tenancy start date" do + record.startdate = Time.zone.local(2024, 2, 1) + record.mrcdate = Time.zone.local(2014, 1, 1) + date_validator.validate_property_major_repairs(record) + date_validator.validate_startdate(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_mrc_date")) + end + + it "must be within 10 years of the tenancy start date" do + record.startdate = Time.zone.local(2024, 2, 1) + record.mrcdate = Time.zone.local(2014, 3, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]).to be_empty + expect(record.errors["startdate"]).to be_empty + end end - it "must be within 10 years of the tenancy start date" do - record.startdate = Time.zone.local(2022, 2, 1) - record.mrcdate = Time.zone.local(2012, 3, 1) - date_validator.validate_property_major_repairs(record) - expect(record.errors["mrcdate"]).to be_empty - expect(record.errors["startdate"]).to be_empty + context "with 2025 logs or later" do + it "cannot be more than 20 years before the tenancy start date" do + record.startdate = Time.zone.local(2026, 2, 1) + record.mrcdate = Time.zone.local(2006, 1, 1) + date_validator.validate_property_major_repairs(record) + date_validator.validate_startdate(record) + expect(record.errors["mrcdate"]) + .to include(match I18n.t("validations.lettings.date.mrcdate.twenty_years_before_tenancy_start")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.lettings.date.startdate.twenty_years_after_mrc_date")) + end + + it "must be within 20 years of the tenancy start date" do + record.startdate = Time.zone.local(2026, 2, 1) + record.mrcdate = Time.zone.local(2006, 3, 1) + date_validator.validate_property_major_repairs(record) + expect(record.errors["mrcdate"]).to be_empty + expect(record.errors["startdate"]).to be_empty + end end context "when reason for vacancy is first let of property" do @@ -139,23 +162,46 @@ RSpec.describe Validations::DateValidations do expect(record.errors["voiddate"]).to be_empty end - it "cannot be more than 10 years before the tenancy start date" do - record.startdate = Time.zone.local(2022, 2, 1) - record.voiddate = Time.zone.local(2012, 1, 1) - date_validator.validate_property_void_date(record) - date_validator.validate_startdate(record) - expect(record.errors["voiddate"]) - .to include(match I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start")) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_void_date")) + context "with 2024 logs or earlier" do + it "cannot be more than 10 years before the tenancy start date" do + record.startdate = Time.zone.local(2024, 2, 1) + record.voiddate = Time.zone.local(2014, 1, 1) + date_validator.validate_property_void_date(record) + date_validator.validate_startdate(record) + expect(record.errors["voiddate"]) + .to include(match I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.lettings.date.startdate.ten_years_after_void_date")) + end + + it "must be within 10 years of the tenancy start date" do + record.startdate = Time.zone.local(2024, 2, 1) + record.voiddate = Time.zone.local(2014, 3, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["voiddate"]).to be_empty + expect(record.errors["startdate"]).to be_empty + end end - it "must be within 10 years of the tenancy start date" do - record.startdate = Time.zone.local(2022, 2, 1) - record.voiddate = Time.zone.local(2012, 3, 1) - date_validator.validate_property_void_date(record) - expect(record.errors["voiddate"]).to be_empty - expect(record.errors["startdate"]).to be_empty + context "with 2025 logs or later" do + it "cannot be more than 20 years before the tenancy start date" do + record.startdate = Time.zone.local(2026, 2, 1) + record.voiddate = Time.zone.local(2006, 1, 1) + date_validator.validate_property_void_date(record) + date_validator.validate_startdate(record) + expect(record.errors["voiddate"]) + .to include(match I18n.t("validations.lettings.date.void_date.twenty_years_before_tenancy_start")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.lettings.date.startdate.twenty_years_after_void_date")) + end + + it "must be within 20 years of the tenancy start date" do + record.startdate = Time.zone.local(2026, 2, 1) + record.voiddate = Time.zone.local(2006, 3, 1) + date_validator.validate_property_void_date(record) + expect(record.errors["voiddate"]).to be_empty + expect(record.errors["startdate"]).to be_empty + end end context "when major repairs have been carried out" do