From c4b18e9871ca6cbcde359293a01324dcac67b599 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Wed, 7 Jun 2023 12:43:27 +0100 Subject: [PATCH] feat: validate reasonable preference/homeless in bu 23 --- app/controllers/form_controller.rb | 4 ++-- app/models/form/question.rb | 4 ++++ app/models/log.rb | 2 +- app/services/bulk_upload/lettings/year2023/row_parser.rb | 9 +++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 5bd151181..f56583257 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -95,7 +95,7 @@ private next unless question_params if %w[checkbox validation_override].include?(question.type) - question.answer_options.keys.reject { |x| x.match(/divider/) }.each do |option| + question.answer_options.answer_keys_without_dividers.each do |option| result[option] = question_params.include?(option) ? 1 : 0 end else @@ -194,7 +194,7 @@ private def question_missing_response?(responses_for_page, question) if %w[checkbox validation_override].include?(question.type) - answered = question.answer_options.keys.reject { |x| x.match(/divider/) }.map do |option| + answered = question.answer_keys_without_dividers.map do |option| session["fields"][option] = @log[option] = params[@log.model_name.param_key][question.id].include?(option) ? 1 : 0 params[@log.model_name.param_key][question.id].exclude?(option) end diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 82d3a09b5..e50d1a66c 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -247,6 +247,10 @@ class Form::Question end end + def answer_keys_without_dividers + answer_options.keys.reject { |x| x.match(/divider/) } + end + private def selected_answer_option_is_derived?(log) diff --git a/app/models/log.rb b/app/models/log.rb index caa7a19f0..0f7d20a7a 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -108,7 +108,7 @@ class Log < ApplicationRecord question = form.questions.find { |q| q.id == error.attribute.to_s } if question.type == "checkbox" - question.answer_options.each_key { |attribute| public_send("#{attribute}=", nil) } + question.answer_keys_without_dividers { |attribute| public_send("#{attribute}=", nil) } else public_send("#{error.attribute}=", nil) end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 70bdac24e..21a84a07f 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -357,6 +357,7 @@ class BulkUpload::Lettings::Year2023::RowParser validate :validate_dont_know_disabled_needs_conjunction, on: :after_log validate :validate_no_and_dont_know_disabled_needs_conjunction, on: :after_log validate :validate_no_housing_needs_questions_answered, on: :after_log + validate :validate_reasonable_preference_homeless, on: :after_log validate :validate_if_log_already_exists, on: :after_log, if: -> { FeatureToggle.bulk_upload_duplicate_log_check_enabled? } validate :validate_owning_org_data_given, on: :after_log @@ -617,6 +618,14 @@ private end end + def validate_reasonable_preference_homeless + if field_110 == 1 + if field_105 == 1 && field_111 == 1 + errors.add(:field_111, I18n.t("validations.household.reasonpref.not_homeless")) + end + end + end + def validate_lettings_type_matches_bulk_upload if [1, 3, 5, 7, 9, 11].include?(field_5) && !general_needs? errors.add(:field_5, I18n.t("validations.setup.lettype.supported_housing_mismatch"))