diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 9d273dab5..225adcd72 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -335,6 +335,7 @@ class BulkUpload::Lettings::Year2022::RowParser validate :validate_declaration_acceptance, on: :after_log validate :validate_valid_radio_option, on: :before_log + validate :validate_incomplete_soft_validations, on: :after_log def self.question_for_field(field) QUESTIONS[field] @@ -716,6 +717,22 @@ private end end + def validate_incomplete_soft_validations + routed_to_soft_validation_questions = log.form.questions.filter { |q| q.type == "interruption_screen" && q.page.routed_to?(log, nil) } + routed_to_soft_validation_questions.each do |question| + next unless question + next if log.optional_fields.include?(question.id) + next if question.completed?(log) + + question.page.interruption_screen_question_ids.each do |interruption_screen_question_id| + field_mapping_for_errors[interruption_screen_question_id.to_sym].each do |field| + error_message = [display_title_text(question.page.title_text, log), display_informative_text(question.page.informative_text, log)].join(". ") + errors.add(field, message: error_message, category: :soft_validation) + end + end + end + end + def setup_question?(question) log.form.setup_sections[0].subsections[0].questions.include?(question) end @@ -1366,4 +1383,44 @@ private def scheme @scheme ||= Scheme.find_by_id_on_multiple_fields(field_4) end + + def display_title_text(title_text, log) + return "" if title_text.nil? + + translation_params = {} + arguments = title_text["arguments"] || {} + arguments.each do |argument| + value = get_value_from_argument(log, argument) + translation_params[argument["i18n_template"].to_sym] = value + end + I18n.t(title_text["translation"], **translation_params).to_s + end + + def display_informative_text(informative_text, log) + return "" unless informative_text["arguments"] + + translation_params = {} + informative_text["arguments"].each do |argument| + value = get_value_from_argument(log, argument) + translation_params[argument["i18n_template"].to_sym] = value + end + + begin + translation = I18n.t(informative_text["translation"], **translation_params) + translation.to_s.html_safe + rescue I18n::MissingInterpolationArgument => e + Rails.logger.error(e.message) + "" + end + end + + def get_value_from_argument(log, argument) + if argument["label"] + log.form.get_question(argument["key"], log).answer_label(log).downcase + elsif argument["arguments_for_key"] + log.public_send(argument["key"], argument["arguments_for_key"]) + else + log.public_send(argument["key"]) + end + end end diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index dd9bf4a5b..1d539c13d 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -954,6 +954,28 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do end end end + + describe "soft validations" do + context "when soft validation is triggered" do + let(:attributes) { setup_section_params.merge({ field_12: 22, field_35: 5, field_20: "F" }) } + + it "adds an error to the relevant fields" do + soft_validation_errors = parser.errors.select { |e| e.options[:category] == :soft_validation } + + expect(soft_validation_errors.find { |e| e.attribute == :field_12 }).to be_present + expect(soft_validation_errors.find { |e| e.attribute == :field_20 }).to be_present + expect(soft_validation_errors.find { |e| e.attribute == :field_35 }).to be_present + end + + it "populates with correct error message" do + soft_validation_errors = parser.errors.select { |e| e.options[:category] == :soft_validation } + + expect(soft_validation_errors.find { |e| e.attribute == :field_12 }.message).to eql("You told us this person is under 60 and retired. The minimum expected retirement age for females in England is 60.") + expect(soft_validation_errors.find { |e| e.attribute == :field_20 }.message).to eql("You told us this person is under 60 and retired. The minimum expected retirement age for females in England is 60.") + expect(soft_validation_errors.find { |e| e.attribute == :field_35 }.message).to eql("You told us this person is under 60 and retired. The minimum expected retirement age for females in England is 60.") + end + end + end end describe "#log" do