From 673800053ed3e32ae3b2b69bfba1da08adb2eb7a Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Tue, 28 Feb 2023 10:35:49 +0000 Subject: [PATCH] feat: move to setup validation --- app/models/validations/date_validations.rb | 69 ---------------- app/models/validations/setup_validations.rb | 57 +++++++++++++ .../validations/date_validations_spec.rb | 80 ------------------ .../validations/setup_validations_spec.rb | 82 +++++++++++++++++++ 4 files changed, 139 insertions(+), 149 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index c20f3af3e..ee7789171 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -33,16 +33,6 @@ module Validations::DateValidations def validate_startdate(record) return unless record.startdate && date_valid?("startdate", record) - created_at = record.created_at || Time.zone.now - - if created_at >= previous_collection_end_date && !record.startdate.between?(current_collection_start_date, next_collection_start_date) - record.errors.add :startdate, validation_error_message - end - - if created_at < previous_collection_end_date && !record.startdate.between?(previous_collection_start_date, next_collection_start_date) - record.errors.add :startdate, validation_error_message - end - 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 @@ -68,65 +58,6 @@ module Validations::DateValidations private - def active_collection_start_date - if FormHandler.instance.lettings_in_crossover_period? - previous_collection_start_date - else - current_collection_start_date - end - end - - def validation_error_message - current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") - - if FormHandler.instance.lettings_in_crossover_period? - I18n.t( - "validations.setup.startdate.previous_and_current_financial_year", - previous_start_year_short: previous_collection_start_date.strftime("%y"), - previous_end_year_short: previous_collection_end_date.strftime("%y"), - previous_start_year_long: previous_collection_start_date.strftime("#{previous_collection_start_date.day.ordinalize} %B %Y"), - current_end_year_short: current_collection_end_date.strftime("%y"), - current_end_year_long:, - ) - else - I18n.t( - "validations.setup.startdate.current_financial_year", - current_start_year_short: current_collection_start_date.strftime("%y"), - current_end_year_short: current_collection_end_date.strftime("%y"), - current_start_year_long: current_collection_start_date.strftime("#{current_collection_start_date.day.ordinalize} %B %Y"), - current_end_year_long:, - ) - end - end - - def previous_collection_start_suffix - previous_collection_start_date.year % 100 - end - - def current_collection_start_suffix - current_collection_start_date.year % 100 - end - - def previous_collection_start_date - FormHandler.instance.lettings_forms["previous_lettings"].start_date - end - - def previous_collection_end_date - FormHandler.instance.lettings_forms["previous_lettings"].end_date - end - - def current_collection_start_date - FormHandler.instance.lettings_forms["current_lettings"].start_date - end - - def current_collection_end_date - FormHandler.instance.lettings_forms["current_lettings"].end_date - end - - def next_collection_start_date - FormHandler.instance.lettings_forms["next_lettings"].start_date - end - def is_rsnvac_first_let?(record) [15, 16, 17].include?(record["rsnvac"]) end diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 93105f9ea..28173660f 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -1,6 +1,20 @@ module Validations::SetupValidations include Validations::SharedValidations + def validate_startdate_setup(record) + return unless record.startdate && date_valid?("startdate", record) + + created_at = record.created_at || Time.zone.now + + if created_at >= previous_collection_end_date && !record.startdate.between?(current_collection_start_date, next_collection_start_date) + record.errors.add :startdate, validation_error_message + end + + if created_at < previous_collection_end_date && !record.startdate.between?(previous_collection_start_date, next_collection_start_date) + record.errors.add :startdate, validation_error_message + end + end + def validate_irproduct_other(record) if intermediate_product_rent_type?(record) && record.irproduct_other.blank? record.errors.add :irproduct_other, I18n.t("validations.setup.intermediate_rent_product_name.blank") @@ -27,6 +41,49 @@ module Validations::SetupValidations private + def validation_error_message + current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") + + if FormHandler.instance.lettings_in_crossover_period? + I18n.t( + "validations.setup.startdate.previous_and_current_financial_year", + previous_start_year_short: previous_collection_start_date.strftime("%y"), + previous_end_year_short: previous_collection_end_date.strftime("%y"), + previous_start_year_long: previous_collection_start_date.strftime("#{previous_collection_start_date.day.ordinalize} %B %Y"), + current_end_year_short: current_collection_end_date.strftime("%y"), + current_end_year_long:, + ) + else + I18n.t( + "validations.setup.startdate.current_financial_year", + current_start_year_short: current_collection_start_date.strftime("%y"), + current_end_year_short: current_collection_end_date.strftime("%y"), + current_start_year_long: current_collection_start_date.strftime("#{current_collection_start_date.day.ordinalize} %B %Y"), + current_end_year_long:, + ) + end + end + + def previous_collection_start_date + FormHandler.instance.lettings_forms["previous_lettings"].start_date + end + + def previous_collection_end_date + FormHandler.instance.lettings_forms["previous_lettings"].end_date + end + + def current_collection_start_date + FormHandler.instance.lettings_forms["current_lettings"].start_date + end + + def current_collection_end_date + FormHandler.instance.lettings_forms["current_lettings"].end_date + end + + def next_collection_start_date + FormHandler.instance.lettings_forms["next_lettings"].start_date + end + def intermediate_product_rent_type?(record) record.rent_type == 5 end diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 07f10b25b..27882f033 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -9,86 +9,6 @@ RSpec.describe Validations::DateValidations do let(:scheme_no_end_date) { FactoryBot.create(:scheme, end_date: nil) } describe "tenancy start date" do - context "when in 22/23 collection" do - context "when in the crossover period" do - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2022, 4, 1)) - record.created_at = Time.zone.local(2022, 4, 1) - end - - it "cannot be before the first collection window start date" do - record.startdate = Time.zone.local(2021, 1, 1) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") - end - - it "cannot be after the second collection window end date" do - record.startdate = Time.zone.local(2023, 7, 1, 6) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") - end - end - - context "when after the crossover period" do - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 1)) - record.created_at = Time.zone.local(2023, 1, 1) - end - - it "cannot be before the first collection window start date" do - record.startdate = Time.zone.local(2022, 1, 1) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") - end - - it "cannot be after the second collection window end date" do - record.startdate = Time.zone.local(2023, 7, 1, 6) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") - end - end - end - - context "when in 23/24 collection" do - context "when in the crossover period" do - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2023, 4, 1)) - record.created_at = Time.zone.local(2023, 4, 1) - end - - it "cannot be before the first collection window start date" do - record.startdate = Time.zone.local(2022, 1, 1) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") - end - - it "cannot be after the second collection window end date" do - record.startdate = Time.zone.local(2024, 7, 1, 6) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") - end - end - - context "when after the crossover period" do - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2024, 1, 1)) - record.created_at = Time.zone.local(2024, 1, 1) - end - - it "cannot be before the first collection window start date" do - record.startdate = Time.zone.local(2023, 1, 1) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") - end - - it "cannot be after the second collection window end date" do - record.startdate = Time.zone.local(2024, 7, 1, 6) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") - end - end - end - it "must be a valid date" do record.startdate = Time.zone.local(0, 7, 1) date_validator.validate_startdate(record) diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 326a6936d..7bb00790a 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -6,6 +6,88 @@ RSpec.describe Validations::SetupValidations do let(:setup_validator_class) { Class.new { include Validations::SetupValidations } } let(:record) { FactoryBot.create(:lettings_log) } + describe "tenancy start date" do + context "when in 22/23 collection" do + context "when in the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2022, 4, 1)) + record.created_at = Time.zone.local(2022, 4, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2021, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2023, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") + end + end + + context "when after the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 1)) + record.created_at = Time.zone.local(2023, 1, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2022, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2023, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") + end + end + end + + context "when in 23/24 collection" do + context "when in the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 4, 1)) + record.created_at = Time.zone.local(2023, 4, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2022, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2024, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") + end + end + + context "when after the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2024, 1, 1)) + record.created_at = Time.zone.local(2024, 1, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2023, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2024, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") + end + end + end + end + describe "#validate_irproduct" do it "adds an error when the intermediate rent product name is not provided but the rent type was given as other intermediate rent product" do record.rent_type = 5