Browse Source

Handle scheme reactivating soon

pull/2605/head
Manny Dinssa 2 years ago
parent
commit
4191e4183d
  1. 2
      app/helpers/locations_helper.rb
  2. 15
      app/models/location.rb
  3. 5
      app/models/scheme.rb
  4. 1
      app/models/scheme_deactivation_period.rb

2
app/helpers/locations_helper.rb

@ -107,13 +107,11 @@ private
periods << ActivePeriod.new(deactivation.reactivation_date, nil) periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end end
if location.deactivated_by_scheme? || location.deactivating_soon_by_scheme?
scheme_periods = location.scheme.scheme_deactivation_periods.sort_by(&:deactivation_date) scheme_periods = location.scheme.scheme_deactivation_periods.sort_by(&:deactivation_date)
scheme_periods.each do |scheme_period| scheme_periods.each do |scheme_period|
periods.last.to = scheme_period.deactivation_date periods.last.to = scheme_period.deactivation_date
periods << ActivePeriod.new(scheme_period.reactivation_date, nil) periods << ActivePeriod.new(scheme_period.reactivation_date, nil)
end end
end
remove_overlapping_and_empty_periods(periods) remove_overlapping_and_empty_periods(periods)
end end

15
app/models/location.rb

@ -90,6 +90,11 @@ class Location < ApplicationRecord
where.not("location_deactivation_periods.reactivation_date IS NULL") where.not("location_deactivation_periods.reactivation_date IS NULL")
.where("location_deactivation_periods.reactivation_date > ?", date) .where("location_deactivation_periods.reactivation_date > ?", date)
.where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) .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| scope :activating_soon, lambda { |date = Time.zone.now|
@ -97,7 +102,7 @@ class Location < ApplicationRecord
} }
scope :active_status, lambda { 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(: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: [:scheme_deactivation_periods]).merge(Location.deactivated_by_scheme).pluck(:id))
.where.not(id: joins(scheme: [:owning_organisation]).merge(Location.deactivated_by_organisation).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| 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(: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: [: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)) .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 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 :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 :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 :active
end end
@ -212,6 +217,10 @@ class Location < ApplicationRecord
status == :deactivating_soon && scheme.status == :deactivating_soon status == :deactivating_soon && scheme.status == :deactivating_soon
end end
def reactivating_soon_by_scheme?
status == :reactivating_soon && scheme.status == :reactivating_soon
end
def validate_postcode def validate_postcode
if !postcode&.match(POSTCODE_REGEXP) if !postcode&.match(POSTCODE_REGEXP)
error_message = I18n.t("validations.postcode") error_message = I18n.t("validations.postcode")

5
app/models/scheme.rb

@ -72,9 +72,10 @@ class Scheme < ApplicationRecord
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id)) .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.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)) .where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
} }

1
app/models/scheme_deactivation_period.rb

@ -48,4 +48,5 @@ class SchemeDeactivationPeriod < ApplicationRecord
attr_accessor :deactivation_date_type, :reactivation_date_type attr_accessor :deactivation_date_type, :reactivation_date_type
scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) } scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) }
scope :deactivations_with_reactivation, -> { where.not(reactivation_date: nil) }
end end

Loading…
Cancel
Save