From 89120548bb2835cd7b2b89db7d8c9e2a9184a717 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Fri, 5 Aug 2022 11:44:44 +0100 Subject: [PATCH] Only consider active locations for routing (#814) * Only consider active locations for routing * Spec nil case --- .../derived_variables/case_log_variables.rb | 4 +-- app/models/location.rb | 2 ++ spec/factories/location.rb | 1 + spec/features/form/page_routing_spec.rb | 33 +++++++++++++++++++ spec/models/location_spec.rb | 18 ++++++++-- 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index edbece2a1..ea4df8009 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -8,7 +8,7 @@ module DerivedVariables::CaseLogVariables def scheme_has_multiple_locations? return false unless scheme - @scheme_locations_count ||= scheme.locations.size + @scheme_locations_count ||= scheme.locations.active.size @scheme_locations_count > 1 end @@ -188,7 +188,7 @@ private def reset_scheme_location! self.location = nil - if scheme && scheme.locations.size == 1 + if scheme && scheme.locations.active.size == 1 self.location = scheme.locations.first end end diff --git a/app/models/location.rb b/app/models/location.rb index c6f727f5c..fd2dde9fe 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -12,6 +12,8 @@ class Location < ApplicationRecord scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } 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("startdate <= ?", Time.zone.today).or(where(startdate: nil)) } + scope :active, -> { where(confirmed: true).and(started) } MOBILITY_TYPE = { "Wheelchair-user standard": "W", diff --git a/spec/factories/location.rb b/spec/factories/location.rb index 433eedd6f..a8418f8ba 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -8,6 +8,7 @@ FactoryBot.define do location_code { "E09000033" } location_admin_district { "Westminster" } startdate { Faker::Date.between(from: 6.months.ago, to: Time.zone.today) } + confirmed { true } scheme trait :export do postcode { "SW1A 2AA" } diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index abf65dad3..f3e6b39a5 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -111,4 +111,37 @@ RSpec.describe "Form Page Routing" do expect(find_field("case_log[startdate(1i)]").value).to eq(nil) end end + + context "when completing the setup section" do + context "with a supported housing log" do + let(:case_log) do + FactoryBot.create( + :case_log, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + needstype: 2, + ) + end + + context "with a scheme with only 1 active location" do + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } + let!(:active_location) { FactoryBot.create(:location, scheme:) } + + before do + FactoryBot.create(:location, scheme:, startdate: Time.zone.today + 20.days) + visit("/logs/#{case_log.id}/scheme") + select(scheme.service_name, from: "case_log[scheme_id]") + click_button("Save and continue") + end + + it "does not route to the scheme location question" do + expect(page).to have_current_path("/logs/#{case_log.id}/renewal") + end + + it "infers the scheme location" do + expect(case_log.reload.location_id).to eq(active_location.id) + end + end + end + end end diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index dccc4674b..0b932b915 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -72,8 +72,10 @@ RSpec.describe Location, type: :model do describe "scopes" do before do - FactoryBot.create(:location, name: "ABC", postcode: "NW1 8RR") - FactoryBot.create(:location, name: "XYZ", postcode: "SE1 6HJ") + FactoryBot.create(:location, name: "ABC", postcode: "NW1 8RR", startdate: Time.zone.today) + FactoryBot.create(:location, name: "XYZ", postcode: "SE1 6HJ", startdate: Time.zone.today + 1.day) + FactoryBot.create(:location, name: "GHQ", postcode: "EW1 7JK", startdate: Time.zone.today - 1.day, confirmed: false) + FactoryBot.create(:location, name: "GHQ", postcode: "EW1 7JK", startdate: nil) end context "when searching by name" do @@ -96,5 +98,17 @@ RSpec.describe Location, type: :model do expect(described_class.search_by("nw18rr").count).to eq(1) end end + + context "when filtering by started locations" do + it "returns only locations that started today or earlier" do + expect(described_class.started.count).to eq(3) + end + end + + context "when filtering by active locations" do + it "returns only locations that started today or earlier and have been confirmed" do + expect(described_class.active.count).to eq(2) + end + end end end