Browse Source

feat: add validation for voiddate and mrcdate when startdate changed … (#942)

* 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
pull/945/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
d7f650a283
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      app/models/validations/date_validations.rb
  2. 2
      config/locales/en.yml
  3. 16
      spec/models/validations/date_validations_spec.rb

12
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

2
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:

16
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

Loading…
Cancel
Save