From 99cb26b93471d5de2f8f0cb37e15509a522ee76a Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 4 May 2023 17:57:41 +0100 Subject: [PATCH] CLDC-2296 Display soft validation errors on bulk upload (#1594) * Add affected_question_ids to pregnancy check * Update is_referrer_interruption_screen? check and naming * Use interruption_screen_question_ids to set soft validation errors on relevant fields * Add soft validations to sales bulk upload * Add soft validations to lettings logs 23/24 bulk upload * Add errors for optional soft validations * Only add soft validations once * Import helper methods * Update test based on new validation messages * Rebase fix --- .../lettings/year2022/row_parser.rb | 19 +++++++ .../lettings/year2023/row_parser.rb | 20 ++++++++ .../bulk_upload/sales/year2022/row_parser.rb | 19 +++++++ .../lettings/year2022/row_parser_spec.rb | 20 ++++++++ .../lettings/year2023/row_parser_spec.rb | 49 +++++++++++++++++++ .../sales/year2022/row_parser_spec.rb | 20 ++++++++ 6 files changed, 147 insertions(+) diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 9d273dab5..80b65c133 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -1,6 +1,7 @@ class BulkUpload::Lettings::Year2022::RowParser include ActiveModel::Model include ActiveModel::Attributes + include InterruptionScreenHelper QUESTIONS = { field_1: "What is the letting type?", @@ -335,6 +336,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 +718,23 @@ 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 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 + def setup_question?(question) log.form.setup_sections[0].subsections[0].questions.include?(question) end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 4393d1af2..46ff0ff12 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -1,6 +1,7 @@ class BulkUpload::Lettings::Year2023::RowParser include ActiveModel::Model include ActiveModel::Attributes + include InterruptionScreenHelper QUESTIONS = { field_1: "Which organisation owns this property?", @@ -339,6 +340,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 +460,23 @@ 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 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| 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 + def duplicate_check_fields %w[ startdate diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index b08db8585..e53b42ecb 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -1,6 +1,7 @@ class BulkUpload::Sales::Year2022::RowParser include ActiveModel::Model include ActiveModel::Attributes + include InterruptionScreenHelper QUESTIONS = { field_1: "What is the purchaser code?", @@ -283,6 +284,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 +946,21 @@ 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 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/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index dd9bf4a5b..25fc64cf9 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,26 @@ 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 }) } + + 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 + 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.") + end + end + end end describe "#log" do 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..f98eed6d0 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -31,6 +31,18 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do before do create(:organisation_relationship, parent_organisation: owning_org, child_organisation: managing_org) + + LaRentRange.create!( + ranges_rent_id: "1", + la: "E09000008", + beds: 1, + lettype: 3, + soft_min: 12.41, + soft_max: 118.85, + hard_min: 9.87, + hard_max: 200.99, + start_year: 2023, + ) end around do |example| @@ -984,6 +996,43 @@ 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 }) } + + 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 + 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.") + end + end + + context "when soft validation is triggered and not required" 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 + 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.") + end + end + end end describe "#log" do 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..8c0a4cd45 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,25 @@ 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 }) } + + 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 + 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.") + end + end + end end end