diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index 741536f61..3dba8cfec 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -1,6 +1,7 @@ class BulkUpload::Sales::Year2023::RowParser include ActiveModel::Model include ActiveModel::Attributes + include InterruptionScreenHelper QUESTIONS = { field_1: "Which organisation owned this property before the sale?", @@ -405,6 +406,7 @@ class BulkUpload::Sales::Year2023::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 validate :validate_uprn_exists_if_any_key_adddress_fields_are_blank, on: :after_log validate :validate_address_line_1, on: :after_log @@ -1209,4 +1211,21 @@ private errors.add(:field_6, error_message) # Purchaser code 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 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| + unless errors.any? { |e| e.options[:category] == :soft_validation && field_mapping_for_errors[interruption_screen_question_id.to_sym].include?(e.attribute) } + error_message = [display_title_text(question.page.title_text, log), display_informative_text(question.page.informative_text, log)].reject(&:empty?).join(". ") + errors.add(field, message: error_message, category: :soft_validation) + end + end + end + end + end end diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb index ecbd457fb..f58e3b17d 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -684,6 +684,26 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do end end end + + describe "soft validations" do + context "when soft validation is triggered" do + let(:attributes) { valid_attributes.merge({ field_30: 22, field_35: 5 }) } + + 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_30 }).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_30 }.message).to eql("You told us this person is aged 22 years and retired.") + expect(soft_validation_errors.find { |e| e.attribute == :field_35 }.message).to eql("You told us this person is aged 22 years and retired.") + end + end + end end describe "#log" do