diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index bc80d99b8..1c5b18cdc 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -107,12 +107,15 @@ private periods << ActivePeriod.new(deactivation.reactivation_date, nil) end + last_location_deactivation_date = sorted_deactivation_periods.last&.deactivation_date scheme_periods = location.scheme.scheme_deactivation_periods - .where("deactivation_date > ? OR reactivation_date > ?", location.available_from, location.available_from) + .where("deactivation_date > ? AND (reactivation_date < ? OR reactivation_date IS NULL)", location.available_from, last_location_deactivation_date || Time.zone.now) .sort_by(&:deactivation_date) - scheme_periods.each do |scheme_period| - periods.last.to = scheme_period.deactivation_date - periods << ActivePeriod.new(scheme_period.reactivation_date, nil) + if scheme_periods.any? + scheme_periods.each do |scheme_period| + periods.last.to = scheme_period.deactivation_date + periods << ActivePeriod.new(scheme_period.reactivation_date, nil) + end end remove_overlapping_and_empty_periods(periods) diff --git a/app/models/location.rb b/app/models/location.rb index 90433a96e..6e32a608d 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -90,7 +90,6 @@ class Location < ApplicationRecord where.not("location_deactivation_periods.reactivation_date IS NULL") .where("location_deactivation_periods.reactivation_date > ?", date) .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) - .or(reactivating_soon_by_scheme(date)) } scope :reactivating_soon_by_scheme, lambda { |date = Time.zone.now| @@ -102,7 +101,8 @@ class Location < ApplicationRecord } scope :active_status, lambda { - where.not(id: joins(:location_deactivation_periods, scheme: [:scheme_deactivation_periods]).reactivating_soon.pluck(:id)) + where.not(id: joins(:location_deactivation_periods).reactivating_soon.pluck(:id)) + .where.not(id: joins(scheme: [:scheme_deactivation_periods]).reactivating_soon_by_scheme.pluck(:id)) .where.not(id: joins(:location_deactivation_periods).merge(Location.deactivated_directly).pluck(:id)) .where.not(id: joins(scheme: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme).pluck(:id)) .where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).pluck(:id)) @@ -113,7 +113,8 @@ class Location < ApplicationRecord } scope :active, lambda { |date = Time.zone.now| - where.not(id: joins(:location_deactivation_periods, scheme: [:scheme_deactivation_periods]).reactivating_soon(date).pluck(:id)) + where.not(id: joins(:location_deactivation_periods).reactivating_soon(date).pluck(:id)) + .where.not(id: joins(scheme: [:scheme_deactivation_periods]).reactivating_soon_by_scheme.pluck(:id)) .where.not(id: joins(:location_deactivation_periods).merge(Location.deactivated_directly(date)).pluck(:id)) .where.not(id: joins(scheme: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme(date)).pluck(:id)) .where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).pluck(:id)) diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 1c6b32cf8..2be4ffb60 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -2534,14 +2534,18 @@ RSpec.describe SchemesController, type: :request do it "redirects to the confirmation page" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} logs") + expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} log") end end - context "and no affected logs" do + fcontext "and no affected logs" do let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) } - it "redirects to the location page and updates the deactivation period" do + before do + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 5), reactivation_date: nil, location:) + end + + it "redirects to the scheme page and updates the deactivation period" do follow_redirect! follow_redirect! follow_redirect! @@ -2561,14 +2565,18 @@ RSpec.describe SchemesController, type: :request do it "redirects to the confirmation page" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} logs") + expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} log") end end context "and no affected logs" do let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) } - it "redirects to the location page and updates the deactivation period" do + before do + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 5), reactivation_date: nil, location:) + end + + it "redirects to the scheme page and updates the deactivation period" do follow_redirect! follow_redirect! follow_redirect! @@ -2752,6 +2760,10 @@ RSpec.describe SchemesController, type: :request do let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2023" } } } let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) } + before do + create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 5), reactivation_date: nil, location:) + end + it "redirects to the scheme page and updates the existing deactivation period" do follow_redirect! follow_redirect! @@ -2772,7 +2784,7 @@ RSpec.describe SchemesController, type: :request do it "redirects to the confirmation page" do follow_redirect! expect(response).to have_http_status(:ok) - expect(page).to have_content("This change will affect 1 logs") + expect(page).to have_content("This change will affect 1 log") end end end