From 92435394831a7e911f199624f1812f9bd8fb9384 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 17 Mar 2023 15:55:21 +0000 Subject: [PATCH] Default to location code if LocalAuthority is not found by location code --- app/models/lettings_log.rb | 2 +- app/models/location.rb | 2 +- spec/models/lettings_log_spec.rb | 13 +++++++++++++ spec/models/location_spec.rb | 13 +++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 764b23f9a..4524ead12 100644 --- a/app/models/lettings_log.rb +++ b/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 diff --git a/app/models/location.rb b/app/models/location.rb index 7002ce5a9..c33caee4e 100644 --- a/app/models/location.rb +++ b/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 diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index e2431ff92..57b5de21a 100644 --- a/spec/models/lettings_log_spec.rb +++ b/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 diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index ceb87121f..eded7d1ee 100644 --- a/spec/models/location_spec.rb +++ b/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