Browse Source

Only consider active locations for routing (#814)

* Only consider active locations for routing

* Spec nil case
pull/818/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
89120548bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/derived_variables/case_log_variables.rb
  2. 2
      app/models/location.rb
  3. 1
      spec/factories/location.rb
  4. 33
      spec/features/form/page_routing_spec.rb
  5. 18
      spec/models/location_spec.rb

4
app/models/derived_variables/case_log_variables.rb

@ -8,7 +8,7 @@ module DerivedVariables::CaseLogVariables
def scheme_has_multiple_locations? def scheme_has_multiple_locations?
return false unless scheme return false unless scheme
@scheme_locations_count ||= scheme.locations.size @scheme_locations_count ||= scheme.locations.active.size
@scheme_locations_count > 1 @scheme_locations_count > 1
end end
@ -188,7 +188,7 @@ private
def reset_scheme_location! def reset_scheme_location!
self.location = nil self.location = nil
if scheme && scheme.locations.size == 1 if scheme && scheme.locations.active.size == 1
self.location = scheme.locations.first self.location = scheme.locations.first
end end
end end

2
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_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") }
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } 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 = { MOBILITY_TYPE = {
"Wheelchair-user standard": "W", "Wheelchair-user standard": "W",

1
spec/factories/location.rb

@ -8,6 +8,7 @@ FactoryBot.define do
location_code { "E09000033" } location_code { "E09000033" }
location_admin_district { "Westminster" } location_admin_district { "Westminster" }
startdate { Faker::Date.between(from: 6.months.ago, to: Time.zone.today) } startdate { Faker::Date.between(from: 6.months.ago, to: Time.zone.today) }
confirmed { true }
scheme scheme
trait :export do trait :export do
postcode { "SW1A 2AA" } postcode { "SW1A 2AA" }

33
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) expect(find_field("case_log[startdate(1i)]").value).to eq(nil)
end end
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 end

18
spec/models/location_spec.rb

@ -72,8 +72,10 @@ RSpec.describe Location, type: :model do
describe "scopes" do describe "scopes" do
before do before do
FactoryBot.create(:location, name: "ABC", postcode: "NW1 8RR") FactoryBot.create(:location, name: "ABC", postcode: "NW1 8RR", startdate: Time.zone.today)
FactoryBot.create(:location, name: "XYZ", postcode: "SE1 6HJ") 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 end
context "when searching by name" do 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) expect(described_class.search_by("nw18rr").count).to eq(1)
end end
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
end end

Loading…
Cancel
Save