diff --git a/app/models/form/question.rb b/app/models/form/question.rb index cc276a5ab..63795f8cf 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -1,7 +1,8 @@ class Form::Question attr_accessor :id, :header, :hint_text, :description, :questions, :type, :min, :max, :step, :width, :fields_to_add, :result_field, - :conditional_for, :readonly, :answer_options, :page, :check_answer_label + :conditional_for, :readonly, :answer_options, :page, :check_answer_label, + :inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value def initialize(id, hsh, page) @id = id @@ -18,6 +19,9 @@ class Form::Question @readonly = hsh["readonly"] @answer_options = hsh["answer_options"] @conditional_for = hsh["conditional_for"] + @inferred_answers = hsh["inferred_answers"] + @inferred_check_answers_value = hsh["inferred_check_answers_value"] + @hidden_in_check_answers = hsh["hidden_in_check_answers"] @page = page end @@ -28,7 +32,15 @@ class Form::Question return checkbox_answer_label(case_log) if type == "checkbox" return case_log[id].strftime("%d %b %Y") if type == "date" - case_log[id].to_s + return case_log[id].to_s if case_log[id].present? + + has_inferred_check_answers_value?(case_log) ? inferred_check_answers_value["value"] : "" + end + + def get_inferred_answers(case_log) + return enabled_inferred_answers(inferred_answers, case_log).keys.map { |x| case_log[x].to_s } if inferred_answers + + [] end def read_only? @@ -41,6 +53,16 @@ class Form::Question conditional_on.map { |condition| evaluate_condition(condition, case_log) }.all? end + def hidden_in_check_answers? + hidden_in_check_answers + end + + def has_inferred_check_answers_value?(case_log) + return inferred_check_answers_value["condition"].values[0] == case_log[inferred_check_answers_value["condition"].keys[0]] if inferred_check_answers_value.present? + + false + end + def update_answer_link_name(case_log) if type == "checkbox" answer_options.keys.any? { |key| case_log[key] == "Yes" } ? "Change" : "Answer" @@ -82,4 +104,8 @@ private raise "Not implemented yet" end end + + def enabled_inferred_answers(inferred_answers, case_log) + inferred_answers.filter { |_key, value| value.all? { |condition_key, condition_value| case_log[condition_key] == condition_value } } + end end diff --git a/app/models/form/subsection.rb b/app/models/form/subsection.rb index 5ae163539..8704d9e8a 100644 --- a/app/models/form/subsection.rb +++ b/app/models/form/subsection.rb @@ -52,7 +52,7 @@ class Form::Subsection end def applicable_questions(case_log) - questions.select { |q| q.page.routed_to?(case_log) && q.enabled?(case_log) } + questions.select { |q| (displayed_to_user?(case_log, q) && !q.hidden_in_check_answers?) || q.has_inferred_check_answers_value?(case_log) } end def answered_questions(case_log) @@ -62,4 +62,8 @@ class Form::Subsection def unanswered_questions(case_log) applicable_questions(case_log) - answered_questions(case_log) end + + def displayed_to_user?(case_log, question) + question.page.routed_to?(case_log) && question.enabled?(case_log) + end end diff --git a/app/views/form/_check_answers_table.html.erb b/app/views/form/_check_answers_table.html.erb index 9bcc11e3d..c3e5c7f1e 100644 --- a/app/views/form/_check_answers_table.html.erb +++ b/app/views/form/_check_answers_table.html.erb @@ -3,7 +3,10 @@ <%= question.check_answer_label.to_s.present? ? question.check_answer_label.to_s : question.header.to_s %>
- <%= question.answer_label(@case_log) %> + <%= question.answer_label(@case_log) %>
+ <% question.get_inferred_answers(@case_log).each do |inferred_answer| %> + <%= inferred_answer %>
+ <% end %>
<%= govuk_link_to(question.update_answer_link_name(@case_log), "/logs/#{@case_log.id}/#{question.page.id.to_s.dasherize}").html_safe %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 97c15becb..a270b8df4 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -29,8 +29,7 @@ "header": "You cannot use this service", "hint_text": "", "description": "We cannot accept data about a tenant or buyer unless they’ve seen the DLUHC privacy notice.", - "questions": { - }, + "questions": {}, "depends_on": { "gdpr_acceptance": "No" } }, "organisation_details": { @@ -92,7 +91,10 @@ } } }, - "depends_on": { "gdpr_acceptance": "Yes", "sale_or_letting": "Letting" } + "depends_on": { + "gdpr_acceptance": "Yes", + "sale_or_letting": "Letting" + } }, "startdate": { "header": "", @@ -105,7 +107,10 @@ "type": "date" } }, - "depends_on": { "gdpr_acceptance": "Yes", "sale_or_letting": "Letting" } + "depends_on": { + "gdpr_acceptance": "Yes", + "sale_or_letting": "Letting" + } }, "about_this_letting": { "header": "Tell us about this letting", @@ -125,7 +130,9 @@ "5": "Other intermediate rent product" }, "conditional_for": { - "intermediate_rent_product_name": ["Other intermediate rent product"] + "intermediate_rent_product_name": [ + "Other intermediate rent product" + ] } }, "intermediate_rent_product_name": { @@ -144,7 +151,10 @@ } } }, - "depends_on": { "gdpr_acceptance": "Yes", "sale_or_letting": "Letting" } + "depends_on": { + "gdpr_acceptance": "Yes", + "sale_or_letting": "Letting" + } }, "tenant_code": { "header": "", @@ -158,7 +168,10 @@ "width": 10 } }, - "depends_on": { "gdpr_acceptance": "Yes", "sale_or_letting": "Letting" } + "depends_on": { + "gdpr_acceptance": "Yes", + "sale_or_letting": "Letting" + } }, "property_reference": { "header": "", @@ -185,7 +198,10 @@ "type": "date" } }, - "depends_on": { "gdpr_acceptance": "Yes", "sale_or_letting": "Sale" } + "depends_on": { + "gdpr_acceptance": "Yes", + "sale_or_letting": "Sale" + } }, "purchaser_code": { "header": "", @@ -199,7 +215,10 @@ "width": 10 } }, - "depends_on": { "gdpr_acceptance": "Yes", "sale_or_letting": "Sale" } + "depends_on": { + "gdpr_acceptance": "Yes", + "sale_or_letting": "Sale" + } } } } @@ -210,7 +229,7 @@ "subsections": { "household_characteristics": { "label": "Household characteristics", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "person_1_age": { "header": "", @@ -302,7 +321,7 @@ "10": "Romania", "11": "Ireland", "12": "Slovakia", - "13":"Slovenia", + "13": "Slovenia", "14": "Croatia", "15": "Other EU Economic Area (EEA country)", "16": "Any other country", @@ -759,7 +778,7 @@ }, "household_situation": { "label": "Household situation", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "previous_housing_situation": { "header": "", @@ -885,7 +904,7 @@ }, "household_needs": { "label": "Household needs", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "armed_forces": { "header": "Experience of the UK Armed Forces", @@ -897,15 +916,19 @@ "type": "radio", "check_answer_label": "Armed Forces", "answer_options": { - "0":"A current or former regular in the UK Armed Forces (exc. National Service)", - "1":"A current or former reserve in the UK Armed Forces (exc. National Service)", + "0": "A current or former regular in the UK Armed Forces (exc. National Service)", + "1": "A current or former reserve in the UK Armed Forces (exc. National Service)", "2": "A spouse / civil partner of a UK Armed Forces member who has separated or been bereaved within the last 2 years", "3": "No", "4": "Tenant prefers not to say" }, "conditional_for": { - "leftreg": ["A current or former regular in the UK Armed Forces (exc. National Service)"], - "reservist": ["A current or former regular in the UK Armed Forces (exc. National Service)"] + "leftreg": [ + "A current or former regular in the UK Armed Forces (exc. National Service)" + ], + "reservist": [ + "A current or former regular in the UK Armed Forces (exc. National Service)" + ] } }, "leftreg": { @@ -1023,7 +1046,7 @@ "subsections": { "tenancy_information": { "label": "Tenancy information", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "starter_tenancy": { "header": "", @@ -1121,14 +1144,20 @@ }, "conditional_for": { "property_postcode": ["Yes"] - } + }, + "hidden_in_check_answers": true }, "property_postcode": { "check_answer_label": "Postcode", "header": "", "hint_text": "", "type": "text", - "width": 5 + "width": 5, + "inferred_answers": { "la": { "is_la_inferred": true } }, + "inferred_check_answers_value": { + "condition": { "postcode_known": "No" }, + "value": "Not known" + } } } }, @@ -1147,7 +1176,7 @@ } } }, - "depends_on": {"is_la_inferred": false} + "depends_on": { "is_la_inferred": false } }, "select_local_authority": { "header": "", @@ -1509,7 +1538,7 @@ } } }, - "depends_on": { "tenant_same_property_renewal": "No"} + "depends_on": { "tenant_same_property_renewal": "No" } }, "unitletas": { "header": "", @@ -1528,7 +1557,10 @@ } } }, - "depends_on": { "first_time_property_let_as_social_housing": "No", "tenant_same_property_renewal": "No" } + "depends_on": { + "first_time_property_let_as_social_housing": "No", + "tenant_same_property_renewal": "No" + } }, "property_vacancy_reason_not_first_let": { "header": "", @@ -1554,7 +1586,10 @@ } } }, - "depends_on": { "first_time_property_let_as_social_housing": "No", "tenant_same_property_renewal": "No" } + "depends_on": { + "first_time_property_let_as_social_housing": "No", + "tenant_same_property_renewal": "No" + } }, "property_vacancy_reason_first_let": { "header": "", @@ -1572,7 +1607,10 @@ } } }, - "depends_on": { "first_time_property_let_as_social_housing": "Yes", "tenant_same_property_renewal": "No" } + "depends_on": { + "first_time_property_let_as_social_housing": "Yes", + "tenant_same_property_renewal": "No" + } }, "property_number_of_times_relet_not_social_let": { "header": "", @@ -1588,7 +1626,10 @@ "step": 1 } }, - "depends_on": { "first_time_property_let_as_social_housing": "No", "tenant_same_property_renewal": "No" } + "depends_on": { + "first_time_property_let_as_social_housing": "No", + "tenant_same_property_renewal": "No" + } }, "property_number_of_times_relet_social_let": { "header": "", @@ -1604,7 +1645,10 @@ "step": 1 } }, - "depends_on": { "first_time_property_let_as_social_housing": "Yes", "tenant_same_property_renewal": "No" } + "depends_on": { + "first_time_property_let_as_social_housing": "Yes", + "tenant_same_property_renewal": "No" + } }, "property_unit_type": { "header": "", @@ -1687,7 +1731,10 @@ "type": "date" } }, - "depends_on": { "rsnvac": "First let of newbuild property", "tenant_same_property_renewal": "No" } + "depends_on": { + "rsnvac": "First let of newbuild property", + "tenant_same_property_renewal": "No" + } }, "new_build_handover_date": { "header": "", @@ -1702,7 +1749,10 @@ }, "depends_on": { "tenant_same_property_renewal": "No", - "rsnvac": ["First let of conversion, rehabilitation or acquired property?", "First let of leased property"] + "rsnvac": [ + "First let of conversion, rehabilitation or acquired property?", + "First let of leased property" + ] } }, "property_major_repairs": { @@ -1729,7 +1779,10 @@ "type": "date" } }, - "depends_on": { "rsnvac": "First let of newbuild property", "tenant_same_property_renewal": "No"} + "depends_on": { + "rsnvac": "First let of newbuild property", + "tenant_same_property_renewal": "No" + } } } } @@ -1740,7 +1793,7 @@ "subsections": { "income_and_benefits": { "label": "Income and benefits", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "net_income": { "header": "", @@ -1834,7 +1887,7 @@ }, "rent": { "label": "Rent", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "rent": { "header": "", @@ -1865,12 +1918,7 @@ "type": "numeric", "min": 0, "step": 1, - "fields-to-add": [ - "brent", - "scharge", - "pscharge", - "supcharg" - ], + "fields-to-add": ["brent", "scharge", "pscharge", "supcharg"], "result-field": "tcharge" }, "scharge": { @@ -1880,12 +1928,7 @@ "type": "numeric", "min": 0, "step": 1, - "fields-to-add": [ - "brent", - "scharge", - "pscharge", - "supcharg" - ], + "fields-to-add": ["brent", "scharge", "pscharge", "supcharg"], "result-field": "tcharge" }, "pscharge": { @@ -1895,12 +1938,7 @@ "type": "numeric", "min": 0, "step": 1, - "fields-to-add": [ - "brent", - "scharge", - "pscharge", - "supcharg" - ], + "fields-to-add": ["brent", "scharge", "pscharge", "supcharg"], "result-field": "tcharge" }, "supcharg": { @@ -1910,12 +1948,7 @@ "type": "numeric", "min": 0, "step": 1, - "fields-to-add": [ - "brent", - "scharge", - "pscharge", - "supcharg" - ], + "fields-to-add": ["brent", "scharge", "pscharge", "supcharg"], "result-field": "tcharge" }, "tcharge": { @@ -1960,7 +1993,7 @@ "subsections": { "local_authority": { "label": "Local authority", - "depends_on": {"about_this_log": "completed"}, + "depends_on": { "about_this_log": "completed" }, "pages": { "time_lived_in_la": { "header": "", @@ -2428,7 +2461,7 @@ "answer_options": { "0": "Yes", "1": "No", - "2": "Do not know" + "2": "Do not know" }, "conditional_for": { "reasonable_preference_reason": ["Yes"] diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index d9f5aa1d8..4bffa4602 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -251,7 +251,12 @@ "header": "", "hint_text": "", "type": "text", - "width": 5 + "width": 5, + "inferred_answers": { "la": { "is_la_inferred": true } }, + "inferred_check_answers_value": { + "condition": { "postcode_known": "No" }, + "value": "Not known" + } } } }, @@ -600,7 +605,7 @@ "hint_text": "If the household has moved from settled accommodation immediately prior to being re-housed", "type": "text", "width": 5, - "conditional_for": { "faake_key": "fake_condition" } + "conditional_for": { "fake_key": "fake_condition" } }, "previous_postcode": { "check_answer_label": "Postcode of previous accomodation if the household has moved from settled accommodation", diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index bd783b8f4..ac5da57fb 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -240,6 +240,44 @@ RSpec.describe CaseLogsController, type: :request do end end end + + context "Check answers" do + let(:postcode_case_log) do + FactoryBot.create(:case_log, + owning_organisation: organisation, + managing_organisation: organisation, + postcode_known: "No") + end + let(:id) { postcode_case_log.id } + + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\"}}", headers: {}) + sign_in user + end + + it "shows the inferred postcode" do + case_log = FactoryBot.create(:case_log, + owning_organisation: organisation, + managing_organisation: organisation, + postcode_known: "Yes", + property_postcode: "P0 0ST") + id = case_log.id + get "/logs/#{id}/property-information/check-answers" + expected_inferred_answer = "Manchester" + expect(CGI.unescape_html(response.body)).to include(expected_inferred_answer) + end + + it "does not show do you know the property postcode question" do + get "/logs/#{id}/property-information/check-answers" + expect(CGI.unescape_html(response.body)).not_to include("Do you know the property postcode?") + end + + it "shows if the postcode is not known" do + get "/logs/#{id}/property-information/check-answers" + expect(CGI.unescape_html(response.body)).to include("Not known") + end + end end describe "PATCH" do