From ea45229317565a6bfaa67c79fb0147de2532e591 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 27 Apr 2023 16:03:41 +0100 Subject: [PATCH] Add soft validations to sales bulk upload --- .../bulk_upload/sales/year2022/row_parser.rb | 57 +++++++++++++++++++ .../sales/year2022/row_parser_spec.rb | 22 +++++++ 2 files changed, 79 insertions(+) diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index b08db8585..44c0439c9 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -283,6 +283,7 @@ class BulkUpload::Sales::Year2022::RowParser validate :validate_created_by_exists, on: :after_log validate :validate_created_by_related, on: :after_log validate :validate_relevant_collection_window, on: :after_log + validate :validate_incomplete_soft_validations, on: :after_log def self.question_for_field(field) QUESTIONS[field] @@ -944,4 +945,60 @@ private errors.add(:field_4, I18n.t("validations.date.outside_collection_window")) 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 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/sales/year2022/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb index bd5520ea7..70bed8aa6 100644 --- a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb @@ -520,5 +520,27 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do end end end + + describe "soft validations" do + context "when soft validation is triggered" do + let(:attributes) { valid_attributes.merge({ field_7: 22, field_24: 5, field_13: "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_7 }).to be_present + expect(soft_validation_errors.find { |e| e.attribute == :field_24 }).to be_present + expect(soft_validation_errors.find { |e| e.attribute == :field_13 }).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_7 }.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_24 }.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_13 }.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 end