diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index 49e9a0bdf..417511211 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -131,16 +131,16 @@ private def get_next_page_path(form, previous_page, case_log = {}) questions_for_page = form.questions_for_page(previous_page) - questions_for_page.each do |_question, content| - next unless content.key?("conditional_route_to") + content = form.all_pages[previous_page] + if content.key?("conditional_route_to") content["conditional_route_to"].each do |route, conditions| if conditions.keys.all? { |x| case_log[x].present? } && conditions.all? { |k, v| v.include?(case_log[k]) } return "case_log_#{route}_path" end + # raise "" end end - form.next_page_redirect_path(previous_page) end end diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index 9ebeba40e..c473c32d6 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -11,9 +11,10 @@ module CheckAnswersHelper def total_questions(subsection, case_log, form) total_questions = {} - page_name = form.pages_for_subsection(subsection).keys.first + subsection_keys = form.pages_for_subsection(subsection).keys + page_name = subsection_keys.first - while page_name.to_s != "check_answers" + while page_name.to_s != "check_answers" && subsection_keys.include?(page_name) questions = form.questions_for_page(page_name) question_key = questions.keys[0] question_value = questions.values[0] @@ -21,7 +22,7 @@ module CheckAnswersHelper applicable_questions = filter_conditional_questions(questions, case_log) total_questions = total_questions.merge(applicable_questions) - page_name = get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value) + page_name = get_next_page_name(form, page_name, case_log) end total_questions @@ -40,15 +41,15 @@ module CheckAnswersHelper applicable_questions end - def get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value) - if applicable_questions[question_key].key?("conditional_route_to") - applicable_questions[question_key]["conditional_route_to"].each do |conditional_page_key, condition| - unless condition.any? { |k, v| condition_not_met(case_log, k, question_value, v) } - return conditional_page_key + def get_next_page_name(form, page_name, case_log) + page = form.all_pages[page_name] + if page.key?("conditional_route_to") + page["conditional_route_to"].each do |conditional_page_name, condition| + unless condition.any? { |field, value| condition_not_met(case_log, field, form.questions_for_page[field], value) } + return conditional_page_name end end end - form.next_page(page_name) end diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json index b4de71970..f5bd9cb48 100644 --- a/spec/fixtures/forms/test_form.json +++ b/spec/fixtures/forms/test_form.json @@ -248,14 +248,14 @@ "answer_options": { "0": "Yes", "1": "No" - }, - "conditional_route_to": { - "rent": { "pregnancy": "Yes", "tenant_gender": "Female" }, - "conditional_question_yes_page": { "pregnancy": "Yes" }, - "conditional_question_no_page": { "pregnancy": "No" } } } }, + "conditional_route_to": { + "rent": { "pregnancy": "Yes", "tenant_gender": "Female" }, + "conditional_question_yes_page": { "pregnancy": "Yes" }, + "conditional_question_no_page": { "pregnancy": "No" } + }, "default_next_page": "check_answers" }, "conditional_question_yes_page": { diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index 120c7947c..54295149a 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -153,8 +153,16 @@ RSpec.describe CheckAnswersHelper do it "it includes conditional pages and questions that were displayed" do case_log["pregnancy"] = "Yes" + case_log["tenant_gender"] = "Female" result = total_questions(conditional_routing_subsection, case_log, form) - expected_keys = %w[pregnancy cbl_letting] + expected_keys = %w[pregnancy] + expect(result.keys).to match_array(expected_keys) + end + + it "it includes conditional pages and questions that were displayed" do + case_log["pregnancy"] = "No" + result = total_questions(conditional_routing_subsection, case_log, form) + expected_keys = %w[pregnancy conditional_question_no_question conditional_question_no_second_question] expect(result.keys).to match_array(expected_keys) end end