diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 6c5cbe68a..d509630f1 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -121,7 +121,7 @@ class CaseLog < ApplicationRecord enum postcode_known: POLAR, _suffix: true enum la_known: POLAR, _suffix: true - AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype].freeze + AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred].freeze OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing].freeze @@ -197,6 +197,7 @@ private self.hhmemb = other_hhmemb + 1 if other_hhmemb.present? 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.present? end @@ -204,7 +205,10 @@ private uri = URI("https://api.os.uk/search/places/v1/postcode?key=#{ENV['OS_PLACES_API_KEY']}&postcode=#{postcode}&dataset=LPI") res = Net::HTTP.get_response(uri) response_body = JSON.parse(res.body) - response_body["results"][0]["LPI"]["ADMINISTRATIVE_AREA"].downcase.capitalize if res.is_a?(Net::HTTPSuccess) && (response_body["header"]["totalresults"]).to_i.positive? + if res.is_a?(Net::HTTPSuccess) && (response_body["header"]["totalresults"]).to_i.positive? + self.is_la_inferred = true + response_body["results"][0]["LPI"]["ADMINISTRATIVE_AREA"].downcase.capitalize + end end def all_fields_completed? diff --git a/app/models/form/page.rb b/app/models/form/page.rb index 6ee911de6..2cdebff39 100644 --- a/app/models/form/page.rb +++ b/app/models/form/page.rb @@ -24,7 +24,7 @@ class Form::Page return true unless depends_on depends_on.all? do |question, value| - case_log[question].present? && case_log[question] == value + !case_log[question].nil? && case_log[question] == value end end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index a9b458c2e..09485844b 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1119,10 +1119,10 @@ "1": "Yes" }, "conditional_for": { - "postcode": ["Yes"] + "property_postcode": ["Yes"] } }, - "postcode": { + "property_postcode": { "check_answer_label": "Postcode", "header": "", "hint_text": "", @@ -1145,7 +1145,8 @@ "1": "Yes" } } - } + }, + "depends_on": {"is_la_inferred": false} }, "select_local_authority": { "header": "", @@ -1473,7 +1474,7 @@ } } }, - "depends_on": { "la_known": "Yes" } + "depends_on": { "la_known": "Yes", "is_la_inferred": false } }, "why_dont_you_know_la": { "header": "", @@ -1486,7 +1487,7 @@ "type": "textarea" } }, - "depends_on": { "la_known": "No" } + "depends_on": { "la_known": "No", "is_la_inferred": false } }, "first_time_property_let_as_social_housing": { "header": "", diff --git a/db/migrate/20211209092131_add_is_la_inferred.rb b/db/migrate/20211209092131_add_is_la_inferred.rb new file mode 100644 index 000000000..d53c06c3d --- /dev/null +++ b/db/migrate/20211209092131_add_is_la_inferred.rb @@ -0,0 +1,7 @@ +class AddIsLaInferred < ActiveRecord::Migration[6.1] + def change + change_table :case_logs, bulk: true do |t| + t.column :is_la_inferred, :boolean + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 7f5ba379d..dde9a5ff4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_12_13_122642) do +ActiveRecord::Schema.define(version: 2021_12_09_092131) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -174,6 +174,7 @@ ActiveRecord::Schema.define(version: 2021_12_13_122642) do t.integer "day" t.integer "month" t.integer "year" + t.boolean "is_la_inferred" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" 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" diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index f92ffc2c9..2d566f231 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -47,4 +47,29 @@ RSpec.describe "Form Page Routing" do click_button("Save and continue") expect(page).to have_current_path("/logs/#{id}/conditional-question/check-answers") end + + context "inferred answers routing", js: true do + it "shows question if the answer could not be inferred" do + visit("/case-logs/#{id}/property-postcode") + fill_in("case-log-property-postcode-field", with: "P0 5ST") + click_button("Save and continue") + expect(page).to have_current_path("/case-logs/#{id}/do-you-know-the-local-authority") + end + + it "shows question if the answer could not be inferred" do + visit("/case-logs/#{id}/property-postcode") + click_button("Save and continue") + expect(page).to have_current_path("/case-logs/#{id}/do-you-know-the-local-authority") + end + + it "does not show question if the answer could be inferred" do + stub_request(:get, /api.os.uk/) + .to_return(status: 200, body: "{\"header\": {\"totalresults\": \"1\"}, \"results\": [{\"LPI\": {\"ADMINISTRATIVE_AREA\": \"MANCHESTER\"}}]}", headers: {}) + + visit("/case-logs/#{id}/property-postcode") + fill_in("case-log-property-postcode-field", with: "P0 5ST") + click_button("Save and continue") + expect(page).to have_current_path("/case-logs/#{id}/property-wheelchair-accessible") + end + end end diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json index 71d002e13..d2c234bc0 100644 --- a/spec/fixtures/forms/test_form.json +++ b/spec/fixtures/forms/test_form.json @@ -224,6 +224,36 @@ "property_information": { "label": "Property information", "pages": { + "property_postcode": { + "header": "", + "description": "", + "questions": { + "property_postcode": { + "check_answer_label": "Postcode", + "header": "", + "hint_text": "", + "type": "text", + "width": 5 + } + } + }, + "do_you_know_the_local_authority": { + "header": "", + "description": "", + "questions": { + "la_known": { + "check_answer_label": "Do you know what local authority the property is located in?", + "header": "Do you know what local authority the property is located in?", + "hint_text": "", + "type": "radio", + "answer_options": { + "0": "No", + "1": "Yes" + } + } + }, + "depends_on": {"is_la_inferred": false} + }, "property_wheelchair_accessible": { "questions": { "wchair": { diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index c17c89de3..704866ce9 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -35,7 +35,7 @@ RSpec.describe TasklistHelper do end it "returns the number of sections in progress" do - expect(get_subsections_count(form, case_log, :in_progress)).to eq(2) + expect(get_subsections_count(form, case_log, :in_progress)).to eq(3) end it "returns 0 for invalid state" do diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index f64024d1c..084a5d951 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1035,7 +1035,7 @@ RSpec.describe Form, type: :model do }) end - it "correctly infers postcode" do + it "correctly infers la" do 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] diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index e87765aac..5ffb54514 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -15,7 +15,7 @@ RSpec.describe FormHandler do form_handler = FormHandler.instance form = form_handler.get_form("test_form") expect(form).to be_a(Form) - expect(form.pages.count).to eq(25) + expect(form.pages.count).to eq(26) end end