diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 2eac1d6c4..c6c92b58e 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -765,9 +765,8 @@ private 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 = log.form.questions.filter { |q| q.type == "interruption_screen" && q.page.routed_to?(log, nil) }.compact 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| diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 851fffd7b..079e3f683 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -510,9 +510,8 @@ private 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 = log.form.questions.filter { |q| q.type == "interruption_screen" && q.page.routed_to?(log, nil) }.compact 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| diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index 02427fe10..83523d3e8 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -990,9 +990,8 @@ private 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 = log.form.questions.filter { |q| q.type == "interruption_screen" && q.page.routed_to?(log, nil) }.compact 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| diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index 994542bc4..f9b690bbd 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 @@ -1213,4 +1215,20 @@ 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) }.compact + routed_to_soft_validation_questions.each do |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/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index a6f614a34..cc7df60e3 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -187,7 +187,7 @@ RSpec.describe SalesLogsController, type: :request do it "does not render pending logs" do get "/sales-logs", headers: headers, params: {} - expect(page).not_to have_content(invisible_log.id) + expect(page).not_to have_link(invisible_log.id, href: "sales-logs/#{invisible_log.id}") 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 0587c0f51..0e0a3c961 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -978,17 +978,13 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do let(:attributes) { setup_section_params.merge({ field_12: 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_12 }).to be_present - expect(soft_validation_errors.find { |e| e.attribute == :field_35 }).to be_present + expect(parser.errors.where(:field_12, category: :soft_validation)).to be_present + expect(parser.errors.where(:field_35, category: :soft_validation)).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 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.") + expect(parser.errors.where(:field_12, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") + expect(parser.errors.where(:field_35, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") 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 4cd9e40bc..d0e18c915 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -1009,17 +1009,13 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do let(:attributes) { setup_section_params.merge({ field_46: 22, field_50: 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_46 }).to be_present - expect(soft_validation_errors.find { |e| e.attribute == :field_50 }).to be_present + expect(parser.errors.where(:field_46, category: :soft_validation)).to be_present + expect(parser.errors.where(:field_50, category: :soft_validation)).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 aged 22 years and retired.") - expect(soft_validation_errors.find { |e| e.attribute == :field_50 }.message).to eql("You told us this person is aged 22 years and retired.") + expect(parser.errors.where(:field_46, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") + expect(parser.errors.where(:field_50, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") end end @@ -1027,16 +1023,12 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do let(:attributes) { setup_section_params.merge({ field_128: 120, field_126: 1, field_32: 1, field_4: 1, field_5: "3", field_25: "E09000008" }) } 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_128 }).to be_present + expect(parser.errors.where(:field_128, category: :soft_validation)).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.count { |e| e.attribute == :field_128 }).to be(1) - expect(soft_validation_errors.find { |e| e.attribute == :field_128 }.message).to eql("You told us the rent is £120.00 every week. The maximum rent expected for this type of property in this local authority is ££118.85 every week.") + expect(parser.errors.where(:field_128, category: :soft_validation).count).to be(1) + expect(parser.errors.where(:field_128, category: :soft_validation).first.message).to eql("You told us the rent is £120.00 every week. The maximum rent expected for this type of property in this local authority is ££118.85 every week.") 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 e83419ad6..7966d4921 100644 --- a/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2022/row_parser_spec.rb @@ -544,17 +544,13 @@ RSpec.describe BulkUpload::Sales::Year2022::RowParser do let(:attributes) { valid_attributes.merge({ field_7: 22, field_24: 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_7 }).to be_present - expect(soft_validation_errors.find { |e| e.attribute == :field_24 }).to be_present + expect(parser.errors.where(:field_7, category: :soft_validation)).to be_present + expect(parser.errors.where(:field_24, category: :soft_validation)).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 aged 22 years and retired.") - expect(soft_validation_errors.find { |e| e.attribute == :field_24 }.message).to eql("You told us this person is aged 22 years and retired.") + expect(parser.errors.where(:field_7, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") + expect(parser.errors.where(:field_24, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") 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 84e0e0cc5..4cfbf9b83 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -703,6 +703,22 @@ 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 + expect(parser.errors.where(:field_30, category: :soft_validation)).to be_present + expect(parser.errors.where(:field_35, category: :soft_validation)).to be_present + end + + it "populates with correct error message" do + expect(parser.errors.where(:field_30, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") + expect(parser.errors.where(:field_30, category: :soft_validation).first.message).to eql("You told us this person is aged 22 years and retired.") + end + end + end end describe "#log" do