From d7f650a283c648dd81f5999c66c4e080e5aeebb9 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Fri, 14 Oct 2022 09:52:52 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20add=20validation=20for=20voiddate=20and?= =?UTF-8?q?=20mrcdate=20when=20startdate=20changed=20=E2=80=A6=20(#942)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add validation for voiddate and mrcdate when startdate changed after these have been added later * test: add tests for new validations * refactor: remove unnecessary parens --- app/models/validations/date_validations.rb | 12 ++++++++++-- config/locales/en.yml | 2 ++ spec/models/validations/date_validations_spec.rb | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 18267ffdb..0353abd26 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -41,16 +41,24 @@ module Validations::DateValidations record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") end - if FeatureToggle.startdate_two_week_validation_enabled? && (record.startdate > Time.zone.today + 14) + if FeatureToggle.startdate_two_week_validation_enabled? && record.startdate > Time.zone.today + 14 record.errors.add :startdate, I18n.t("validations.setup.startdate.later_than_14_days_after") end if record.scheme_id.present? scheme_end_date = record.scheme.end_date - if scheme_end_date.present? && (record.startdate > scheme_end_date) + if scheme_end_date.present? && record.startdate > scheme_end_date record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date") end end + + if record["voiddate"].present? && record.startdate < record["voiddate"] + record.errors.add :startdate, I18n.t("validations.setup.startdate.after_void_date") + end + + if record["mrcdate"].present? && record.startdate < record["mrcdate"] + record.errors.add :startdate, I18n.t("validations.setup.startdate.after_major_repair_date") + end end private diff --git a/config/locales/en.yml b/config/locales/en.yml index ef58f0cae..8b9a12286 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -118,6 +118,8 @@ en: startdate: later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme" + 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" property: mrcdate: diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 35ff79b99..0b20ab07a 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -41,6 +41,22 @@ RSpec.describe Validations::DateValidations do .to include(match I18n.t("validations.setup.startdate.before_scheme_end_date")) end + it "validates that the tenancy start date is after the void date if it has a void date" do + record.startdate = Time.zone.local(2022, 1, 1) + record.voiddate = Time.zone.local(2022, 2, 1) + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.after_void_date")) + end + + it "validates that the tenancy start date is after the major repair date if it has a major repair date" do + record.startdate = Time.zone.local(2022, 1, 1) + record.mrcdate = Time.zone.local(2022, 2, 1) + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.after_major_repair_date")) + end + it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do record.startdate = Time.zone.today - 30.days record.scheme = scheme