From bc1fe83750b9b6bf480b65bf30908af666e77a69 Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:50:14 +0000 Subject: [PATCH] CLDC-3757: Sales - Change values in validation check for Q84 (25/26) (#2781) * Update 2025 version of validation check * Update validation for 2025 * Put two if statements into one * Different approach to comparing dates * Alternative method to compare dates * Update tests --- .../form/sales/pages/handover_date_check.rb | 12 ++- .../sales/sale_information_validations.rb | 5 +- .../validations/sales/soft_validations.rb | 6 ++ .../validations/sales/sale_information.en.yml | 2 + .../sales/pages/handover_date_check_spec.rb | 82 ++++++++++++------- .../sale_information_validations_spec.rb | 5 +- 6 files changed, 77 insertions(+), 35 deletions(-) diff --git a/app/models/form/sales/pages/handover_date_check.rb b/app/models/form/sales/pages/handover_date_check.rb index 690c3dde7..8ae41316c 100644 --- a/app/models/form/sales/pages/handover_date_check.rb +++ b/app/models/form/sales/pages/handover_date_check.rb @@ -3,8 +3,6 @@ class Form::Sales::Pages::HandoverDateCheck < ::Form::Page super @id = "handover_date_check" @copy_key = "sales.soft_validations.hodate_check" - @depends_on = [{ "saledate_check" => nil, "hodate_3_years_or_more_saledate?" => true }, - { "saledate_check" => 1, "hodate_3_years_or_more_saledate?" => true }] @informative_text = {} @title_text = { "translation" => "forms.#{form.start_date.year}.#{@copy_key}.title_text", @@ -12,6 +10,16 @@ class Form::Sales::Pages::HandoverDateCheck < ::Form::Page } end + def depends_on + if form.start_year_2025_or_later? + [{ "saledate_check" => nil, "hodate_5_years_or_more_saledate?" => true }, + { "saledate_check" => 1, "hodate_5_years_or_more_saledate?" => true }] + else + [{ "saledate_check" => nil, "hodate_3_years_or_more_saledate?" => true }, + { "saledate_check" => 1, "hodate_3_years_or_more_saledate?" => true }] + end + end + def questions @questions ||= [ Form::Sales::Questions::HandoverDateCheck.new(nil, nil, self), diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 3c5e3f2b9..fa095a5e2 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -12,7 +12,10 @@ module Validations::Sales::SaleInformationValidations record.errors.add :saledate, I18n.t("validations.sales.sale_information.saledate.must_be_after_hodate") end - if record.saledate - record.hodate >= 3.years && record.form.start_year_2024_or_later? + if (record.saledate - 5.years) >= record.hodate && record.form.start_year_2025_or_later? + record.errors.add :hodate, I18n.t("validations.sales.sale_information.hodate.must_be_less_than_5_years_from_saledate") + record.errors.add :saledate, I18n.t("validations.sales.sale_information.saledate.must_be_less_than_5_years_from_hodate") + elsif (record.saledate - 3.years) >= record.hodate && record.startdate.year <= 2024 record.errors.add :hodate, I18n.t("validations.sales.sale_information.hodate.must_be_less_than_3_years_from_saledate") record.errors.add :saledate, I18n.t("validations.sales.sale_information.saledate.must_be_less_than_3_years_from_hodate") end diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index 5095e5ff9..d53391dd1 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -110,6 +110,12 @@ module Validations::Sales::SoftValidations saledate - hodate >= 3.years end + def hodate_5_years_or_more_saledate? + return unless hodate && saledate + + saledate - hodate >= 5.years + end + def purchase_price_higher_or_lower_text value < sale_range.soft_min ? "lower" : "higher" end diff --git a/config/locales/validations/sales/sale_information.en.yml b/config/locales/validations/sales/sale_information.en.yml index 20aca17d2..8fb7d02d4 100644 --- a/config/locales/validations/sales/sale_information.en.yml +++ b/config/locales/validations/sales/sale_information.en.yml @@ -8,9 +8,11 @@ en: hodate: must_be_before_saledate: "Practical completion or handover date must be 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." + must_be_less_than_5_years_from_saledate: "Practical completion or handover date must be less than 5 years before sale completion date." saledate: must_be_after_hodate: "Sale completion date must be 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_5_years_from_hodate: "Sale completion date must be less than 5 years after practical completion or handover 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." mortgage_used_year: "You must answer either ‘yes’ or ‘no’ to the question ‘was a mortgage used’ for the selected year." diff --git a/spec/models/form/sales/pages/handover_date_check_spec.rb b/spec/models/form/sales/pages/handover_date_check_spec.rb index ae465da16..e6cc3acf5 100644 --- a/spec/models/form/sales/pages/handover_date_check_spec.rb +++ b/spec/models/form/sales/pages/handover_date_check_spec.rb @@ -5,44 +5,66 @@ RSpec.describe Form::Sales::Pages::HandoverDateCheck, type: :model do let(:page_id) { "" } let(:page_definition) { nil } - let(:form) { instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) } - let(:subsection) { instance_double(Form::Subsection, form:) } - it "has correct subsection" do - expect(page.subsection).to eq(subsection) - end + context "when form start year is <= 2024" do + let(:form) { instance_double(Form, start_date: Time.zone.local(2024, 4, 1), start_year_2025_or_later?: false) } + let(:subsection) { instance_double(Form::Subsection, form:) } - it "has correct questions" do - expect(page.questions.map(&:id)).to eq(%w[hodate_check]) - end + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end - it "has the correct id" do - expect(page.id).to eq("handover_date_check") - end + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[hodate_check]) + end - it "has the correct title_text" do - expect(page.title_text).to eq({ - "translation" => "forms.2024.sales.soft_validations.hodate_check.title_text", - "arguments" => [], - }) - end + it "has the correct id" do + expect(page.id).to eq("handover_date_check") + end - it "has the correct informative_text" do - expect(page.informative_text).to eq({}) - end + it "has the correct title_text" do + expect(page.title_text).to eq({ + "translation" => "forms.2024.sales.soft_validations.hodate_check.title_text", + "arguments" => [], + }) + end - it "has correct depends_on" do - expect(page.depends_on).to eq([ - { "hodate_3_years_or_more_saledate?" => true, "saledate_check" => nil }, - { "hodate_3_years_or_more_saledate?" => true, "saledate_check" => 1 }, - ]) - end + it "has the correct informative_text" do + expect(page.informative_text).to eq({}) + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([ + { "hodate_3_years_or_more_saledate?" => true, "saledate_check" => nil }, + { "hodate_3_years_or_more_saledate?" => true, "saledate_check" => 1 }, + ]) + end + + it "is interruption screen page" do + expect(page.interruption_screen?).to eq(true) + end - it "is interruption screen page" do - expect(page.interruption_screen?).to eq(true) + it "is has correct interruption_screen_question_ids" do + expect(page.interruption_screen_question_ids).to eq(%w[hodate saledate]) + end end - it "is has correct interruption_screen_question_ids" do - expect(page.interruption_screen_question_ids).to eq(%w[hodate saledate]) + context "when form start year is 2025" do + let(:form) { instance_double(Form, start_date: Time.zone.local(2025, 4, 1), start_year_2025_or_later?: true) } + let(:subsection) { instance_double(Form::Subsection, form:) } + + it "has the correct title_text" do + expect(page.title_text).to eq({ + "translation" => "forms.2025.sales.soft_validations.hodate_check.title_text", + "arguments" => [], + }) + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([ + { "hodate_5_years_or_more_saledate?" => true, "saledate_check" => nil }, + { "hodate_5_years_or_more_saledate?" => true, "saledate_check" => 1 }, + ]) + end end end diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 36dadc345..5cc0cdf07 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -60,10 +60,11 @@ RSpec.describe Validations::Sales::SaleInformationValidations 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 + it "does add an error" do sale_information_validator.validate_practical_completion_date(record) - expect(record.errors).not_to be_present + expect(record.errors[:hodate]).to be_present + expect(record.errors[:saledate]).to be_present end end