From ec910bac8a8a7df90065461a8fc0bc091c40bf56 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 24 Feb 2023 16:43:02 +0000 Subject: [PATCH] feat: add validation and start to freeze time in tests --- app/models/validations/date_validations.rb | 52 ---------------- app/models/validations/setup_validations.rb | 62 +++++++++++++++++++ spec/factories/lettings_log.rb | 2 +- .../validations/soft_validations_spec.rb | 6 ++ 4 files changed, 69 insertions(+), 53 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 66e472ba0..ee847f094 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -30,60 +30,8 @@ module Validations::DateValidations end end - def validate_startdate(record) - return unless record.startdate && date_valid?("startdate", record) - - created_at = record.created_at || Time.zone.now - - if created_at > first_collection_end_date && record.startdate < second_collection_start_date - record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") - end - - if (record.startdate < first_collection_start_date || record.startdate > second_collection_end_date) && FeatureToggle.startdate_collection_window_validation_enabled? - record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") - 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 - - if record.scheme_id.present? - scheme_end_date = record.scheme.end_date - if scheme_end_date.present? && record.startdate > scheme_end_date - record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date") - end - end - - if record["voiddate"].present? && record.startdate < record["voiddate"] - record.errors.add :startdate, I18n.t("validations.setup.startdate.after_void_date") - end - - if record["mrcdate"].present? && record.startdate < record["mrcdate"] - record.errors.add :startdate, I18n.t("validations.setup.startdate.after_major_repair_date") - end - - location_during_startdate_validation(record, :startdate) - scheme_during_startdate_validation(record, :startdate) - end - private - def first_collection_start_date - @first_collection_start_date ||= FormHandler.instance.lettings_forms["previous_lettings"].start_date - end - - def first_collection_end_date - @first_collection_end_date ||= FormHandler.instance.lettings_forms["previous_lettings"].end_date - end - - def second_collection_start_date - @second_collection_start_date ||= FormHandler.instance.lettings_forms["current_lettings"].start_date - end - - def second_collection_end_date - @second_collection_end_date ||= FormHandler.instance.lettings_forms["current_lettings"].end_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..c845526b7 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -1,5 +1,36 @@ module Validations::SetupValidations include Validations::SharedValidations + include CollectionTimeHelper + + def validate_startdate(record) + return unless record.startdate && date_valid?("startdate", record) + + unless record.startdate.between?(active_collection_start_date, current_collection_end_date) || !FeatureToggle.startdate_collection_window_validation_enabled? + 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 + + if record.scheme_id.present? + scheme_end_date = record.scheme.end_date + if scheme_end_date.present? && record.startdate > scheme_end_date + record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date") + end + end + + if record["voiddate"].present? && record.startdate < record["voiddate"] + record.errors.add :startdate, I18n.t("validations.setup.startdate.after_void_date") + end + + if record["mrcdate"].present? && record.startdate < record["mrcdate"] + record.errors.add :startdate, I18n.t("validations.setup.startdate.after_major_repair_date") + end + + location_during_startdate_validation(record, :startdate) + scheme_during_startdate_validation(record, :startdate) + end def validate_irproduct_other(record) if intermediate_product_rent_type?(record) && record.irproduct_other.blank? @@ -27,6 +58,37 @@ module Validations::SetupValidations 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 intermediate_product_rent_type?(record) record.rent_type == 5 end diff --git a/spec/factories/lettings_log.rb b/spec/factories/lettings_log.rb index cb97eed68..68e6afe63 100644 --- a/spec/factories/lettings_log.rb +++ b/spec/factories/lettings_log.rb @@ -18,7 +18,7 @@ FactoryBot.define do age2 { 19 } renewal { 1 } rent_type { 1 } - startdate { Time.zone.local(2021, 5, 1) } + startdate { Time.zone.local(2023, 1, 1) } end trait :soft_validations_triggered do status { 1 } diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 9ca9436fb..84ee0ac91 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -4,6 +4,12 @@ RSpec.describe Validations::SoftValidations do let(:organisation) { FactoryBot.create(:organisation, provider_type: "PRP") } let(:record) { FactoryBot.create(:lettings_log, owning_organisation: organisation) } + around do |example| + Timecop.freeze(Time.zone.local(2022, 2, 1)) do + example.run + end + end + describe "rent min max validations" do before do LaRentRange.create!(