Browse Source

CLDC-1944: Replace soft date validation with hard date validation for 2024 and allow dates before 2000 in cases of soft validation failure

pull/2280/head
Robert Sullivan 2 years ago
parent
commit
15d4c9a202
  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. 11
      app/models/validations/sales/sale_information_validations.rb
  5. 6
      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)"]
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 > 0
Date.new(year.to_i, month.to_i, day.to_i)
else
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 }]
@informative_text = {}
@title_text = {
"translation" => "validations.sale_information.hodate.must_be_less_than_3_years_from_saledate",
"translation" => "validations.sale_information.hodate.must_be_less_than_3_years_from_saledate_soft",
"arguments" => [],
}
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 }]
@informative_text = {}
@title_text = {
"translation" => "validations.sale_information.saledate.must_be_less_than_3_years_from_hodate",
"translation" => "validations.sale_information.saledate.must_be_less_than_3_years_from_hodate_soft",
"arguments" => [],
}
end

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

@ -1,14 +1,21 @@
module Validations::Sales::SaleInformationValidations
include Validations::SharedValidations
include CollectionTimeHelper
include MoneyFormattingHelper
def validate_practical_completion_date_before_saledate(record)
return if record.saledate.blank? || record.hodate.blank?
def validate_practical_completion_date(record)
return unless !record.hodate.blank? && date_valid?("hodate", record)
return if record.saledate.blank?
if record.hodate > record.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")
end
if record.saledate - record.hodate >= 3.years && record.form.start_year_after_2024?
record.errors.add :hodate, :over_a_year_from_saledate, message: 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
def validate_exchange_date(record)

6
config/locales/en.yml

@ -605,14 +605,16 @@ en:
rent_to_buy: "Rent to Buy buyers should not have lived here before"
hodate:
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_soft: "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:
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"
saledate:
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_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_less_than_3_years_from_hodate_soft: "You told us sale completion date is more than 3 years after practical completion or handover date"
must_be_after_hodate: "Sale completion date must be after practical completion or handover date"
previous_property_type:
property_type_bedsit: "A bedsit cannot have more than 1 bedroom"

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
expect(page.title_text).to eq({
"translation" => "validations.sale_information.hodate.must_be_less_than_3_years_from_saledate",
"translation" => "validations.sale_information.hodate.must_be_less_than_3_years_from_saledate_soft",
"arguments" => [],
})
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
expect(page.title_text).to eq({
"translation" => "validations.sale_information.saledate.must_be_less_than_3_years_from_hodate",
"translation" => "validations.sale_information.saledate.must_be_less_than_3_years_from_hodate_soft",
"arguments" => [],
})
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 } }
describe "#validate_practical_completion_date_before_saledate" do
describe "#validate_practical_completion_date" do
context "when hodate blank" do
let(:record) { build(:sales_log, hodate: nil) }
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
end
@ -20,7 +20,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:record) { build(:sales_log, saledate: nil) }
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
end
@ -30,27 +30,59 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
let(:record) { build(:sales_log, hodate: nil, saledate: nil) }
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
end
end
context "when hodate before saledate" do
let(:record) { build(:sales_log, hodate: 2.months.ago, saledate: 1.month.ago) }
context "when hodate invalid" do
let(:record) { build(:sales_log, hodate: Date.new(0, 1, 1)) }
it "does not add the error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record)
it "adds an error" do
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
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
let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) }
it "adds error" do
sale_information_validator.validate_practical_completion_date_before_saledate(record)
it "adds an error" do
sale_information_validator.validate_practical_completion_date(record)
expect(record.errors[:hodate]).to be_present
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")) }
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
end

Loading…
Cancel
Save