diff --git a/app/models/location.rb b/app/models/location.rb index bbcf7af4d..732fe0049 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -394,9 +394,10 @@ class Location < ApplicationRecord end def status_during(date) + return unless date.present? closest_reactivation = location_deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date) } return { status: :reactivating_soon, date: closest_reactivation.reactivation_date } if closest_reactivation.present? - return { status: :reactivating_soon, date: available_from } if available_from > date + return { status: :reactivating_soon, date: available_from } if available_from.present? && available_from > date open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date < date diff --git a/app/models/scheme.rb b/app/models/scheme.rb index fce696bb6..cfbbf0bc0 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -233,4 +233,12 @@ class Scheme < ApplicationRecord def reactivating_soon? status == :reactivating_soon end + + def status_during(date) + closest_reactivation = scheme_deactivation_periods.reverse.find { |period| period.reactivation_date.present? && date.between?(period.deactivation_date, period.reactivation_date) } + return { status: :reactivating_soon, date: closest_reactivation.reactivation_date } if closest_reactivation.present? + + open_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first + return { status: :deactivated, date: open_deactivation.deactivation_date } if open_deactivation.present? && open_deactivation.deactivation_date < date + end end diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index e9bf199b1..c52e3d0d9 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -60,13 +60,22 @@ module Validations::DateValidations record.errors.add :startdate, I18n.t("validations.setup.startdate.after_major_repair_date") end - status_during_startdate = record.location&.status_during(record.startdate) - if status_during_startdate.present? && status_during_startdate[:status] == :deactivated - record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) + location_status_during_startdate = record.location&.status_during(record.startdate) + if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated + record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date)) end - if status_during_startdate.present? && status_during_startdate[:status] == :reactivating_soon - record.errors.add :startdate, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) + if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon + record.errors.add :startdate, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date)) + end + + scheme_status_during_startdate = record.scheme&.status_during(record.startdate) + if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :deactivated + record.errors.add :startdate, I18n.t("validations.setup.startdate.during_deactivated_scheme", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date)) + end + + if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :reactivating_soon + record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date)) end end diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 6ebb79a2f..c14cb38e7 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -17,13 +17,22 @@ module Validations::SetupValidations end def validate_scheme(record) - status_during_startdate = record.location&.status_during(record.startdate) - if status_during_startdate.present? && status_during_startdate[:status] == :deactivated - record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) + location_status_during_startdate = record.location&.status_during(record.startdate) + if location_status_during_startdate.present? && location_status_during_startdate[:status] == :deactivated + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_location", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date)) end - if status_during_startdate.present? && status_during_startdate[:status] == :reactivating_soon - record.errors.add :scheme_id, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: status_during_startdate[:date].to_formatted_s(:govuk_date)) + if location_status_during_startdate.present? && location_status_during_startdate[:status] == :reactivating_soon + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: record.location.postcode, date: location_status_during_startdate[:date].to_formatted_s(:govuk_date)) + end + + scheme_status_during_startdate = record.scheme&.status_during(record.startdate) + if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :deactivated + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.during_deactivated_scheme", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date)) + end + + if scheme_status_during_startdate.present? && scheme_status_during_startdate[:status] == :reactivating_soon + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: record.scheme.service_name, date: scheme_status_during_startdate[:date].to_formatted_s(:govuk_date)) end end diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index dafb2b86d..328ecccde 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -180,6 +180,56 @@ RSpec.describe Validations::DateValidations do .to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "15 September 2022")) end end + + context "with a scheme that is reactivating soon" do + let(:scheme) { create(:scheme) } + + before do + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) + scheme.reload + end + + it "produces error when tenancy start date is during deactivated scheme period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: scheme.service_name, date: "4 August 2022")) + end + + it "produces no error when tenancy start date is during an active scheme period" do + record.startdate = Time.zone.local(2022, 9, 1) + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + end + + context "with a scheme that has many reactivations soon" do + let(:scheme) { create(:scheme) } + + before do + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:) + scheme.reload + end + + it "produces error when tenancy start date is during deactivated scheme period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: scheme.service_name, date: "4 September 2022")) + end + + it "produces no error when tenancy start date is during an active scheme period" do + record.startdate = Time.zone.local(2022, 10, 1) + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + end end describe "major repairs date" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 69890f2e9..d30a8e548 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -101,6 +101,56 @@ RSpec.describe Validations::SetupValidations do .to include(match I18n.t("validations.setup.startdate.location_reactivating_soon", postcode: location.postcode, date: "15 September 2022")) end end + + context "with a scheme that is reactivating soon" do + let(:scheme) { create(:scheme) } + + before do + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) + scheme.reload + end + + it "produces error when tenancy start date is during deactivated scheme period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: scheme.service_name, date: "4 August 2022")) + end + + it "produces no error when tenancy start date is during an active scheme period" do + record.startdate = Time.zone.local(2022, 9, 1) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["scheme_id"]).to be_empty + end + end + + context "with a scheme that has many reactivations soon" do + let(:scheme) { create(:scheme) } + + before do + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:) + scheme.reload + end + + it "produces error when tenancy start date is during deactivated scheme period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.scheme_reactivating_soon", name: scheme.service_name, date: "4 September 2022")) + end + + it "produces no error when tenancy start date is during an active scheme period" do + record.startdate = Time.zone.local(2022, 10, 1) + record.scheme = scheme + setup_validator.validate_scheme(record) + expect(record.errors["scheme_id"]).to be_empty + end + end end describe "#validate_location" do