From 9b0cbac406e60a7a93e499f269d2102d3adbf59c Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Thu, 25 Jul 2024 15:26:30 +0100 Subject: [PATCH] CLDC 2954 handle schemes with only deactivated locations (#2510) --- app/models/location.rb | 27 ++++--- app/models/scheme.rb | 10 +++ app/models/validations/setup_validations.rb | 19 ++--- app/models/validations/shared_validations.rb | 9 +++ app/views/schemes/_scheme_list.html.erb | 7 +- app/views/schemes/show.html.erb | 3 + config/locales/en.yml | 7 +- .../validations/setup_validations_spec.rb | 79 +++++++++++++++++++ 8 files changed, 139 insertions(+), 22 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index dfcbef41b..2b652c659 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -22,7 +22,6 @@ class Location < ApplicationRecord scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } scope :started, -> { where("locations.startdate <= ?", Time.zone.today).or(where(startdate: nil)) } - scope :active, -> { where(confirmed: true).and(started) } scope :started_in_2_weeks, -> { where("locations.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) } @@ -62,25 +61,25 @@ class Location < ApplicationRecord merge(Organisation.filter_by_inactive) } - scope :deactivated_directly, lambda { + scope :deactivated_directly, lambda { |date = Time.zone.now| merge(LocationDeactivationPeriod.deactivations_without_reactivation) - .where("location_deactivation_periods.deactivation_date <= ?", Time.zone.now) + .where("location_deactivation_periods.deactivation_date <= ?", date) } - scope :deactivating_soon, lambda { + scope :deactivating_soon, lambda { |date = Time.zone.now| merge(LocationDeactivationPeriod.deactivations_without_reactivation) - .where("location_deactivation_periods.deactivation_date > ?", Time.zone.now) + .where("location_deactivation_periods.deactivation_date > ?", date) .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) } - scope :reactivating_soon, lambda { + scope :reactivating_soon, lambda { |date = Time.zone.now| where.not("location_deactivation_periods.reactivation_date IS NULL") - .where("location_deactivation_periods.reactivation_date > ?", Time.zone.now) + .where("location_deactivation_periods.reactivation_date > ?", date) .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) } - scope :activating_soon, lambda { - where("locations.startdate > ?", Time.zone.now) + scope :activating_soon, lambda { |date = Time.zone.now| + where("locations.startdate > ?", date) } scope :active_status, lambda { @@ -92,6 +91,14 @@ class Location < ApplicationRecord .where.not(id: activating_soon.pluck(:id)) } + scope :active, lambda { |date = Time.zone.now| + where.not(id: joins(:location_deactivation_periods).reactivating_soon(date).pluck(:id)) + .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) + .where.not(id: joins(:location_deactivation_periods).deactivated_directly(date).pluck(:id)) + .where.not(id: incomplete.pluck(:id)) + .where.not(id: activating_soon(date).pluck(:id)) + } + scope :visible, -> { where(discarded_at: nil) } LOCAL_AUTHORITIES = LocalAuthority.all.map { |la| [la.name, la.code] }.to_h @@ -157,9 +164,9 @@ class Location < ApplicationRecord return :incomplete unless confirmed return :deactivated if scheme.owning_organisation.status_at(date) == :deactivated || open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date + return :activating_soon if startdate.present? && date < startdate return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date - return :activating_soon if startdate.present? && date < startdate :active end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 5b95ed98a..07ec14731 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -295,6 +295,16 @@ class Scheme < ApplicationRecord status == :active end + def has_active_locations? + locations.active.exists? + end + + def has_active_locations_on_date?(date) + return false unless date + + locations.active(date).exists? + end + def reactivating_soon? status == :reactivating_soon end diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index b06217ad5..528126ab7 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -72,15 +72,6 @@ module Validations::SetupValidations end end - def validate_location(record) - location_during_startdate_validation(record) - - if record.location&.status == :incomplete - record.errors.add :location_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") - record.errors.add :scheme_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") - end - end - def validate_scheme_has_confirmed_locations_validation(record) return unless record.scheme @@ -95,6 +86,16 @@ module Validations::SetupValidations end scheme_during_startdate_validation(record) + tenancy_startdate_with_scheme_locations(record) + end + + def validate_location(record) + location_during_startdate_validation(record) + + if record.location&.status == :incomplete + record.errors.add :location_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") + record.errors.add :scheme_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") + end end def validate_managing_organisation_data_sharing_agremeent_signed(record) diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index f3b0a9c34..ab9f9d7a8 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -87,6 +87,7 @@ module Validations::SharedValidations 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 :startdate, I18n.t("validations.setup.startdate.scheme.#{scope}.startdate", name: record.scheme.service_name, date:, deactivation_date:) @@ -112,6 +113,14 @@ module Validations::SharedValidations { scope: status, date: date&.to_formatted_s(:govuk_date), deactivation_date: closest_reactivation&.deactivation_date&.to_formatted_s(:govuk_date) } end + def tenancy_startdate_with_scheme_locations(record) + return if record.scheme.blank? || record.startdate.blank? + return if record.scheme.has_active_locations_on_date?(record.startdate) + + record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme.locations_inactive.startdate", name: record.scheme.service_name) + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme.locations_inactive.scheme_id", name: record.scheme.service_name) + end + def shared_validate_partner_count(record, max_people) return if record.form.start_year_after_2024? diff --git a/app/views/schemes/_scheme_list.html.erb b/app/views/schemes/_scheme_list.html.erb index 83908d85b..967295236 100644 --- a/app/views/schemes/_scheme_list.html.erb +++ b/app/views/schemes/_scheme_list.html.erb @@ -26,7 +26,12 @@ <% row.with_cell(text: scheme.owning_organisation&.name) %> <% row.with_cell(text: scheme.id_to_display) %> <% row.with_cell(text: scheme.locations.visible&.count) %> - <% row.with_cell(text: status_tag_from_resource(scheme)) %> + <% row.with_cell do %> + <%= status_tag_from_resource(scheme) %> + <% if scheme.active? && !scheme.has_active_locations? %> + <%= content_tag(:div, "No currently active locations", class: "app-!-colour-muted", style: "margin-top: 10px") %> + <% end %> + <% end %> <% end %> <% end %> <% end %> diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 1aefadef5..6cefa5847 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -26,6 +26,9 @@