Browse Source

Extract lettings date validations

pull/2738/head
Manny Dinssa 8 months ago
parent
commit
8cdbb59a88
  1. 14
      app/models/validations/date_validations.rb
  2. 11
      config/locales/en.yml
  3. 13
      config/locales/validations/lettings/date.en.yml
  4. 18
      spec/models/validations/date_validations_spec.rb

14
app/models/validations/date_validations.rb

@ -4,30 +4,30 @@ module Validations::DateValidations
def validate_property_major_repairs(record)
date_valid?("mrcdate", record)
if record["startdate"].present? && record["mrcdate"].present? && record["startdate"] < record["mrcdate"]
record.errors.add :mrcdate, I18n.t("validations.property.mrcdate.before_tenancy_start")
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.before_tenancy_start")
end
if is_rsnvac_first_let?(record) && record["mrcdate"].present?
record.errors.add :mrcdate, I18n.t("validations.property.mrcdate.not_first_let")
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
record.errors.add :mrcdate, I18n.t("validations.property.mrcdate.ten_years_before_tenancy_start")
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.property.void_date.ten_years_before_tenancy_start")
record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.ten_years_before_tenancy_start")
end
if record["voiddate"].present? && record["startdate"].present? && record["startdate"].to_date < record["voiddate"].to_date
record.errors.add :voiddate, I18n.t("validations.property.void_date.before_tenancy_start")
record.errors.add :voiddate, I18n.t("validations.lettings.date.void_date.before_tenancy_start")
end
if record["voiddate"].present? && record["mrcdate"].present? && record["mrcdate"].to_date < record["voiddate"].to_date
record.errors.add :voiddate, :after_mrcdate, message: I18n.t("validations.property.void_date.after_mrcdate")
record.errors.add :mrcdate, I18n.t("validations.property.mrcdate.before_void_date")
record.errors.add :voiddate, :after_mrcdate, message: I18n.t("validations.lettings.date.void_date.after_mrcdate")
record.errors.add :mrcdate, I18n.t("validations.lettings.date.mrcdate.before_void_date")
end
end

11
config/locales/en.yml

@ -234,7 +234,7 @@ en:
scheme_deleted: "%{service_name} has been deleted."
user_deleted: "%{name} has been deleted."
organisation_deleted: "%{name} has been deleted."
user_updated:
user_updated:
self: "Your account details have been updated."
other: "%{name}’s details have been updated."
@ -362,15 +362,6 @@ en:
invalid: "UPRN must be 12 digits or less."
uprn_known:
invalid: "You must answer UPRN known?"
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."
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."
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."
la:
la_invalid_for_org: "%{org_name} does not operate in %{la_name}."
postcode_invalid_for_org: "Enter a postcode in an area covered by %{org_name}."

13
config/locales/validations/lettings/date.en.yml

@ -0,0 +1,13 @@
en:
validations:
lettings:
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."
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."
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."

18
spec/models/validations/date_validations_spec.rb

@ -58,7 +58,7 @@ RSpec.describe Validations::DateValidations do
record.mrcdate = Time.zone.local(2022, 2, 1)
date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.property.mrcdate.before_tenancy_start"))
.to include(match I18n.t("validations.lettings.date.mrcdate.before_tenancy_start"))
end
it "must be before the tenancy start date" do
@ -74,7 +74,7 @@ RSpec.describe Validations::DateValidations do
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"))
.to include(match I18n.t("validations.lettings.date.mrcdate.ten_years_before_tenancy_start"))
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.ten_years_after_mrc_date"))
end
@ -93,7 +93,7 @@ RSpec.describe Validations::DateValidations do
record.mrcdate = Time.zone.local(2022, 1, 1)
date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.property.mrcdate.not_first_let"))
.to include(match I18n.t("validations.lettings.date.mrcdate.not_first_let"))
end
it "validates that no major repair date is provided for a conversion" do
@ -101,7 +101,7 @@ RSpec.describe Validations::DateValidations do
record.mrcdate = Time.zone.local(2022, 1, 1)
date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.property.mrcdate.not_first_let"))
.to include(match I18n.t("validations.lettings.date.mrcdate.not_first_let"))
end
it "validates that no major repair date is provided for a leased property" do
@ -109,7 +109,7 @@ RSpec.describe Validations::DateValidations do
record.mrcdate = Time.zone.local(2022, 1, 1)
date_validator.validate_property_major_repairs(record)
expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.property.mrcdate.not_first_let"))
.to include(match I18n.t("validations.lettings.date.mrcdate.not_first_let"))
end
end
@ -129,7 +129,7 @@ RSpec.describe Validations::DateValidations do
record.voiddate = Time.zone.local(2022, 2, 1)
date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"])
.to include(match I18n.t("validations.property.void_date.before_tenancy_start"))
.to include(match I18n.t("validations.lettings.date.void_date.before_tenancy_start"))
end
it "must be before the tenancy start date" do
@ -145,7 +145,7 @@ RSpec.describe Validations::DateValidations do
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"))
.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.setup.startdate.ten_years_after_void_date"))
end
@ -164,9 +164,9 @@ RSpec.describe Validations::DateValidations do
record.voiddate = Time.zone.local(2022, 2, 1)
date_validator.validate_property_void_date(record)
expect(record.errors["voiddate"])
.to include(match I18n.t("validations.property.void_date.after_mrcdate"))
.to include(match I18n.t("validations.lettings.date.void_date.after_mrcdate"))
expect(record.errors["mrcdate"])
.to include(match I18n.t("validations.property.mrcdate.before_void_date"))
.to include(match I18n.t("validations.lettings.date.mrcdate.before_void_date"))
end
it "must be before major repairs date" do

Loading…
Cancel
Save