From 6fd9cd2eaccb035b51eb1c88b21c3e9dfd9d92e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Fri, 7 Jan 2022 09:35:33 +0000 Subject: [PATCH] CDLC-783: Errors when a postcode is known but not provided --- app/models/case_log.rb | 11 +++++----- .../validations/property_validations.rb | 6 ++++++ config/forms/2021_2022.json | 2 +- spec/models/case_log_spec.rb | 21 +++++++------------ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 2426ecc0e..e88c109ee 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -222,7 +222,9 @@ private if property_postcode.blank? || postcode_known == "No" reset_location_fields else - self.la = get_la(property_postcode) + inferred_la = get_inferred_la(property_postcode) + self.is_la_inferred = inferred_la.present? + self.la = inferred_la if inferred_la.present? end if property_postcode.present? self.postcode = UKPostcode.parse(property_postcode).outcode @@ -254,15 +256,12 @@ private end end - def get_la(postcode) + def get_inferred_la(postcode) 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 + postcode_lookup.admin_district end - self.la = nil - self.is_la_inferred = false end def reset_location_fields diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index 828bceb81..3cef737e6 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -33,4 +33,10 @@ module Validations::PropertyValidations record.errors.add :unitletas, "Property cannot have a previous let type if it is being let as social housing for the first time" end end + + def validate_property_postcode(record) + if record.postcode_known == "Yes" && record.property_postcode.blank? + record.errors.add :property_postcode, "Enter a postcode in the correct format, for example AA1 1AA" + end + end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 97c15becb..dd232dda5 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1125,7 +1125,7 @@ }, "property_postcode": { "check_answer_label": "Postcode", - "header": "", + "header": "What is the property's postcode?", "hint_text": "", "type": "text", "width": 5 diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 90056cc98..d719d994e 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1134,14 +1134,9 @@ RSpec.describe Form, type: :model do expect(record_from_db["la"]).to eq("E08000003") end - 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, 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) + it "errors if the property postcode is emptied" do + expect { address_case_log.update!({ property_postcode: "" }) } + .to raise_error(ActiveRecord::RecordInvalid, /Enter a postcode in the correct format/) end it "correctly resets all fields if property postcode not known" do @@ -1154,7 +1149,7 @@ RSpec.describe Form, type: :model do 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 + it "changes the LA if property postcode changes from not known to known and provided" do address_case_log.update!({ postcode_known: "No" }) address_case_log.update!({ la: "Westminster" }) address_case_log.reload @@ -1164,13 +1159,13 @@ RSpec.describe Form, type: :model do 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.update!({ postcode_known: "Yes", property_postcode: "M1 1AD" }) 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") + expect(record_from_db["property_postcode"]).to eq("M1 1AD") + expect(address_case_log.la).to eq("Manchester") + expect(record_from_db["la"]).to eq("E08000003") end end