From 4191e4183d6134d80a5cab40498b251f5a0b4f9f Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:57:03 +0100 Subject: [PATCH] Handle scheme reactivating soon --- app/helpers/locations_helper.rb | 10 ++++------ app/models/location.rb | 19 ++++++++++++++----- app/models/scheme.rb | 5 +++-- app/models/scheme_deactivation_period.rb | 1 + 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index 7d2b6dee1..a95774d83 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -107,12 +107,10 @@ private periods << ActivePeriod.new(deactivation.reactivation_date, nil) end - if location.deactivated_by_scheme? || location.deactivating_soon_by_scheme? - scheme_periods = location.scheme.scheme_deactivation_periods.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) - end + scheme_periods = location.scheme.scheme_deactivation_periods.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) end remove_overlapping_and_empty_periods(periods) diff --git a/app/models/location.rb b/app/models/location.rb index bcc424d21..09d21a826 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -88,8 +88,13 @@ class Location < ApplicationRecord scope :reactivating_soon, lambda { |date = Time.zone.now| 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)) + .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| + merge(Scheme.reactivating_soon(date)) } scope :activating_soon, lambda { |date = Time.zone.now| @@ -97,7 +102,7 @@ class Location < ApplicationRecord } scope :active_status, lambda { - where.not(id: joins(:location_deactivation_periods).reactivating_soon.pluck(:id)) + where.not(id: joins(:location_deactivation_periods, scheme: [:scheme_deactivation_periods]).reactivating_soon.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)) @@ -108,7 +113,7 @@ class Location < ApplicationRecord } scope :active, lambda { |date = Time.zone.now| - where.not(id: joins(:location_deactivation_periods).reactivating_soon(date).pluck(:id)) + where.not(id: joins(:location_deactivation_periods, scheme: [:scheme_deactivation_periods]).reactivating_soon(date).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)) @@ -183,7 +188,7 @@ class Location < ApplicationRecord open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date || scheme.status_at(date) == :deactivated return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date || scheme.status_at(date) == :deactivating_soon return :activating_soon if startdate.present? && date < startdate - return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date + return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date || scheme.status_at(date) == :reactivating_soon :active end @@ -212,6 +217,10 @@ class Location < ApplicationRecord status == :deactivating_soon && scheme.status == :deactivating_soon end + def reactivating_soon_by_scheme? + status == :reactivating_soon && scheme.status == :reactivating_soon + end + def validate_postcode if !postcode&.match(POSTCODE_REGEXP) error_message = I18n.t("validations.postcode") diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 824efc6ee..c43a3f9f5 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -72,9 +72,10 @@ class Scheme < ApplicationRecord .where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id)) } - scope :reactivating_soon, lambda { + scope :reactivating_soon, lambda { |date = Time.zone.now| + merge(SchemeDeactivationPeriod.deactivations_with_reactivation) where.not("scheme_deactivation_periods.reactivation_date IS NULL") - .where("scheme_deactivation_periods.reactivation_date > ?", Time.zone.now) + .where("scheme_deactivation_periods.reactivation_date > ?", date) .where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id)) } diff --git a/app/models/scheme_deactivation_period.rb b/app/models/scheme_deactivation_period.rb index 176d15211..cb27534f7 100644 --- a/app/models/scheme_deactivation_period.rb +++ b/app/models/scheme_deactivation_period.rb @@ -48,4 +48,5 @@ class SchemeDeactivationPeriod < ApplicationRecord attr_accessor :deactivation_date_type, :reactivation_date_type scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) } + scope :deactivations_with_reactivation, -> { where.not(reactivation_date: nil) } end