From b358a67dde9cb57cca37691d129e139fa89cf9db Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 15 Nov 2021 15:42:11 +0000 Subject: [PATCH 1/4] CLDC-679: Update main tenancy type mappings (#91) * Update main tenancy type mappins * Fix the tests and validations --- app/constants/db_enums.rb | 9 ++++----- app/models/case_log.rb | 2 +- app/validations/tenancy_validations.rb | 6 +++--- config/forms/2021_2022.json | 11 +++++------ docs/api/DLUHC-CORE-Data.v1.json | 2 +- spec/fixtures/complete_case_log.json | 2 +- spec/models/case_log_spec.rb | 18 +++++++++--------- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/constants/db_enums.rb b/app/constants/db_enums.rb index 23e4b4bd4..d28393105 100644 --- a/app/constants/db_enums.rb +++ b/app/constants/db_enums.rb @@ -166,11 +166,10 @@ module DbEnums def self.tenancy { - "Fixed term – Secure" => 1, - "Fixed term – Assured Shorthold Tenancy (AST)" => 4, - "Lifetime – Secure" => 100, - "Lifetime – Assured" => 2, - "License agreement" => 5, + "Secure (including flexible)" => 1, + "Assured" => 2, + "Assured Shorthold" => 4, + "Licence agreement (almshouses only)" => 5, "Other" => 3, } end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 5553b5a78..f73df0264 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -230,7 +230,7 @@ private dynamically_not_required << "incfreq" end - if tenancy == "Fixed term – Secure" + if tenancy == "Secure (including flexible)" dynamically_not_required << "tenancylength" end diff --git a/app/validations/tenancy_validations.rb b/app/validations/tenancy_validations.rb index 32ff8e2b0..fc275f91e 100644 --- a/app/validations/tenancy_validations.rb +++ b/app/validations/tenancy_validations.rb @@ -4,12 +4,12 @@ module TenancyValidations def validate_fixed_term_tenancy(record) is_present = record.tenancylength.present? is_in_range = record.tenancylength.to_i.between?(2, 99) - is_secure = record.tenancy == "Fixed term – Secure" - is_ast = record.tenancy == "Fixed term – Assured Shorthold Tenancy (AST)" + is_secure = record.tenancy == "Secure (including flexible)" + is_ast = record.tenancy == "Assured Shorthold" conditions = [ { condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" }, { condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" }, - { condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" }, + { condition: is_secure && (!is_in_range && is_present), error: "Secure (including flexible) should be between 2 and 99 years or not specified" }, ] conditions.each { |condition| condition[:condition] ? (record.errors.add :tenancylength, condition[:error]) : nil } diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 859e5fa14..7433f4d7b 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -1068,12 +1068,11 @@ "hint_text": "", "type": "radio", "answer_options": { - "0": "Fixed term – Secure", - "1": "Fixed term – Assured Shorthold Tenancy (AST)", - "2": "Lifetime – Secure", - "3": "Lifetime – Assured", - "4": "License agreement", - "5": "Other" + "0": "Secure (including flexible)", + "1": "Assured", + "2": "Assured Shorthold", + "3": "Licence agreement (almshouses only)", + "4": "Other" }, "conditional_for": { "other_tenancy_type": ["Other"] diff --git a/docs/api/DLUHC-CORE-Data.v1.json b/docs/api/DLUHC-CORE-Data.v1.json index 17560d14c..5723c9311 100644 --- a/docs/api/DLUHC-CORE-Data.v1.json +++ b/docs/api/DLUHC-CORE-Data.v1.json @@ -306,7 +306,7 @@ "startdate": "12/03/2019", "startertenancy": "No", "tenancylength": "No", - "tenancy": "Fixed term – Secure", + "tenancy": "Secure (including flexible)", "lettype": "Affordable Rent - General Needs", "landlord": "This landlord", "la": "Barnet", diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 4656a04f8..81f2735fc 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -52,7 +52,7 @@ "startdate": "12/03/2019", "startertenancy": "No", "tenancylength": "5", - "tenancy": "Fixed term – Secure", + "tenancy": "Secure (including flexible)", "lettype": "Affordable Rent - General Needs", "landlord": "This landlord", "la": "Barnet", diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index e663937db..fad6ca4b9 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -182,39 +182,39 @@ RSpec.describe Form, type: :model do it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do expect { - CaseLog.create!(tenancy: "Fixed term – Assured Shorthold Tenancy (AST)", + CaseLog.create!(tenancy: "Assured Shorthold", tenancylength: 1) }.to raise_error(ActiveRecord::RecordInvalid) expect { - CaseLog.create!(tenancy: "Fixed term – Assured Shorthold Tenancy (AST)", + CaseLog.create!(tenancy: "Assured Shorthold", tenancylength: nil) }.to raise_error(ActiveRecord::RecordInvalid) expect { - CaseLog.create!(tenancy: "Fixed term – Assured Shorthold Tenancy (AST)", + CaseLog.create!(tenancy: "Assured Shorthold", tenancylength: 2) }.not_to raise_error end it "Must be empty or between 2 and 99 if type of tenancy is Secure" do expect { - CaseLog.create!(tenancy: "Fixed term – Secure", + CaseLog.create!(tenancy: "Secure (including flexible)", tenancylength: 1) }.to raise_error(ActiveRecord::RecordInvalid) expect { - CaseLog.create!(tenancy: "Fixed term – Secure", + CaseLog.create!(tenancy: "Secure (including flexible)", tenancylength: 100) }.to raise_error(ActiveRecord::RecordInvalid) expect { - CaseLog.create!(tenancy: "Fixed term – Secure", + CaseLog.create!(tenancy: "Secure (including flexible)", tenancylength: nil) }.not_to raise_error expect { - CaseLog.create!(tenancy: "Fixed term – Secure", + CaseLog.create!(tenancy: "Secure (including flexible)", tenancylength: 2) }.not_to raise_error end @@ -294,12 +294,12 @@ RSpec.describe Form, type: :model do it "must not be provided if tenancy type is not other" do expect { - CaseLog.create!(tenancy: "Fixed term – Secure", + CaseLog.create!(tenancy: "Secure (including flexible)", tenancyother: "the other reason provided") }.to raise_error(ActiveRecord::RecordInvalid) expect { - CaseLog.create!(tenancy: "Fixed term – Secure", + CaseLog.create!(tenancy: "Secure (including flexible)", tenancyother: nil) }.not_to raise_error end From b787ad803fd0294bd7a193f5494909f8e5fac3a2 Mon Sep 17 00:00:00 2001 From: Daniel Baark <5101747+baarkerlounger@users.noreply.github.com> Date: Mon, 15 Nov 2021 16:38:21 +0000 Subject: [PATCH 2/4] CLDC-709: Conditional section status bugfix (#92) * Failing spec * Fix the failing spec * Fix rake spec --- app/helpers/check_answers_helper.rb | 32 +------------------ app/helpers/tasklist_helper.rb | 21 ++++++------ app/models/form.rb | 30 +++++++++++++++++ app/views/case_logs/_tasklist.html.erb | 4 +-- lib/tasks/form_definition.rake | 10 +++--- spec/factories/case_log.rb | 9 ++++++ spec/helpers/tasklist_helper_spec.rb | 27 ++++++++++------ .../tasks/form_definition_validator_spec.rb | 2 +- 8 files changed, 76 insertions(+), 59 deletions(-) diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index 6282f9d06..ba6373b72 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -16,7 +16,7 @@ module CheckAnswersHelper while page_name.to_s != "check_answers" && subsection_keys.include?(page_name) questions = form.questions_for_page(page_name) - applicable_questions = filter_conditional_questions(questions, case_log) + applicable_questions = form.filter_conditional_questions(questions, case_log) total_questions = total_questions.merge(applicable_questions) page_name = get_next_page_name(form, page_name, case_log) @@ -25,19 +25,6 @@ module CheckAnswersHelper total_questions end - def filter_conditional_questions(questions, case_log) - applicable_questions = questions - - questions.each do |k, question| - question.fetch("conditional_for", []).each do |conditional_question_key, condition| - if condition_not_met(case_log, k, question, condition) - applicable_questions = applicable_questions.reject { |z| z == conditional_question_key } - end - end - end - applicable_questions - end - def get_next_page_name(form, page_name, case_log) page = form.all_pages[page_name] if page.key?("conditional_route_to") @@ -50,23 +37,6 @@ module CheckAnswersHelper form.next_page(page_name) end - def condition_not_met(case_log, question_key, question, condition) - case question["type"] - when "numeric" - operator = condition[/[<>=]+/].to_sym - operand = condition[/\d+/].to_i - case_log[question_key].blank? || !case_log[question_key].send(operator, operand) - when "text" - case_log[question_key].blank? || !condition.include?(case_log[question_key]) - when "radio" - case_log[question_key].blank? || !condition.include?(case_log[question_key]) - when "select" - case_log[question_key].blank? || !condition.include?(case_log[question_key]) - else - raise "Not implemented yet" - end - end - def create_update_answer_link(case_log_answer, case_log_id, page) link_name = case_log_answer.blank? ? "Answer" : "Change" link_to(link_name, "/case_logs/#{case_log_id}/#{page}", class: "govuk-link").html_safe diff --git a/app/helpers/tasklist_helper.rb b/app/helpers/tasklist_helper.rb index 4ca9f182d..1d3f376c5 100644 --- a/app/helpers/tasklist_helper.rb +++ b/app/helpers/tasklist_helper.rb @@ -13,31 +13,32 @@ module TasklistHelper in_progress: "govuk-tag--blue", }.freeze - def get_subsection_status(subsection_name, case_log, questions) + def get_subsection_status(subsection_name, case_log, form, questions) + applicable_questions = form.filter_conditional_questions(questions, case_log).keys if subsection_name == "declaration" return case_log.completed? ? :not_started : :cannot_start_yet end - return :not_started if questions.all? { |question| case_log[question].blank? } - return :completed if questions.all? { |question| case_log[question].present? } + return :not_started if applicable_questions.all? { |question| case_log[question].blank? } + return :completed if applicable_questions.all? { |question| case_log[question].present? } :in_progress end def get_next_incomplete_section(form, case_log) subsections = form.all_subsections.keys - subsections.find { |subsection| is_incomplete?(subsection, case_log, form.questions_for_subsection(subsection).keys) } + subsections.find { |subsection| is_incomplete?(subsection, case_log, form, form.questions_for_subsection(subsection)) } end def get_subsections_count(form, case_log, status = :all) subsections = form.all_subsections.keys return subsections.count if status == :all - subsections.count { |subsection| get_subsection_status(subsection, case_log, form.questions_for_subsection(subsection).keys) == status } + subsections.count { |subsection| get_subsection_status(subsection, case_log, form, form.questions_for_subsection(subsection)) == status } end def get_first_page_or_check_answers(subsection, case_log, form, questions) - path = if is_started?(subsection, case_log, questions) + path = if is_started?(subsection, case_log, form, questions) "case_log_#{subsection}_check_answers_path" else "case_log_#{form.first_page_for_subsection(subsection)}_path" @@ -47,13 +48,13 @@ module TasklistHelper private - def is_incomplete?(subsection, case_log, questions) - status = get_subsection_status(subsection, case_log, questions) + def is_incomplete?(subsection, case_log, form, questions) + status = get_subsection_status(subsection, case_log, form, questions) %i[not_started in_progress].include?(status) end - def is_started?(subsection, case_log, questions) - status = get_subsection_status(subsection, case_log, questions) + def is_started?(subsection, case_log, form, questions) + status = get_subsection_status(subsection, case_log, form, questions) %i[in_progress completed].include?(status) end end diff --git a/app/models/form.rb b/app/models/form.rb index 3eeac8987..a2479e20b 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -97,6 +97,36 @@ class Form }.reduce(:merge) end + def filter_conditional_questions(questions, case_log) + applicable_questions = questions + + questions.each do |k, question| + question.fetch("conditional_for", []).each do |conditional_question_key, condition| + if condition_not_met(case_log, k, question, condition) + applicable_questions = applicable_questions.reject { |z| z == conditional_question_key } + end + end + end + applicable_questions + end + + def condition_not_met(case_log, question_key, question, condition) + case question["type"] + when "numeric" + operator = condition[/[<>=]+/].to_sym + operand = condition[/\d+/].to_i + case_log[question_key].blank? || !case_log[question_key].send(operator, operand) + when "text" + case_log[question_key].blank? || !condition.include?(case_log[question_key]) + when "radio" + case_log[question_key].blank? || !condition.include?(case_log[question_key]) + when "select" + case_log[question_key].blank? || !condition.include?(case_log[question_key]) + else + raise "Not implemented yet" + end + end + def get_answer_label(case_log, question_title) question = all_questions[question_title] if question["type"] == "checkbox" diff --git a/app/views/case_logs/_tasklist.html.erb b/app/views/case_logs/_tasklist.html.erb index fdf4c5aa9..1e75f207b 100644 --- a/app/views/case_logs/_tasklist.html.erb +++ b/app/views/case_logs/_tasklist.html.erb @@ -9,10 +9,10 @@