Browse Source

CLDC-1944: Replace soft date validation with hard date validation for 2024 and allow dates before 2000 (#2280)

* CLDC-1944: Replace soft date validation with hard date validation for 2024 and allow dates before 2000 in cases of soft validation failure
pull/2290/head
Robert Sullivan 10 months ago committed by GitHub
parent
commit
8693037132
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      app/controllers/form_controller.rb
  2. 2
      app/models/form/sales/pages/handover_date_check.rb
  3. 2
      app/models/form/sales/pages/sale_date_check.rb
  4. 13
      app/models/validations/sales/sale_information_validations.rb
  5. 9
      config/locales/en.yml
  6. 2
      spec/models/form/sales/pages/handover_date_check_spec.rb
  7. 2
      spec/models/form/sales/pages/sale_date_check_spec.rb
  8. 54
      spec/models/validations/sales/sale_information_validations_spec.rb

2
app/controllers/form_controller.rb

@ -86,7 +86,7 @@ private
year = params[@log.model_name.param_key]["#{question.id}(1i)"] year = params[@log.model_name.param_key]["#{question.id}(1i)"]
next unless [day, month, year].any?(&:present?) next unless [day, month, year].any?(&:present?)
result[question.id] = if Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.between?(2000, 2200) result[question.id] = if Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.positive?
Date.new(year.to_i, month.to_i, day.to_i) Date.new(year.to_i, month.to_i, day.to_i)
else else
Date.new(0, 1, 1) Date.new(0, 1, 1)

2
app/models/form/sales/pages/handover_date_check.rb

@ -6,7 +6,7 @@ class Form::Sales::Pages::HandoverDateCheck < ::Form::Page
{ "saledate_check" => 1, "hodate_3_years_or_more_saledate?" => true }] { "saledate_check" => 1, "hodate_3_years_or_more_saledate?" => true }]
@informative_text = {} @informative_text = {}
@title_text = { @title_text = {
"translation" => "validations.sale_information.hodate.must_be_less_than_3_years_from_saledate", "translation" => "soft_validations.hodate.must_be_less_than_3_years_from_saledate",
"arguments" => [], "arguments" => [],
} }
end end

2
app/models/form/sales/pages/sale_date_check.rb

@ -6,7 +6,7 @@ class Form::Sales::Pages::SaleDateCheck < ::Form::Page
{ "hodate_check" => 1, "hodate_3_years_or_more_saledate?" => true }] { "hodate_check" => 1, "hodate_3_years_or_more_saledate?" => true }]
@informative_text = {} @informative_text = {}
@title_text = { @title_text = {
"translation" => "validations.sale_information.saledate.must_be_less_than_3_years_from_hodate", "translation" => "soft_validations.saledate.must_be_less_than_3_years_from_hodate",
"arguments" => [], "arguments" => [],
} }
end end

13
app/models/validations/sales/sale_information_validations.rb

@ -1,14 +1,21 @@
module Validations::Sales::SaleInformationValidations module Validations::Sales::SaleInformationValidations
include Validations::SharedValidations
include CollectionTimeHelper include CollectionTimeHelper
include MoneyFormattingHelper include MoneyFormattingHelper
def validate_practical_completion_date_before_saledate(record) def validate_practical_completion_date(record)
return if record.saledate.blank? || record.hodate.blank? return unless record.hodate.present? && date_valid?("hodate", record)
return if record.saledate.blank?
if record.hodate > record.saledate if record.hodate > record.saledate
record.errors.add :hodate, I18n.t("validations.sale_information.hodate.must_be_before_saledate") record.errors.add :hodate, I18n.t("validations.sale_information.hodate.must_be_before_saledate")
record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_after_hodate") record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_after_hodate")
end end
if record.saledate - record.hodate >= 3.years && record.form.start_year_after_2024?
record.errors.add :hodate, I18n.t("validations.sale_information.hodate.must_be_less_than_3_years_from_saledate")
record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_less_than_3_years_from_hodate")
end
end end
def validate_exchange_date(record) def validate_exchange_date(record)
@ -20,7 +27,7 @@ module Validations::Sales::SaleInformationValidations
end end
if record.saledate - record.exdate >= 1.year if record.saledate - record.exdate >= 1.year
record.errors.add :exdate, :over_a_year_from_saledate, message: I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate") record.errors.add :exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")
record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_less_than_1_year_from_exdate") record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_less_than_1_year_from_exdate")
end end
end end

9
config/locales/en.yml

@ -606,14 +606,14 @@ en:
rent_to_buy: "Rent to Buy buyers should not have lived here before" rent_to_buy: "Rent to Buy buyers should not have lived here before"
hodate: hodate:
must_be_before_saledate: "Practical completion or handover date must be before sale completion date" must_be_before_saledate: "Practical completion or handover date must be before sale completion date"
must_be_less_than_3_years_from_saledate: "You told us practical completion or handover date is more than 3 years before sale completion date" must_be_less_than_3_years_from_saledate: "Practical completion or handover date must be less than 3 years before sale completion date"
exdate: exdate:
must_be_before_saledate: "Contract exchange date must be before sale completion date" must_be_before_saledate: "Contract exchange date must be before sale completion date"
must_be_less_than_1_year_from_saledate: "Contract exchange date must be less than 1 year before sale completion date" must_be_less_than_1_year_from_saledate: "Contract exchange date must be less than 1 year before sale completion date"
saledate: saledate:
must_be_after_exdate: "Sale completion date must be after contract exchange date" must_be_after_exdate: "Sale completion date must be after contract exchange date"
must_be_less_than_1_year_from_exdate: "Sale completion date must be less than 1 year after contract exchange date" must_be_less_than_1_year_from_exdate: "Sale completion date must be less than 1 year after contract exchange date"
must_be_less_than_3_years_from_hodate: "You told us sale completion date is more than 3 years after practical completion or handover date" must_be_less_than_3_years_from_hodate: "Sale completion date must be less than 3 years after practical completion or handover date"
must_be_after_hodate: "Sale completion date must be after practical completion or handover date" must_be_after_hodate: "Sale completion date must be after practical completion or handover date"
previous_property_type: previous_property_type:
property_type_bedsit: "A bedsit cannot have more than 1 bedroom" property_type_bedsit: "A bedsit cannot have more than 1 bedroom"
@ -754,6 +754,11 @@ Make sure these answers are correct."
informative_text: "The reason you have entered looks very similar to one of the existing response categories. informative_text: "The reason you have entered looks very similar to one of the existing response categories.
Please check the categories and select the appropriate one. Please check the categories and select the appropriate one.
If the existing categories are not suitable, please confirm here to move onto the next question." If the existing categories are not suitable, please confirm here to move onto the next question."
hodate:
must_be_less_than_3_years_from_saledate: "You told us practical completion or handover date is more than 3 years before sale completion date"
saledate:
must_be_less_than_3_years_from_hodate: "You told us sale completion date is more than 3 years after practical completion or handover date"
devise: devise:
email: email:

2
spec/models/form/sales/pages/handover_date_check_spec.rb

@ -25,7 +25,7 @@ RSpec.describe Form::Sales::Pages::HandoverDateCheck, type: :model do
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ expect(page.title_text).to eq({
"translation" => "validations.sale_information.hodate.must_be_less_than_3_years_from_saledate", "translation" => "soft_validations.hodate.must_be_less_than_3_years_from_saledate",
"arguments" => [], "arguments" => [],
}) })
end end

2
spec/models/form/sales/pages/sale_date_check_spec.rb

@ -25,7 +25,7 @@ RSpec.describe Form::Sales::Pages::SaleDateCheck, type: :model do
it "has the correct title_text" do it "has the correct title_text" do
expect(page.title_text).to eq({ expect(page.title_text).to eq({
"translation" => "validations.sale_information.saledate.must_be_less_than_3_years_from_hodate", "translation" => "soft_validations.saledate.must_be_less_than_3_years_from_hodate",
"arguments" => [], "arguments" => [],
}) })
end end

54
spec/models/validations/sales/sale_information_validations_spec.rb

@ -5,12 +5,12 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } } let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } }
describe "#validate_practical_completion_date_before_saledate" do describe "#validate_practical_completion_date" do
context "when hodate blank" do context "when hodate blank" do
let(:record) { build(:sales_log, hodate: nil) } let(:record) { build(:sales_log, hodate: nil) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors).not_to be_present expect(record.errors).not_to be_present
end end
@ -20,7 +20,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:record) { build(:sales_log, saledate: nil) } let(:record) { build(:sales_log, saledate: nil) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors).not_to be_present expect(record.errors).not_to be_present
end end
@ -30,27 +30,59 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:record) { build(:sales_log, hodate: nil, saledate: nil) } let(:record) { build(:sales_log, hodate: nil, saledate: nil) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors).not_to be_present expect(record.errors).not_to be_present
end end
end end
context "when hodate before saledate" do context "when hodate invalid" do
let(:record) { build(:sales_log, hodate: 2.months.ago, saledate: 1.month.ago) } let(:record) { build(:sales_log, hodate: Date.new(0, 1, 1)) }
it "does not add the error" do it "adds an error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).to be_present
end
end
context "when hodate less than 3 years before saledate" do
let(:record) { build(:sales_log, hodate: Date.new(2021, 12, 2), saledate: Date.new(2024, 12, 1)) }
it "does not add an error" do
sale_information_validator.validate_practical_completion_date(record)
expect(record.errors).not_to be_present expect(record.errors).not_to be_present
end end
end end
context "when hodate 3 or more years before saledate" do
context "and form year is 2023 or earlier" do
let(:record) { build(:sales_log, hodate: Date.new(2020, 12, 1), saledate: Date.new(2023, 12, 1)) }
it "does not add an error" do
sale_information_validator.validate_practical_completion_date(record)
expect(record.errors).not_to be_present
end
end
context "and form year is 2024 or later" do
let(:record) { build(:sales_log, hodate: Date.new(2021, 12, 1), saledate: Date.new(2024, 12, 1)) }
it "adds an error" do
sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).to be_present
end
end
end
context "when hodate after saledate" do context "when hodate after saledate" do
let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) } let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) }
it "adds error" do it "adds an error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).to be_present expect(record.errors[:hodate]).to be_present
end end
@ -60,7 +92,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:record) { build(:sales_log, hodate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } let(:record) { build(:sales_log, hodate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record) sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).not_to be_present expect(record.errors[:hodate]).not_to be_present
end end

Loading…
Cancel
Save