From 1cb525c618d27d56450477b05a161c86eb525cd7 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 16 Dec 2021 08:26:43 +0000 Subject: [PATCH] reset location fields when postode not known --- app/models/case_log.rb | 27 +++++++++++++++++++-------- app/models/constants/case_log.rb | 2 +- config/forms/2021_2022.json | 2 +- spec/models/case_log_spec.rb | 3 ++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 98ffcea6d..3ca7e8498 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -213,7 +213,12 @@ 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 + self.totchild = get_totchild self.totelder = get_totelder self.totadult = get_totadult @@ -241,18 +246,24 @@ private end def get_la(postcode) - if postcode.present? - 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 + 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..71a7c2383 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 @@ -1054,7 +1055,7 @@ RSpec.describe Form, type: :model do 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