Browse Source

Add routing based on inferred la

pull/165/head
Kat 4 years ago
parent
commit
6022fc5326
  1. 8
      app/models/case_log.rb
  2. 2
      app/models/form/page.rb
  3. 11
      config/forms/2021_2022.json
  4. 7
      db/migrate/20211209092131_add_is_la_inferred.rb
  5. 3
      db/schema.rb
  6. 25
      spec/features/form/page_routing_spec.rb
  7. 30
      spec/fixtures/forms/test_form.json
  8. 2
      spec/helpers/tasklist_helper_spec.rb
  9. 2
      spec/models/case_log_spec.rb
  10. 2
      spec/models/form_handler_spec.rb

8
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?

2
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

11
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": "",

7
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

3
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"

25
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

30
spec/fixtures/forms/test_form.json vendored

@ -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": {

2
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

2
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]

2
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

Loading…
Cancel
Save