diff --git a/app/mailers/bulk_upload_mailer.rb b/app/mailers/bulk_upload_mailer.rb index c6e59b15e..e1c28a056 100644 --- a/app/mailers/bulk_upload_mailer.rb +++ b/app/mailers/bulk_upload_mailer.rb @@ -6,6 +6,7 @@ class BulkUploadMailer < NotifyMailer FAILED_FILE_SETUP_ERROR_TEMPLATE_ID = "24c9f4c7-96ad-470a-ba31-eb51b7cbafd9".freeze FAILED_SERVICE_ERROR_TEMPLATE_ID = "c3f6288c-7a74-4e77-99ee-6c4a0f6e125a".freeze HOW_FIX_UPLOAD_TEMPLATE_ID = "21a07b26-f625-4846-9f4d-39e30937aa24".freeze + CHECK_SOFT_VALIDATIONS_TEMPLATE_ID = "123".freeze def send_how_fix_upload_mail(bulk_upload:) title = "We found #{pluralize(bulk_upload.bulk_upload_errors.count, 'error')} in your bulk upload" @@ -25,6 +26,24 @@ class BulkUploadMailer < NotifyMailer ) end + def send_check_soft_validations_mail(bulk_upload:) + title = "Check your file data" + description = "Some of your #{bulk_upload.year_combo} #{bulk_upload.log_type} data might not be right. Click the link below to review the potential errors, and check your file to see if the data is correct." + cta_link = bulk_upload.sales? ? bulk_upload_sales_check_data_url(bulk_upload) : bulk_upload_lettings_check_data_url(bulk_upload) + + send_email( + bulk_upload.user.email, + CHECK_SOFT_VALIDATIONS_TEMPLATE_ID, + { + title:, + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at.to_fs(:govuk_date_and_time), + description:, + cta_link:, + }, + ) + end + def send_bulk_upload_complete_mail(user:, bulk_upload:) url = if bulk_upload.lettings? lettings_logs_url("[years][]" => "", "[status][]" => "", user: :all, organisation_select: :all) diff --git a/app/services/bulk_upload/lettings/validator.rb b/app/services/bulk_upload/lettings/validator.rb index c24e67235..3a2bf073e 100644 --- a/app/services/bulk_upload/lettings/validator.rb +++ b/app/services/bulk_upload/lettings/validator.rb @@ -63,6 +63,24 @@ class BulkUpload::Lettings::Validator .positive? end + def soft_validation_errors_only? + errors = bulk_upload.bulk_upload_errors + errors.count == errors.where(category: "soft_validation").count + end + + def over_column_error_threshold? + fields = ("field_1".."field_134").to_a + percentage_threshold = (row_parsers.size * COLUMN_PERCENTAGE_ERROR_THRESHOLD).ceil + + fields.any? do |field| + count = row_parsers.count { |row_parser| row_parser.errors[field].present? } + + next if count < COLUMN_ABSOLUTE_ERROR_THRESHOLD + + count > percentage_threshold + end + end + def any_logs_already_exist? row_parsers.any?(&:log_already_exists?) end diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index 66c6b9d10..649984c1c 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -18,7 +18,9 @@ class BulkUpload::Processor elsif validator.create_logs? create_logs - if created_logs_but_incompleted? + if validator.soft_validation_errors_only? + send_check_soft_validations_mail + elsif created_logs_but_incompleted? send_how_fix_upload_mail elsif created_logs_and_all_completed? bulk_upload.unpend @@ -46,6 +48,12 @@ private .deliver_later end + def send_check_soft_validations_mail + BulkUploadMailer + .send_check_soft_validations_mail(bulk_upload:) + .deliver_later + end + def send_setup_errors_mail BulkUploadMailer .send_bulk_upload_failed_file_setup_error_mail(bulk_upload:) diff --git a/spec/services/bulk_upload/processor_spec.rb b/spec/services/bulk_upload/processor_spec.rb index 0c9d50be8..3fe22c439 100644 --- a/spec/services/bulk_upload/processor_spec.rb +++ b/spec/services/bulk_upload/processor_spec.rb @@ -236,6 +236,74 @@ RSpec.describe BulkUpload::Processor do end end + context "when a bulk upload has logs with only soft validations triggered" do + let(:mock_downloader) do + instance_double( + BulkUpload::Downloader, + call: nil, + path:, + delete_local_file!: nil, + ) + end + + let(:file) { Tempfile.new } + let(:path) { file.path } + + let(:log) do + build( + :lettings_log, + :completed, + renttype: 3, + age1: 20, + ecstat1: 5, + owning_organisation: owning_org, + managing_organisation: owning_org, + created_by: nil, + national: 18, + waityear: 9, + joint: 2, + tenancy: 2, + ppcodenk: 0, + voiddate: Date.new(2022, 1, 1), + reason: 40, + leftreg: 3, + mrcdate: nil, + startdate: Date.new(2022, 10, 1), + tenancylength: nil, + ) + end + + before do + FormHandler.instance.use_real_forms! + file.write(BulkUpload::LettingsLogToCsv.new(log:, col_offset: 0).to_2022_csv_row) + file.rewind + + allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) + allow(FeatureToggle).to receive(:bulk_upload_duplicate_log_check_enabled?).and_return(true) + end + + after do + FormHandler.instance.use_fake_forms! + end + + it "creates pending log" do + expect { processor.call }.to change(LettingsLog.pending, :count).by(1) + end + + it "sends check_soft_validations_mail" do + mail_double = instance_double("ActionMailer::MessageDelivery", deliver_later: nil) + + allow(BulkUploadMailer).to receive(:send_check_soft_validations_mail).and_return(mail_double) + allow(BulkUploadMailer).to receive(:send_how_fix_upload_mail).and_return(mail_double) + + processor.call + + expect(BulkUploadMailer).to have_received(:send_check_soft_validations_mail) + expect(BulkUploadMailer).not_to have_received(:send_how_fix_upload_mail) + expect(mail_double).to have_received(:deliver_later) + end + end + context "when upload has no setup errors something blocks log creation" do let(:mock_downloader) do instance_double( diff --git a/spec/support/bulk_upload/lettings_log_to_csv.rb b/spec/support/bulk_upload/lettings_log_to_csv.rb index 6d3110b2a..ad9985a4e 100644 --- a/spec/support/bulk_upload/lettings_log_to_csv.rb +++ b/spec/support/bulk_upload/lettings_log_to_csv.rb @@ -123,7 +123,7 @@ class BulkUpload::LettingsLogToCsv log.benefits, log.earnings, # 50 net_income_known, - nil, + log.reason, log.reasonother, nil, nil,