Browse Source

Add timeout for postcode lookup (#170)

* Add timeout for postcode lookup

* reset location fields when postode not known

* Reset partial postcodes after setting the main postcode field and add tests
pull/172/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
e09e804d6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      app/models/case_log.rb
  2. 2
      app/models/constants/case_log.rb
  3. 2
      config/forms/2021_2022.json
  4. 36
      spec/models/case_log_spec.rb

36
app/models/case_log.rb

@ -1,4 +1,5 @@
require "postcodes_io" require "postcodes_io"
require "timeout"
class CaseLogValidator < ActiveModel::Validator class CaseLogValidator < ActiveModel::Validator
# Validations methods need to be called 'validate_' to run on model save # Validations methods need to be called 'validate_' to run on model save
@ -189,10 +190,6 @@ private
end end
def set_derived_fields 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? if previous_postcode.present?
self.ppostc1 = UKPostcode.parse(previous_postcode).outcode self.ppostc1 = UKPostcode.parse(previous_postcode).outcode
self.ppostc2 = UKPostcode.parse(previous_postcode).incode self.ppostc2 = UKPostcode.parse(previous_postcode).incode
@ -212,7 +209,17 @@ private
self.renttype = RENT_TYPE_MAPPING[rent_type] 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.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.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.totchild = get_totchild
self.totelder = get_totelder self.totelder = get_totelder
self.totadult = get_totadult self.totadult = get_totadult
@ -240,17 +247,24 @@ private
end end
def get_la(postcode) def get_la(postcode)
if postcode.present? postcode_lookup = nil
postcode_lookup = PIO.lookup(postcode) Timeout.timeout(5) { postcode_lookup = PIO.lookup(postcode) }
if postcode_lookup && postcode_lookup.info.present? if postcode_lookup && postcode_lookup.info.present?
self.is_la_inferred = true self.is_la_inferred = true
return postcode_lookup.admin_district return postcode_lookup.admin_district
end
end end
self.la = nil self.la = nil
self.is_la_inferred = false self.is_la_inferred = false
end 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? def all_fields_completed?
mandatory_fields.none? { |_key, val| val.nil? } mandatory_fields.none? { |_key, val| val.nil? }
end end

2
app/models/constants/case_log.rb

@ -489,7 +489,7 @@ module Constants::CaseLog
"Tewkesbury" => "E07000083", "Tewkesbury" => "E07000083",
"Basingstoke and Deane" => "E07000084", "Basingstoke and Deane" => "E07000084",
"East Hampshire" => "E07000085", "East Hampshire" => "E07000085",
"Kings Lynn and West Norfolk" => "E07000146", "King's Lynn and West Norfolk" => "E07000146",
"Eastleigh" => "E07000086", "Eastleigh" => "E07000086",
"North Norfolk" => "E07000147", "North Norfolk" => "E07000147",
"Norwich" => "E07000148", "Norwich" => "E07000148",

2
config/forms/2021_2022.json

@ -1294,7 +1294,7 @@
"133": "Isles of Scilly", "133": "Isles of Scilly",
"134": "Islington", "134": "Islington",
"135": "Kensington & Chelsea", "135": "Kensington & Chelsea",
"136": "Kings Lynn & West Norfolk", "136": "King's Lynn and West Norfolk",
"137": "Kingston-upon-Hull", "137": "Kingston-upon-Hull",
"138": "Kingston-upon-Thames", "138": "Kingston-upon-Thames",
"139": "Kirklees", "139": "Kirklees",

36
spec/models/case_log_spec.rb

@ -1031,6 +1031,7 @@ RSpec.describe Form, type: :model do
CaseLog.create({ CaseLog.create({
managing_organisation: organisation, managing_organisation: organisation,
owning_organisation: organisation, owning_organisation: organisation,
postcode_known: "Yes",
property_postcode: "M1 1AE", property_postcode: "M1 1AE",
}) })
end end
@ -1043,21 +1044,44 @@ RSpec.describe Form, type: :model do
expect(record_from_db["la"]).to eq("E08000003") expect(record_from_db["la"]).to eq("E08000003")
end 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 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] record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(address_case_log.la).to eq("Manchester") expect(record_from_db["property_postcode"]).to eq(nil)
expect(record_from_db["la"]).to eq("E08000003") 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 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] 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(address_case_log.la).to eq(nil)
expect(record_from_db["la"]).to eq(nil) expect(record_from_db["la"]).to eq(nil)
end 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 end
context "household members derived vars" do context "household members derived vars" do

Loading…
Cancel
Save