From ffc38f18a630b1119c4dfc6739a50cd67c241108 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 27 Apr 2023 16:04:08 +0100 Subject: [PATCH] Add soft validations to lettings logs 23/24 bulk upload --- .../lettings/year2023/row_parser.rb | 58 +++++++++++++++++++ .../lettings/year2023/row_parser_spec.rb | 22 +++++++ 2 files changed, 80 insertions(+) diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 4393d1af2..29832cf1a 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -339,6 +339,8 @@ class BulkUpload::Lettings::Year2023::RowParser validate :validate_uprn_exists_if_any_key_adddress_fields_are_blank, on: :after_log + validate :validate_incomplete_soft_validations, on: :after_log + def self.question_for_field(field) QUESTIONS[field] end @@ -457,6 +459,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 duplicate_check_fields %w[ startdate @@ -1389,4 +1407,44 @@ private 0 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/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index 58b216e43..2be59d860 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -984,6 +984,28 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end end + + describe "soft validations" do + context "when soft validation is triggered" do + let(:attributes) { setup_section_params.merge({ field_46: 22, field_50: 5, field_47: "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_46 }).to be_present + expect(soft_validation_errors.find { |e| e.attribute == :field_47 }).to be_present + expect(soft_validation_errors.find { |e| e.attribute == :field_50 }).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_46 }.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_47 }.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_50 }.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