From 7c6b731a45beead174d412aecd34a586af98563a Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 24 Mar 2023 17:14:50 +0000 Subject: [PATCH] feat: add ten year validations to startdate too --- app/models/validations/date_validations.rb | 8 ++++++++ config/locales/en.yml | 3 +++ spec/models/validations/date_validations_spec.rb | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 4a0cfe22c..518f0361f 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -46,6 +46,14 @@ module Validations::DateValidations record.errors.add :startdate, I18n.t("validations.setup.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.setup.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.setup.startdate.ten_years_after_mrc_date") + end + location_during_startdate_validation(record, :startdate) scheme_during_startdate_validation(record, :startdate) end diff --git a/config/locales/en.yml b/config/locales/en.yml index cc6b4a5f1..ea8603dc4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -173,6 +173,9 @@ en: after_void_date: "Enter a tenancy start date that is after the void date" after_major_repair_date: "Enter a tenancy start date that is after the major repair date" year_not_two_digits: Tenancy start year must be 2 digits + 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" + location: deactivated: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered." reactivating_soon: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date" diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 426f6007d..f34c655ae 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -232,8 +232,11 @@ RSpec.describe Validations::DateValidations 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.property.mrcdate.ten_years_before_tenancy_start")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.ten_years_after_mrc_date")) end it "must be within 10 years of the tenancy start date" do @@ -241,6 +244,7 @@ RSpec.describe Validations::DateValidations do 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 end context "when reason for vacancy is first let of property" do @@ -299,8 +303,11 @@ RSpec.describe Validations::DateValidations 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.property.void_date.ten_years_before_tenancy_start")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.ten_years_after_void_date")) end it "must be within 10 years of the tenancy start date" do @@ -308,6 +315,7 @@ RSpec.describe Validations::DateValidations do 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 end context "when major repairs have been carried out" do