From 23ea9c88ae3c1e187b371747ed37c2f9878ce920 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Fri, 3 Feb 2023 12:29:07 +0000 Subject: [PATCH] add bulk upload mailer test --- app/mailers/bulk_upload_mailer.rb | 22 ++++++++++++------- spec/mailers/bulk_upload_mailer_spec.rb | 28 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/mailers/bulk_upload_mailer.rb b/app/mailers/bulk_upload_mailer.rb index ac8f626f3..7b985d9b5 100644 --- a/app/mailers/bulk_upload_mailer.rb +++ b/app/mailers/bulk_upload_mailer.rb @@ -83,16 +83,24 @@ class BulkUploadMailer < NotifyMailer ) end - def send_bulk_upload_with_errors_mail(user, bulk_upload) + def send_bulk_upload_with_errors_mail(bulk_upload:) + n = bulk_upload.logs.where.not(status: %w[completed]).count + + n_logs = pluralize(n, "log") + + title = "We found #{n_logs} with errors" + + error_description = "We created logs from your #{bulk_upload.year_combo} #{bulk_upload.log_type} data. There was a problem with #{n} of the logs. Click the below link to fix these logs." + send_email( - user.email, + bulk_upload.user.email, BULK_UPLOAD_WITH_ERRORS_TEMPLATE_ID, { - title: "[#{bulk_upload} title]", - filename: "[#{bulk_upload} filename]", - upload_timestamp: "[#{bulk_upload} upload_timestamp]", - error_description: "[#{bulk_upload} error_description]", - summary_report_link: "[#{bulk_upload} summary_report_link]", + title:, + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at.to_fs(:govuk_date_and_time), + error_description:, + summary_report_link: summary_bulk_upload_lettings_result_url(bulk_upload), }, ) end diff --git a/spec/mailers/bulk_upload_mailer_spec.rb b/spec/mailers/bulk_upload_mailer_spec.rb index 3d7c0454f..d25e88a63 100644 --- a/spec/mailers/bulk_upload_mailer_spec.rb +++ b/spec/mailers/bulk_upload_mailer_spec.rb @@ -47,4 +47,32 @@ RSpec.describe BulkUploadMailer do mailer.send_bulk_upload_failed_service_error_mail(bulk_upload:) end end + + context "when bulk upload has log which is not completed" do + before do + create(:lettings_log, :in_progress, bulk_upload:) + end + + describe "#send_bulk_upload_with_errors_mail" do + let(:error_description) do + "We created logs from your 2022/23 lettings data. There was a problem with 1 of the logs. Click the below link to fix these logs." + end + + it "sends correctly formed email" do + expect(notify_client).to receive(:send_email).with( + email_address: bulk_upload.user.email, + template_id: described_class::BULK_UPLOAD_WITH_ERRORS_TEMPLATE_ID, + personalisation: { + title: "We found 1 log with errors", + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at.to_fs(:govuk_date_and_time), + error_description:, + summary_report_link: "http://localhost:3000/lettings-logs/bulk-upload-results/#{bulk_upload.id}/summary", + }, + ) + + mailer.send_bulk_upload_with_errors_mail(bulk_upload:) + end + end + end end