diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 3a7350a8a..d29f21ce2 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -32,7 +32,7 @@ module DerivedVariables::LettingsLogVariables def scheme_has_multiple_locations? return false unless scheme - @scheme_locations_count ||= scheme.locations.active.size + @scheme_locations_count ||= scheme.locations.active_in_2_weeks.size @scheme_locations_count > 1 end @@ -258,7 +258,7 @@ private def reset_scheme_location! self.location = nil - if scheme && scheme.locations.active.size == 1 + if scheme && scheme.locations.active_in_2_weeks.size == 1 self.location = scheme.locations.first end end diff --git a/app/models/form/lettings/questions/location_id.rb b/app/models/form/lettings/questions/location_id.rb index 80c2516e8..6e5ed3dc0 100644 --- a/app/models/form/lettings/questions/location_id.rb +++ b/app/models/form/lettings/questions/location_id.rb @@ -19,8 +19,7 @@ class Form::Lettings::Questions::LocationId < ::Form::Question answer_opts = {} return answer_opts unless ActiveRecord::Base.connected? - Location.select(:id, :postcode, :name).where("startdate <= ? or startdate IS NULL", - Time.zone.today).each_with_object(answer_opts) do |location, hsh| + Location.started_in_2_weeks.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } hsh end diff --git a/app/models/form/lettings/questions/scheme_id.rb b/app/models/form/lettings/questions/scheme_id.rb index 20bda8412..5d140b21b 100644 --- a/app/models/form/lettings/questions/scheme_id.rb +++ b/app/models/form/lettings/questions/scheme_id.rb @@ -35,8 +35,7 @@ class Form::Lettings::Questions::SchemeId < ::Form::Question else Scheme.includes(:locations).select(:id).where(confirmed: true) end - filtered_scheme_ids = schemes.joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", - Time.zone.today)).map(&:id) + filtered_scheme_ids = schemes.joins(:locations).merge(Location.started_in_2_weeks).map(&:id) answer_options.select do |k, _v| filtered_scheme_ids.include?(k.to_i) || k.blank? end diff --git a/app/models/location.rb b/app/models/location.rb index 818ea5700..0dd6ba522 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -23,6 +23,8 @@ class Location < ApplicationRecord scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } scope :started, -> { where("startdate <= ?", Time.zone.today).or(where(startdate: nil)) } scope :active, -> { where(confirmed: true).and(started) } + scope :started_in_2_weeks, -> { where("startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) } + scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) } scope :confirmed, -> { where(confirmed: true) } scope :unconfirmed, -> { where.not(confirmed: true) } diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index b83b143cc..3757f1cfb 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -54,8 +54,8 @@ module Validations::DateValidations record.errors.add :startdate, I18n.t("validations.setup.startdate.ten_years_after_mrc_date") end - location_during_startdate_validation(record, :startdate) - scheme_during_startdate_validation(record, :startdate) + location_during_startdate_validation(record) + scheme_during_startdate_validation(record) end private diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 04fb2a2d7..3e3569a55 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -23,7 +23,7 @@ module Validations::SetupValidations end def validate_location(record) - location_during_startdate_validation(record, :location_id) + location_during_startdate_validation(record) end def validate_scheme_has_confirmed_locations_validation(record) @@ -35,8 +35,8 @@ module Validations::SetupValidations end def validate_scheme(record) - location_during_startdate_validation(record, :scheme_id) - scheme_during_startdate_validation(record, :scheme_id) + location_during_startdate_validation(record) + scheme_during_startdate_validation(record) end def validate_organisation(record) diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index f9cd25e46..8cf2e53fe 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -64,20 +64,23 @@ module Validations::SharedValidations end end - def location_during_startdate_validation(record, field) + def location_during_startdate_validation(record) location_inactive_status = inactive_status(record.startdate, record.location) if location_inactive_status.present? date, scope, deactivation_date = location_inactive_status.values_at(:date, :scope, :deactivation_date) - record.errors.add field, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}", postcode: record.location.postcode, date:, deactivation_date:) + record.errors.add :startdate, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}.startdate", postcode: record.location.postcode, date:, deactivation_date:) + record.errors.add :location_id, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:, deactivation_date:) + record.errors.add :scheme_id, :not_active, message: I18n.t("validations.setup.startdate.location.#{scope}.location_id", postcode: record.location.postcode, date:, deactivation_date:) end end - def scheme_during_startdate_validation(record, field) + def scheme_during_startdate_validation(record) scheme_inactive_status = inactive_status(record.startdate, record.scheme) if scheme_inactive_status.present? date, scope, deactivation_date = scheme_inactive_status.values_at(:date, :scope, :deactivation_date) - record.errors.add field, I18n.t("validations.setup.startdate.scheme.#{scope}", name: record.scheme.service_name, date:, deactivation_date:) + record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme.#{scope}.startdate", name: record.scheme.service_name, date:, deactivation_date:) + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme.#{scope}.scheme_id", name: record.scheme.service_name, date:, deactivation_date:) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 1365fda3d..ec73f2906 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -248,13 +248,22 @@ en: ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date" location: - deactivated: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered." - reactivating_soon: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date" - activating_soon: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}" + deactivated: + startdate: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered. Select another location or edit the tenancy start date" + location_id: "The location %{postcode} was deactivated on %{date} and was not available on the day you entered. Select another location or edit the tenancy start date" + activating_soon: + startdate: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}" + location_id: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date" + reactivating_soon: + startdate: "The location %{postcode} is not available until %{date}. Enter a tenancy start date after %{date}" + location_id: "The location %{postcode} is not available until %{date}. Select another location or edit the tenancy start date" scheme: - deactivated: "%{name} was deactivated on %{date} and was not available on the day you entered" - reactivating_soon: "The scheme %{name} is not available until %{date}. Select another scheme or edit the tenancy start date" - activating_soon: "%{name} is not available until %{date}. Enter a tenancy start date after %{date}" + deactivated: + startdate: "The scheme %{name} was deactivated on %{date} and was not available on the day you entered. Select another scheme or edit the tenancy start date" + scheme_id: "The scheme %{name} was deactivated on %{date} and was not available on the day you entered. Select another scheme or edit the tenancy start date" + reactivating_soon: + startdate: "The scheme %{name} is not available until %{date}. Enter a tenancy start date after %{date}" + scheme_id: "The scheme %{name} is not available until %{date}. Select another scheme or edit the tenancy start date" owning_organisation: invalid: "Please select the owning organisation or managing organisation that you belong to" data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation." diff --git a/spec/models/form/lettings/questions/location_id_spec.rb b/spec/models/form/lettings/questions/location_id_spec.rb index fbe580970..ba3358e02 100644 --- a/spec/models/form/lettings/questions/location_id_spec.rb +++ b/spec/models/form/lettings/questions/location_id_spec.rb @@ -68,9 +68,9 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do Timecop.unfreeze end - context "and all the locations have a future startdate" do + context "and all the locations have a startdate more than 2 weeks in the future" do before do - FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 13)) + FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 6, 1)) FactoryBot.create(:location, scheme:, startdate: Time.utc(2023, 1, 1)) lettings_log.update!(scheme:) end @@ -80,6 +80,18 @@ RSpec.describe Form::Lettings::Questions::LocationId, type: :model do end end + context "and all but one of the locations have a startdate more than 2 weeks in the future" do + before do + FactoryBot.create(:location, scheme:, startdate: Time.utc(2022, 5, 13)) + FactoryBot.create(:location, scheme:, startdate: Time.utc(2023, 1, 1)) + lettings_log.update!(scheme:) + end + + it "the displayed_answer_options shows the locations" do + expect(question.displayed_answer_options(lettings_log).count).to eq(1) + end + end + context "and the locations have no startdate" do before do FactoryBot.create(:location, scheme:, startdate: nil) diff --git a/spec/models/form/lettings/questions/scheme_id_spec.rb b/spec/models/form/lettings/questions/scheme_id_spec.rb index 0b7c8da96..140b23020 100644 --- a/spec/models/form/lettings/questions/scheme_id_spec.rb +++ b/spec/models/form/lettings/questions/scheme_id_spec.rb @@ -90,13 +90,37 @@ RSpec.describe Form::Lettings::Questions::SchemeId, type: :model do end context "when a scheme with at least 1 location exists" do - before do - FactoryBot.create(:location, scheme:) + context "when the location is active" do + before do + FactoryBot.create(:location, startdate: Time.zone.yesterday, scheme:) + end + + it "has the correct answer_options based on the schemes the user's organisation owns or manages" do + expected_answer = { "" => "Select an option", scheme.id.to_s => scheme } + expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer) + end end - it "has the correct answer_options based on the schemes the user's organisation owns or manages" do - expected_answer = { "" => "Select an option", scheme.id.to_s => scheme } - expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer) + context "when the location is activating soon" do + before do + FactoryBot.create(:location, startdate: Time.zone.tomorrow, scheme:) + end + + it "has the correct answer_options based on the schemes the user's organisation owns or manages" do + expected_answer = { "" => "Select an option", scheme.id.to_s => scheme } + expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer) + end + end + + context "when the location is activating more than 2 weeks in the future" do + before do + FactoryBot.create(:location, startdate: Time.zone.today + 3.weeks, scheme:) + end + + it "has the correct answer_options based on the schemes the user's organisation owns or manages" do + expected_answer = { "" => "Select an option" } + expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer) + end end end diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index b9c747e9c..95761a3dc 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -78,7 +78,11 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -86,6 +90,8 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end end @@ -103,7 +109,11 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -111,6 +121,8 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end end @@ -130,7 +142,11 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -138,10 +154,12 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end end - context "with a location with no deactivation periods" do + context "with a location that is activating soon (has no deactivation periods)" do let(:scheme) { create(:scheme) } let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) } @@ -150,6 +168,8 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end it "produces an error when the date is before available_from date" do @@ -157,7 +177,39 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) + end + end + + context "with a deactivated scheme" do + let(:scheme) { create(:scheme) } + + before do + create(:location, scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 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.deactivated.startdate", name: scheme.service_name, date: "4 June 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.scheme.deactivated.scheme_id", name: scheme.service_name, date: "4 June 2022")) + end + + it "produces no error when tenancy start date is during an active scheme period" do + record.startdate = Time.zone.local(2022, 6, 1) + record.scheme = scheme + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end end @@ -175,7 +227,9 @@ RSpec.describe Validations::DateValidations do 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")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active scheme period" do @@ -183,6 +237,7 @@ RSpec.describe Validations::DateValidations do record.scheme = scheme date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end end @@ -202,7 +257,9 @@ RSpec.describe Validations::DateValidations do 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")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 September 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 September 2022")) end it "produces no error when tenancy start date is during an active scheme period" do @@ -210,6 +267,7 @@ RSpec.describe Validations::DateValidations do record.scheme = scheme date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty + expect(record.errors["scheme_id"]).to be_empty end end end diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 20f6b1d25..7e343f266 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -156,125 +156,198 @@ RSpec.describe Validations::SetupValidations do end describe "#validate_scheme" do - context "with a deactivated location" do + context "with a deactivated scheme" do let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } before do - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) - location.reload + create(:location, scheme:) + create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) + scheme.reload end - it "produces error when tenancy start date is during deactivated location period" do + it "produces error when tenancy start date is during deactivated scheme period" do record.startdate = Time.zone.local(2022, 7, 5) - record.location = location + record.scheme = scheme setup_validator.validate_scheme(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.scheme.deactivated.startdate", name: scheme.service_name, date: "4 June 2022")) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) + .to include(match I18n.t("validations.setup.startdate.scheme.deactivated.scheme_id", name: scheme.service_name, date: "4 June 2022")) end - it "produces no error when tenancy start date is during an active location period" do + it "produces no error when tenancy start date is during an active scheme period" do record.startdate = Time.zone.local(2022, 6, 1) - record.location = location + record.scheme = scheme setup_validator.validate_scheme(record) + expect(record.errors["startdate"]).to be_empty expect(record.errors["scheme_id"]).to be_empty end end - context "with a location that is reactivating soon" do + context "with a scheme that is reactivating soon" do let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } before do - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - location.reload + create(:location, scheme:) + 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 location period" do + it "produces error when tenancy start date is during deactivated scheme period" do record.startdate = Time.zone.local(2022, 7, 5) - record.location = location + record.scheme = scheme setup_validator.validate_scheme(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022")) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022")) end - it "produces no error when tenancy start date is during an active location period" do + it "produces no error when tenancy start date is during an active scheme period" do record.startdate = Time.zone.local(2022, 9, 1) - record.location = location + record.scheme = scheme setup_validator.validate_scheme(record) + expect(record.errors["startdate"]).to be_empty expect(record.errors["scheme_id"]).to be_empty end end - context "with a location with no deactivation periods" do - let(:scheme) { create(:scheme, created_at: Time.zone.local(2022, 10, 3)) } - let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) } + context "with a scheme that has many reactivations soon" do + let(:scheme) { create(:scheme) } - it "produces no error" do - record.startdate = Time.zone.local(2022, 10, 15) - record.location = location + before do + create(:location, scheme:) + 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["startdate"]) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 September 2022")) + expect(record.errors["scheme_id"]) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", 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["startdate"]).to be_empty expect(record.errors["scheme_id"]).to be_empty end + end - it "produces an error when the date is before available_from date" do - record.startdate = Time.zone.local(2022, 8, 15) + context "with a deactivated location" do + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:) } + + before do + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) + location.reload + end + + it "produces error when tenancy start date is during deactivated location period" do + record.startdate = Time.zone.local(2022, 7, 5) record.location = location setup_validator.validate_scheme(record) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) + end + + it "produces no error when tenancy start date is during an active location period" do + record.startdate = Time.zone.local(2022, 6, 1) + record.location = location + setup_validator.validate_scheme(record) + expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty end end - context "with a scheme that is reactivating soon" do - let(:scheme) { create(:scheme, created_at: Time.zone.local(2022, 4, 1)) } + context "with a location that is reactivating soon" do + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:) } before do - create(:location, scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - scheme.reload + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) + location.reload end - it "produces error when tenancy start date is during deactivated scheme period" do + it "produces error when tenancy start date is during deactivated location period" do record.startdate = Time.zone.local(2022, 7, 5) - record.scheme = scheme + record.location = location 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")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) end - it "produces no error when tenancy start date is during an active scheme period" do + it "produces no error when tenancy start date is during an active location period" do record.startdate = Time.zone.local(2022, 9, 1) - record.scheme = scheme + record.location = location setup_validator.validate_scheme(record) - expect(record.errors["scheme_id"]).to be_empty + expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty end end - context "with a scheme that has many reactivations soon" do - let(:scheme) { create(:scheme, created_at: Time.zone.local(2022, 4, 1)) } + context "with a location that has many reactivations soon" do + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:) } before do - create(:location, scheme:) - 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 + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) + location.reload end - it "produces error when tenancy start date is during deactivated scheme period" do + it "produces error when tenancy start date is during deactivated location period" do record.startdate = Time.zone.local(2022, 7, 5) - record.scheme = scheme + record.location = location 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")) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) end - it "produces no error when tenancy start date is during an active scheme period" do + it "produces no error when tenancy start date is during an active location period" do record.startdate = Time.zone.local(2022, 10, 1) - record.scheme = scheme + record.location = location setup_validator.validate_scheme(record) - expect(record.errors["scheme_id"]).to be_empty + expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + end + end + + context "with a location that is activating soon (has no deactivation periods)" do + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) } + + it "produces no error" do + record.startdate = Time.zone.local(2022, 10, 15) + record.location = location + setup_validator.validate_scheme(record) + expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + end + + it "produces an error when the date is before available_from date" do + record.startdate = Time.zone.local(2022, 8, 15) + record.location = location + setup_validator.validate_scheme(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) end end end @@ -293,14 +366,17 @@ RSpec.describe Validations::SetupValidations do record.startdate = Time.zone.local(2022, 7, 5) record.location = location setup_validator.validate_location(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022")) expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) + .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) end it "produces no error when tenancy start date is during an active location period" do record.startdate = Time.zone.local(2022, 6, 1) record.location = location setup_validator.validate_location(record) + expect(record.errors["startdate"]).to be_empty expect(record.errors["location_id"]).to be_empty end end @@ -318,19 +394,52 @@ RSpec.describe Validations::SetupValidations do record.startdate = Time.zone.local(2022, 7, 5) record.location = location setup_validator.validate_location(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022")) expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active location period" do record.startdate = Time.zone.local(2022, 9, 1) record.location = location setup_validator.validate_location(record) + expect(record.errors["startdate"]).to be_empty + expect(record.errors["location_id"]).to be_empty + end + end + + context "with a location that has many reactivations soon" do + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:) } + + before do + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) + location.reload + end + + it "produces error when tenancy start date is during deactivated location period" do + record.startdate = Time.zone.local(2022, 7, 5) + record.location = location + setup_validator.validate_location(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022")) + expect(record.errors["location_id"]) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) + end + + it "produces no error when tenancy start date is during an active location period" do + record.startdate = Time.zone.local(2022, 10, 1) + record.location = location + setup_validator.validate_location(record) + expect(record.errors["startdate"]).to be_empty expect(record.errors["location_id"]).to be_empty end end - context "with a location with no deactivation periods" do + context "with a location that is activating soon (has no deactivation periods)" do let(:scheme) { create(:scheme) } let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) } @@ -338,6 +447,7 @@ RSpec.describe Validations::SetupValidations do record.startdate = Time.zone.local(2022, 10, 15) record.location = location setup_validator.validate_location(record) + expect(record.errors["startdate"]).to be_empty expect(record.errors["location_id"]).to be_empty end @@ -345,8 +455,10 @@ RSpec.describe Validations::SetupValidations do record.startdate = Time.zone.local(2022, 8, 15) record.location = location setup_validator.validate_location(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022")) expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) end end end