Browse Source

Modify location availability text and fix active scope

pull/2605/head
Manny Dinssa 2 years ago
parent
commit
8923db2d6a
  1. 38
      app/helpers/locations_helper.rb
  2. 13
      app/models/location.rb
  3. 16
      app/models/scheme.rb

38
app/helpers/locations_helper.rb

@ -100,25 +100,37 @@ private
ActivePeriod = Struct.new(:from, :to)
def location_active_periods(location)
periods = [ActivePeriod.new(location.available_from, nil)]
location_deactivation_periods = location_deactivation_periods(location)
scheme_deactivation_periods = scheme_deactivation_periods(location, location_deactivation_periods)
sorted_deactivation_periods = remove_nested_periods(location.location_deactivation_periods.sort_by(&:deactivation_date))
sorted_deactivation_periods.each do |deactivation|
periods.last.to = deactivation.deactivation_date
periods << ActivePeriod.new(deactivation.reactivation_date, nil)
combined_deactivation_periods = location_deactivation_periods + scheme_deactivation_periods
sorted_deactivation_periods = combined_deactivation_periods.sort_by(&:deactivation_date)
update_periods_with_deactivations(periods, sorted_deactivation_periods)
remove_overlapping_and_empty_periods(periods)
end
if location.scheme.scheme_deactivation_periods.any?
last_location_deactivation_date = sorted_deactivation_periods.last&.deactivation_date
scheme_periods = location.scheme.scheme_deactivation_periods
.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)
def location_deactivation_periods(location)
periods = remove_nested_periods(location.location_deactivation_periods.sort_by(&:deactivation_date))
periods.last&.deactivation_date if periods.last&.reactivation_date.nil?
periods
end
def scheme_deactivation_periods(location, location_deactivation_periods)
return [] unless location.scheme.scheme_deactivation_periods.any?
location_deactivation_date = location_deactivation_periods.last&.deactivation_date
periods = remove_nested_periods(location.scheme.scheme_deactivation_periods.sort_by(&:deactivation_date))
periods.select do |period|
period.deactivation_date >= location.available_from && (location_deactivation_date.nil? || period.deactivation_date <= location_deactivation_date)
end
end
remove_overlapping_and_empty_periods(periods)
def update_periods_with_deactivations(periods, sorted_deactivation_periods)
sorted_deactivation_periods.each do |deactivation|
periods.last.to = deactivation.deactivation_date
periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end
end
def remove_overlapping_and_empty_periods(periods)

13
app/models/location.rb

@ -113,13 +113,12 @@ 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(scheme: [:scheme_deactivation_periods]).reactivating_soon_by_scheme.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))
.where.not(id: incomplete.pluck(:id))
.where.not(id: activating_soon(date).pluck(:id))
.where(scheme: Scheme.active)
}
scope :visible, -> { where(discarded_at: nil) }
@ -183,6 +182,14 @@ class Location < ApplicationRecord
end
def status_at(date)
Rails.logger.debug "Checking status at #{date} for location #{id}"
Rails.logger.debug "Discarded at: #{discarded_at}"
Rails.logger.debug "Confirmed: #{confirmed}"
Rails.logger.debug "Scheme status: #{scheme.status_at(date)}"
Rails.logger.debug "Open deactivation: #{open_deactivation&.deactivation_date}"
Rails.logger.debug "Last deactivation before: #{last_deactivation_before(date)&.reactivation_date}"
Rails.logger.debug "Start date: #{startdate}"
return :deleted if discarded_at.present?
return :incomplete unless confirmed
return :deactivated if scheme.owning_organisation.status_at(date) == :deactivated ||

16
app/models/scheme.rb

@ -74,13 +74,14 @@ class Scheme < ApplicationRecord
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 > ?", date)
.where("scheme_deactivation_periods.deactivation_date <= ?", date)
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
}
scope :activating_soon, lambda {
where("schemes.startdate > ?", Time.zone.now)
scope :activating_soon, lambda { |date = Time.zone.now|
where("schemes.startdate > ?", date)
}
scope :active_status, lambda {
@ -92,6 +93,14 @@ class Scheme < ApplicationRecord
.where.not(id: activating_soon.pluck(:id))
}
scope :active, lambda { |date = Time.zone.now|
where.not(id: joins(:scheme_deactivation_periods).reactivating_soon(date).pluck(:id))
.where.not(id: incomplete.pluck(:id))
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
.where.not(id: joins(:owning_organisation).joins(:scheme_deactivation_periods).deactivated_directly(date).pluck(:id))
.where.not(id: activating_soon(date).pluck(:id))
}
scope :visible, -> { where(discarded_at: nil) }
validate :validate_confirmed
@ -301,6 +310,7 @@ class Scheme < ApplicationRecord
end
def has_active_locations?
# binding.pry
locations.active.exists?
end

Loading…
Cancel
Save