Browse Source

CDLC-783: Errors when a postcode is known but not provided

pull/196/head
Stéphane Meny 3 years ago
parent
commit
6fd9cd2eac
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 11
      app/models/case_log.rb
  2. 6
      app/models/validations/property_validations.rb
  3. 2
      config/forms/2021_2022.json
  4. 21
      spec/models/case_log_spec.rb

11
app/models/case_log.rb

@ -222,7 +222,9 @@ private
if property_postcode.blank? || postcode_known == "No" if property_postcode.blank? || postcode_known == "No"
reset_location_fields reset_location_fields
else 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 end
if property_postcode.present? if property_postcode.present?
self.postcode = UKPostcode.parse(property_postcode).outcode self.postcode = UKPostcode.parse(property_postcode).outcode
@ -254,15 +256,12 @@ private
end end
end end
def get_la(postcode) def get_inferred_la(postcode)
postcode_lookup = nil postcode_lookup = nil
Timeout.timeout(5) { 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 postcode_lookup.admin_district
return postcode_lookup.admin_district
end end
self.la = nil
self.is_la_inferred = false
end end
def reset_location_fields def reset_location_fields

6
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" 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
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 end

2
config/forms/2021_2022.json

@ -1125,7 +1125,7 @@
}, },
"property_postcode": { "property_postcode": {
"check_answer_label": "Postcode", "check_answer_label": "Postcode",
"header": "", "header": "What is the property's postcode?",
"hint_text": "", "hint_text": "",
"type": "text", "type": "text",
"width": 5 "width": 5

21
spec/models/case_log_spec.rb

@ -1134,14 +1134,9 @@ 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 if property postcode is empty" do it "errors if the property postcode is emptied" do
address_case_log.update!({ property_postcode: "" }) expect { address_case_log.update!({ property_postcode: "" }) }
address_case_log.reload .to raise_error(ActiveRecord::RecordInvalid, /Enter a postcode in the correct format/)
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 end
it "correctly resets all fields if property postcode not known" do 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) 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 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!({ postcode_known: "No" })
address_case_log.update!({ la: "Westminster" }) address_case_log.update!({ la: "Westminster" })
address_case_log.reload address_case_log.reload
@ -1164,13 +1159,13 @@ RSpec.describe Form, type: :model do
expect(address_case_log.la).to eq("Westminster") expect(address_case_log.la).to eq("Westminster")
expect(record_from_db["la"]).to eq("E09000033") 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 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(nil) expect(record_from_db["property_postcode"]).to eq("M1 1AD")
expect(address_case_log.la).to eq("Westminster") expect(address_case_log.la).to eq("Manchester")
expect(record_from_db["la"]).to eq("E09000033") expect(record_from_db["la"]).to eq("E08000003")
end end
end end

Loading…
Cancel
Save