Browse Source

Default to location code if LocalAuthority is not found by location code

pull/1439/head
Kat 3 years ago
parent
commit
9243539483
  1. 2
      app/models/lettings_log.rb
  2. 2
      app/models/location.rb
  3. 13
      spec/models/lettings_log_spec.rb
  4. 13
      spec/models/location_spec.rb

2
app/models/lettings_log.rb

@ -84,7 +84,7 @@ class LettingsLog < Log
def la
if location
location.linked_local_authorities.active(form.start_date).first&.code
location.linked_local_authorities.active(form.start_date).first&.code || location.location_code
else
super
end

2
app/models/location.rb

@ -132,7 +132,7 @@ class Location < ApplicationRecord
def linked_local_authorities
la = LocalAuthority.find_by(code: location_code)
return [] unless la
return LocalAuthority.none unless la
LocalAuthority.where(id: [la.id] + la.linked_local_authority_ids)
end

13
spec/models/lettings_log_spec.rb

@ -2033,6 +2033,19 @@ RSpec.describe LettingsLog do
end
end
end
context "and the location no local authorities associated with the location_code" do
before do
location.update!(location_code: "E01231231")
lettings_log.update!(location:)
end
it "returns the correct la" do
expect(location.location_code).to eq("E01231231")
expect(lettings_log["location_id"]).to eq(location.id)
expect(lettings_log.la).to eq("E01231231")
end
end
end
context "and not renewal" do

13
spec/models/location_spec.rb

@ -32,6 +32,19 @@ RSpec.describe Location, type: :model do
expect(location.linked_local_authorities.active(Time.zone.local(2022, 4, 1)).first.code).to eq("E07000030")
expect(location.linked_local_authorities.active(Time.zone.local(2023, 4, 1)).first.code).to eq("E06000063")
end
context "when location_code is no in LocalAuthorities table" do
before do
stub_request(:get, /api.postcodes.io\/postcodes\/CA101AA/)
.to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E01231231"}}}', headers: {})
end
it "defaults for location code for la" do
location.update!(postcode: "CA10 1AA")
expect(location.linked_local_authorities.count).to eq(0)
expect(location.location_code).to eq("E01231231")
end
end
end
describe "#postcode" do

Loading…
Cancel
Save