From c481055f4dc6fb6a913f31499cf38ea6c3c9a79f Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Fri, 10 Feb 2023 16:46:00 +0000 Subject: [PATCH] CLDC-1965 Bulk upload correct again email (#1274) * tests for bulk upload mailer * bulk upload send upload again email --- app/mailers/bulk_upload_mailer.rb | 28 ++++++++--- app/models/bulk_upload.rb | 8 +++ app/services/bulk_upload/processor.rb | 5 ++ spec/mailers/bulk_upload_mailer_spec.rb | 54 ++++++++++++++++++++- spec/services/bulk_upload/processor_spec.rb | 11 +++++ 5 files changed, 97 insertions(+), 9 deletions(-) diff --git a/app/mailers/bulk_upload_mailer.rb b/app/mailers/bulk_upload_mailer.rb index 59d03ce9b..be0a71e05 100644 --- a/app/mailers/bulk_upload_mailer.rb +++ b/app/mailers/bulk_upload_mailer.rb @@ -33,17 +33,29 @@ class BulkUploadMailer < NotifyMailer ) end - def send_bulk_upload_failed_csv_errors_mail(user, bulk_upload) + def columns_with_errors(bulk_upload:) + array = bulk_upload.columns_with_errors + + if array.size > 3 + "#{array.take(3).join(', ')} and more" + else + array.join(", ") + end + end + + def send_correct_and_upload_again_mail(bulk_upload:) + error_description = "We noticed that you have a lot of similar errors in column #{columns_with_errors(bulk_upload:)}. Please correct your data export and upload again." + send_email( - user.email, + bulk_upload.user.email, BULK_UPLOAD_FAILED_CSV_ERRORS_TEMPLATE_ID, { - filename: "[#{bulk_upload} filename]", - upload_timestamp: "[#{bulk_upload} upload_timestamp]", - year_combo: "[#{bulk_upload} year_combo]", - lettings_or_sales: "[#{bulk_upload} lettings_or_sales]", - error_description: "[#{bulk_upload} error_description]", - summary_report_link: "[#{bulk_upload} summary_report_link]", + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at.to_fs(:govuk_date_and_time), + year_combo: bulk_upload.year_combo, + lettings_or_sales: bulk_upload.log_type, + error_description:, + summary_report_link: summary_bulk_upload_lettings_result_url(bulk_upload), }, ) end diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index cf2965446..8385b9ba4 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -30,6 +30,14 @@ class BulkUpload < ApplicationRecord end end + def columns_with_errors + bulk_upload_errors + .select(:col) + .distinct(:col) + .pluck(:col) + .sort_by { |col| col.rjust(2, "0") } + end + def general_needs? needstype == 1 end diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index d69f76243..bbf7e6cd1 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -13,6 +13,7 @@ class BulkUpload::Processor validator.call create_logs if validator.create_logs? + send_correct_and_upload_again_mail unless validator.create_logs? send_fix_errors_mail if created_logs_but_incompleted? send_success_mail if created_logs_and_all_completed? @@ -25,6 +26,10 @@ class BulkUpload::Processor private + def send_correct_and_upload_again_mail + BulkUploadMailer.send_correct_and_upload_again_mail(bulk_upload:).deliver_later + end + def send_fix_errors_mail BulkUploadMailer.send_bulk_upload_with_errors_mail(bulk_upload:).deliver_later end diff --git a/spec/mailers/bulk_upload_mailer_spec.rb b/spec/mailers/bulk_upload_mailer_spec.rb index a3706beb3..d0217bfaf 100644 --- a/spec/mailers/bulk_upload_mailer_spec.rb +++ b/spec/mailers/bulk_upload_mailer_spec.rb @@ -5,7 +5,7 @@ RSpec.describe BulkUploadMailer do let(:notify_client) { instance_double(Notifications::Client) } let(:user) { create(:user, email: "user@example.com") } - let(:bulk_upload) { build(:bulk_upload, :lettings, user:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } before do allow(Notifications::Client).to receive(:new).and_return(notify_client) @@ -75,4 +75,56 @@ RSpec.describe BulkUploadMailer do end end end + + describe "#send_correct_and_upload_again_mail" do + context "when 2 columns with errors" do + before do + create(:bulk_upload_error, bulk_upload:, col: "A") + create(:bulk_upload_error, bulk_upload:, col: "B") + end + + it "sends correctly formed email with A, B" do + expect(notify_client).to receive(:send_email).with( + email_address: user.email, + template_id: described_class::BULK_UPLOAD_FAILED_CSV_ERRORS_TEMPLATE_ID, + personalisation: { + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at.to_fs(:govuk_date_and_time), + year_combo: bulk_upload.year_combo, + lettings_or_sales: bulk_upload.log_type, + error_description: "We noticed that you have a lot of similar errors in column A, B. Please correct your data export and upload again.", + summary_report_link: "http://localhost:3000/lettings-logs/bulk-upload-results/#{bulk_upload.id}/summary", + }, + ) + + mailer.send_correct_and_upload_again_mail(bulk_upload:) + end + end + + context "when 4 columns with errors" do + before do + create(:bulk_upload_error, bulk_upload:, col: "A") + create(:bulk_upload_error, bulk_upload:, col: "B") + create(:bulk_upload_error, bulk_upload:, col: "C") + create(:bulk_upload_error, bulk_upload:, col: "D") + end + + it "sends correctly formed email with A, B, C and more" do + expect(notify_client).to receive(:send_email).with( + email_address: user.email, + template_id: described_class::BULK_UPLOAD_FAILED_CSV_ERRORS_TEMPLATE_ID, + personalisation: { + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at.to_fs(:govuk_date_and_time), + year_combo: bulk_upload.year_combo, + lettings_or_sales: bulk_upload.log_type, + error_description: "We noticed that you have a lot of similar errors in column A, B, C and more. Please correct your data export and upload again.", + summary_report_link: "http://localhost:3000/lettings-logs/bulk-upload-results/#{bulk_upload.id}/summary", + }, + ) + + mailer.send_correct_and_upload_again_mail(bulk_upload:) + end + end + end end diff --git a/spec/services/bulk_upload/processor_spec.rb b/spec/services/bulk_upload/processor_spec.rb index b751e7dd7..d56b2ff4e 100644 --- a/spec/services/bulk_upload/processor_spec.rb +++ b/spec/services/bulk_upload/processor_spec.rb @@ -171,6 +171,17 @@ RSpec.describe BulkUpload::Processor do expect(mock_downloader).to have_received(:delete_local_file!) end + it "sends correct and upload again mail" do + mail_double = instance_double("ActionMailer::MessageDelivery", deliver_later: nil) + + allow(BulkUploadMailer).to receive(:send_correct_and_upload_again_mail).and_return(mail_double) + + processor.call + + expect(BulkUploadMailer).to have_received(:send_correct_and_upload_again_mail) + expect(mail_double).to have_received(:deliver_later) + end + it "does not send fix errors email" do allow(BulkUploadMailer).to receive(:send_bulk_upload_with_errors_mail).and_call_original