diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 0567342b9..841c02503 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -38,8 +38,10 @@ class CaseLog < ApplicationRecord validates_with CaseLogValidator before_validation :process_postcode_changes!, if: :property_postcode_changed? + before_validation :process_previous_postcode_changes!, if: :previous_postcode_changed? before_validation :reset_invalidated_dependent_fields! before_validation :reset_location_fields!, unless: :postcode_known? + before_validation :reset_previous_location_fields!, unless: :previous_postcode_known? before_validation :set_derived_fields! before_save :update_status! @@ -135,6 +137,7 @@ class CaseLog < ApplicationRecord enum needstype: NEEDS_TYPE, _suffix: true enum lettype: LET_TYPE, _suffix: true enum postcode_known: POLAR, _suffix: true + enum previous_postcode_known: POLAR, _suffix: true enum la_known: POLAR, _suffix: true enum net_income_known: NET_INCOME_KNOWN, _suffix: true enum household_charge: POLAR, _suffix: true @@ -180,6 +183,10 @@ class CaseLog < ApplicationRecord postcode_known == "Yes" end + def previous_postcode_known? + previous_postcode_known == "Yes" + end + def weekly_net_income return unless earnings && incfreq @@ -258,24 +265,40 @@ private end def process_postcode_changes! - return if property_postcode.blank? - - self.postcode_known = "Yes" - inferred_la = get_inferred_la(property_postcode) - self.is_la_inferred = inferred_la.present? - self.la = inferred_la if inferred_la.present? - self.postcode = UKPostcode.parse(property_postcode).outcode - self.postcod2 = UKPostcode.parse(property_postcode).incode + process_postcode(property_postcode, "postcode_known", "is_la_inferred", "la", "postcode", "postcod2") + end + + def process_previous_postcode_changes! + process_postcode(previous_postcode, "previous_postcode_known", "is_previous_la_inferred", "prevloc", "ppostc1", "ppostc2") + end + + def process_postcode(postcode, postcode_known_key, la_inferred_key, la_key, outcode_key, incode_key) + return if postcode.blank? + + self[postcode_known_key] = "Yes" + inferred_la = get_inferred_la(postcode) + self[la_inferred_key] = inferred_la.present? + self[la_key] = inferred_la if inferred_la.present? + self[outcode_key] = UKPostcode.parse(postcode).outcode + self[incode_key] = UKPostcode.parse(postcode).incode end def reset_location_fields! - if is_la_inferred == true - self.la = nil + reset_location(is_la_inferred, "la", "is_la_inferred", "property_postcode", "postcode", "postcod2") + end + + def reset_previous_location_fields! + reset_location(is_previous_la_inferred, "prevloc", "is_previous_la_inferred", "previous_postcode", "ppostc1", "ppostc2") + end + + def reset_location(is_inferred, la_key, is_inferred_key, postcode_key, incode_key, outcode_key) + if is_inferred + self[la_key] = nil end - self.is_la_inferred = false - self.property_postcode = nil - self.postcode = nil - self.postcod2 = nil + self[is_inferred_key] = false + self[postcode_key] = nil + self[incode_key] = nil + self[outcode_key] = nil end def get_totelder diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index b241e158e..2b00c52e5 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -4,7 +4,7 @@ module Validations::LocalAuthorityValidations def validate_previous_accommodation_postcode(record) postcode = record.previous_postcode - if postcode.present? && !postcode.match(POSTCODE_REGEXP) + if record.previous_postcode_known == "Yes" && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) error_message = I18n.t("validations.postcode") record.errors.add :previous_postcode, error_message end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index d39bd3458..4fecaded1 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1445,6 +1445,55 @@ } } }, + "previous_postcode": { + "header": "", + "description": "", + "questions": { + "previuos_postcode_known": { + "check_answer_label": "Do you know the postcode of the household's last settled accommodation?", + "header": "Do you know the property’s postcode?", + "hint_text": "This is also known as the household's 'last settled home'.", + "type": "radio", + "answer_options": { + "1": "Yes", + "0": "No" + }, + "conditional_for": { + "previous_postcode": ["Yes"] + }, + "hidden_in_check_answers": true + }, + "previous_postcode": { + "check_answer_label": "Postcode of previous accommodation if the household has moved from settled accommodation", + "header": "Postcode for the previous accommodation", + "hint_text": "", + "type": "text", + "width": 5, + "inferred_answers": { "la": { "is_la_inferred": true } }, + "inferred_check_answers_value": { + "condition": { "postcode_known": "No" }, + "value": "Not known" + } + } + } + }, + "previous_la_known": { + "header": "", + "description": "", + "questions": { + "previous_la_known": { + "check_answer_label": "Do you know the local authority of the household’s last settled accommodation?", + "header": "This is also known as the household's 'last settled home'.", + "hint_text": "", + "type": "radio", + "answer_options": { + "1": "Yes", + "0": "No" + }, + "depends_on": [{ "is_previous_la_inferred": false }] + } + } + }, "previous_la": { "header": "", "description": "", @@ -1839,19 +1888,8 @@ "S92000003": "Scotland", "W92000004": "Wales", "9300000XX": "Outside UK" - } - } - } - }, - "previous_postcode": { - "header": "", - "description": "", - "questions": { - "previous_postcode": { - "check_answer_label": "Postcode of previous accommodation if the household has moved from settled accommodation", - "header": "Postcode for the previous accommodation", - "hint_text": "If the household has moved from settled accommodation immediately prior to being re-housed.", - "type": "text" + }, + "depends_on": [{"previous_la_known": "Yes", "is_previous_la_inferred": false }] } } }, diff --git a/db/migrate/20220209132411_add_previous_postcode_known.rb b/db/migrate/20220209132411_add_previous_postcode_known.rb new file mode 100644 index 000000000..ac865614b --- /dev/null +++ b/db/migrate/20220209132411_add_previous_postcode_known.rb @@ -0,0 +1,7 @@ +class AddPreviousPostcodeKnown < ActiveRecord::Migration[7.0] + change_table :case_logs, bulk: true do |t| + t.column :previous_postcode_known, :integer + t.column :previous_la_known, :integer + t.column :is_previous_la_inferred, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 035cde2df..ced88fb96 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -187,6 +187,9 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do t.decimal "tshortfall", precision: 10, scale: 2 t.decimal "chcharge", precision: 10, scale: 2 t.integer "declaration" + t.integer "previous_postcode_known" + t.integer "previous_la_known" + t.boolean "is_previous_la_inferred" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 3ca826ba0..3f18a2032 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1087,6 +1087,64 @@ RSpec.describe CaseLog do end end + context "when saving previous address" do + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\"}}", headers: {}) + end + + let!(:address_case_log) do + described_class.create({ + managing_organisation: organisation, + owning_organisation: organisation, + previous_postcode_known: "Yes", + previous_postcode: "M1 1AE", + }) + end + + it "correctly infers prevloc" do + record_from_db = ActiveRecord::Base.connection.execute("select prevloc from case_logs where id=#{address_case_log.id}").to_a[0] + expect(address_case_log.prevloc).to eq("Manchester") + expect(record_from_db["prevloc"]).to eq("E08000003") + end + + it "errors if the previous postcode is emptied" do + expect { address_case_log.update!({ previous_postcode: "" }) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) + end + + it "errors if the previous postcode is not valid" do + expect { address_case_log.update!({ previous_postcode: "invalid_postcode" }) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) + end + + it "correctly resets all fields if previous postcode not known" do + address_case_log.update!({ previous_postcode_known: "No" }) + + record_from_db = ActiveRecord::Base.connection.execute("select prevloc, previous_postcode from case_logs where id=#{address_case_log.id}").to_a[0] + expect(record_from_db["previous_postcode"]).to eq(nil) + expect(address_case_log.prevloc).to eq(nil) + expect(record_from_db["prevloc"]).to eq(nil) + end + + it "changes the prevloc if previous postcode changes from not known to known and provided" do + address_case_log.update!({ previous_postcode_known: "No" }) + address_case_log.update!({ prevloc: "Westminster" }) + + record_from_db = ActiveRecord::Base.connection.execute("select prevloc, previous_postcode from case_logs where id=#{address_case_log.id}").to_a[0] + expect(record_from_db["previous_postcode"]).to eq(nil) + expect(address_case_log.prevloc).to eq("Westminster") + expect(record_from_db["prevloc"]).to eq("E09000033") + + address_case_log.update!({ previous_postcode_known: "Yes", previous_postcode: "M1 1AD" }) + + record_from_db = ActiveRecord::Base.connection.execute("select prevloc, previous_postcode from case_logs where id=#{address_case_log.id}").to_a[0] + expect(record_from_db["previous_postcode"]).to eq("M1 1AD") + expect(address_case_log.prevloc).to eq("Manchester") + expect(record_from_db["prevloc"]).to eq("E08000003") + end + end + context "when saving rent and charges" do let!(:case_log) do described_class.create({ diff --git a/spec/models/validations/local_authority_validations_spec.rb b/spec/models/validations/local_authority_validations_spec.rb index dc5bebe3d..0c4c27313 100644 --- a/spec/models/validations/local_authority_validations_spec.rb +++ b/spec/models/validations/local_authority_validations_spec.rb @@ -14,18 +14,21 @@ RSpec.describe Validations::LocalAuthorityValidations do end it "does not add an error if the record previous_postcode is valid (uppercase space)" do + record.previous_postcode_known = "Yes" record.previous_postcode = "M1 1AE" local_auth_validator.validate_previous_accommodation_postcode(record) expect(record.errors).to be_empty end it "does not add an error if the record previous_postcode is valid (lowercase no space)" do + record.previous_postcode_known = "Yes" record.previous_postcode = "m11ae" local_auth_validator.validate_previous_accommodation_postcode(record) expect(record.errors).to be_empty end it "does add an error when the postcode is invalid" do + record.previous_postcode_known = "Yes" record.previous_postcode = "invalid" local_auth_validator.validate_previous_accommodation_postcode(record) expect(record.errors).not_to be_empty