diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a5cc2eff7..b3dc8b109 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,4 +1,5 @@ require "postcodes_io" +require "timeout" class CaseLogValidator < ActiveModel::Validator # Validations methods need to be called 'validate_' to run on model save @@ -189,10 +190,6 @@ private end def set_derived_fields - if property_postcode.present? - self.postcode = UKPostcode.parse(property_postcode).outcode - self.postcod2 = UKPostcode.parse(property_postcode).incode - end if previous_postcode.present? self.ppostc1 = UKPostcode.parse(previous_postcode).outcode self.ppostc2 = UKPostcode.parse(previous_postcode).incode @@ -212,7 +209,17 @@ private self.renttype = RENT_TYPE_MAPPING[rent_type] self.lettype = "#{renttype} #{needstype} #{owning_organisation['Org type']}" if renttype.present? && needstype.present? && owning_organisation["Org type"].present? self.is_la_inferred = false if is_la_inferred.nil? - self.la = get_la(property_postcode) + if property_postcode.blank? || postcode_known == "No" + reset_location_fields + else + self.la = get_la(property_postcode) + end + if property_postcode.present? + self.postcode = UKPostcode.parse(property_postcode).outcode + self.postcod2 = UKPostcode.parse(property_postcode).incode + else + self.postcode = self.postcod2 = nil + end self.totchild = get_totchild self.totelder = get_totelder self.totadult = get_totadult @@ -240,17 +247,24 @@ private end def get_la(postcode) - if postcode.present? - postcode_lookup = PIO.lookup(postcode) - if postcode_lookup && postcode_lookup.info.present? - self.is_la_inferred = true - return postcode_lookup.admin_district - end + postcode_lookup = nil + Timeout.timeout(5) { postcode_lookup = PIO.lookup(postcode) } + if postcode_lookup && postcode_lookup.info.present? + self.is_la_inferred = true + return postcode_lookup.admin_district end self.la = nil self.is_la_inferred = false end + def reset_location_fields + if is_la_inferred == true + self.la = nil + end + self.is_la_inferred = false + self.property_postcode = nil + end + def all_fields_completed? mandatory_fields.none? { |_key, val| val.nil? } end diff --git a/app/models/constants/case_log.rb b/app/models/constants/case_log.rb index a7a5322a0..d71b0cccc 100644 --- a/app/models/constants/case_log.rb +++ b/app/models/constants/case_log.rb @@ -489,7 +489,7 @@ module Constants::CaseLog "Tewkesbury" => "E07000083", "Basingstoke and Deane" => "E07000084", "East Hampshire" => "E07000085", - "King’s Lynn and West Norfolk" => "E07000146", + "King's Lynn and West Norfolk" => "E07000146", "Eastleigh" => "E07000086", "North Norfolk" => "E07000147", "Norwich" => "E07000148", diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 09485844b..dbe2e2635 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1294,7 +1294,7 @@ "133": "Isles of Scilly", "134": "Islington", "135": "Kensington & Chelsea", - "136": "Kings Lynn & West Norfolk", + "136": "King's Lynn and West Norfolk", "137": "Kingston-upon-Hull", "138": "Kingston-upon-Thames", "139": "Kirklees", diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 7b82ecd90..5326b73c0 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1031,6 +1031,7 @@ RSpec.describe Form, type: :model do CaseLog.create({ managing_organisation: organisation, owning_organisation: organisation, + postcode_known: "Yes", property_postcode: "M1 1AE", }) end @@ -1043,21 +1044,44 @@ RSpec.describe Form, type: :model do expect(record_from_db["la"]).to eq("E08000003") end - it "correctly resets all fields" do + it "correctly resets all fields if property postcode is empty" do + address_case_log.update!({ property_postcode: "" }) address_case_log.reload - record_from_db = ActiveRecord::Base.connection.execute("select la from case_logs where id=#{address_case_log.id}").to_a[0] - expect(address_case_log.la).to eq("Manchester") - expect(record_from_db["la"]).to eq("E08000003") + record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0] + expect(record_from_db["property_postcode"]).to eq(nil) + expect(address_case_log.la).to eq(nil) + expect(record_from_db["la"]).to eq(nil) + end - address_case_log.update!({ property_postcode: "" }) + it "correctly resets all fields if property postcode not known" do + address_case_log.update!({ postcode_known: "No" }) address_case_log.reload record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0] - expect(record_from_db["property_postcode"]).to eq("") + expect(record_from_db["property_postcode"]).to eq(nil) expect(address_case_log.la).to eq(nil) expect(record_from_db["la"]).to eq(nil) end + + it "keeps the LA if property postcode changes from not known to known and not provided" do + address_case_log.update!({ postcode_known: "No" }) + address_case_log.update!({ la: "Westminster" }) + address_case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0] + expect(record_from_db["property_postcode"]).to eq(nil) + expect(address_case_log.la).to eq("Westminster") + expect(record_from_db["la"]).to eq("E09000033") + + address_case_log.update!({ postcode_known: "Yes" }) + address_case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0] + expect(record_from_db["property_postcode"]).to eq(nil) + expect(address_case_log.la).to eq("Westminster") + expect(record_from_db["la"]).to eq("E09000033") + end end context "household members derived vars" do