From 90e0bad2b7cf29de25eaee3059e127890c9dba38 Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Wed, 1 Feb 2023 16:53:56 +0000 Subject: [PATCH] bulk upload sends success email (#1261) - this happens if the csv is perfect and all logs are created as completed --- app/mailers/bulk_upload_mailer.rb | 26 +++++++++++++---- app/models/bulk_upload.rb | 8 ++++++ app/services/bulk_upload/processor.rb | 11 +++++++ spec/mailers/bulk_upload_mailer_spec.rb | 32 +++++++++++++++++++++ spec/services/bulk_upload/processor_spec.rb | 31 +++++++++++++++++--- 5 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 spec/mailers/bulk_upload_mailer_spec.rb diff --git a/app/mailers/bulk_upload_mailer.rb b/app/mailers/bulk_upload_mailer.rb index 5ab0d70b0..b406a5c4b 100644 --- a/app/mailers/bulk_upload_mailer.rb +++ b/app/mailers/bulk_upload_mailer.rb @@ -1,20 +1,34 @@ class BulkUploadMailer < NotifyMailer + include ActionView::Helpers::TextHelper + BULK_UPLOAD_COMPLETE_TEMPLATE_ID = "83279578-c890-4168-838b-33c9cf0dc9f0".freeze BULK_UPLOAD_FAILED_CSV_ERRORS_TEMPLATE_ID = "e27abcd4-5295-48c2-b127-e9ee4b781b75".freeze BULK_UPLOAD_FAILED_FILE_SETUP_ERROR_TEMPLATE_ID = "24c9f4c7-96ad-470a-ba31-eb51b7cbafd9".freeze BULK_UPLOAD_FAILED_SERVICE_ERROR_TEMPLATE_ID = "c3f6288c-7a74-4e77-99ee-6c4a0f6e125a".freeze BULK_UPLOAD_WITH_ERRORS_TEMPLATE_ID = "eb539005-6234-404e-812d-167728cf4274".freeze - def send_bulk_upload_complete_mail(user, bulk_upload) + def send_bulk_upload_complete_mail(user:, bulk_upload:) + url = if bulk_upload.lettings? + lettings_logs_url + else + sales_logs_url + end + + n_logs = pluralize(bulk_upload.logs.count, "log") + + title = "You’ve successfully uploaded #{n_logs}" + + success_description = "The #{bulk_upload.log_type} #{bulk_upload.year_combo} data you uploaded has been checked. The #{n_logs} you uploaded are now complete." + send_email( user.email, BULK_UPLOAD_COMPLETE_TEMPLATE_ID, { - title: "[#{bulk_upload} title]", - filename: "[#{bulk_upload} filename]", - upload_timestamp: "[#{bulk_upload} upload_timestamp]", - success_description: "[#{bulk_upload} success_description]", - logs_link: "[#{bulk_upload} logs_link]", + title:, + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at, + success_description:, + logs_link: url, }, ) end diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index 3d0ef93f0..56e10f399 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -14,6 +14,14 @@ class BulkUpload < ApplicationRecord "#{year}/#{year - 2000 + 1}" end + def logs + if lettings? + lettings_logs + else + sales_logs + end + end + private def generate_identifier diff --git a/app/services/bulk_upload/processor.rb b/app/services/bulk_upload/processor.rb index c8c8398c2..6b444a205 100644 --- a/app/services/bulk_upload/processor.rb +++ b/app/services/bulk_upload/processor.rb @@ -9,12 +9,23 @@ class BulkUpload::Processor download validator.call create_logs if validator.create_logs? + send_success_mail ensure downloader.delete_local_file! end private + def send_success_mail + if validator.create_logs? && bulk_upload.logs.group(:status).count.keys == %w[completed] + BulkUploadMailer.send_bulk_upload_complete_mail(user:, bulk_upload:).deliver_later + end + end + + def user + bulk_upload.user + end + def create_logs log_creator_class.new( bulk_upload:, diff --git a/spec/mailers/bulk_upload_mailer_spec.rb b/spec/mailers/bulk_upload_mailer_spec.rb new file mode 100644 index 000000000..526f3584a --- /dev/null +++ b/spec/mailers/bulk_upload_mailer_spec.rb @@ -0,0 +1,32 @@ +require "rails_helper" + +RSpec.describe BulkUploadMailer do + subject(:mailer) { described_class.new } + + let(:notify_client) { instance_double(Notifications::Client) } + let(:user) { create(:user, email: "user@example.com") } + let(:bulk_upload) { build(:bulk_upload, :lettings) } + + before do + allow(Notifications::Client).to receive(:new).and_return(notify_client) + allow(notify_client).to receive(:send_email).and_return(true) + end + + describe "#send_bulk_upload_complete_mail" do + it "sends correctly formed email" do + expect(notify_client).to receive(:send_email).with( + email_address: user.email, + template_id: described_class::BULK_UPLOAD_COMPLETE_TEMPLATE_ID, + personalisation: { + title: "You’ve successfully uploaded 0 logs", + filename: bulk_upload.filename, + upload_timestamp: bulk_upload.created_at, + success_description: "The lettings 2022/23 data you uploaded has been checked. The 0 logs you uploaded are now complete.", + logs_link: lettings_logs_url, + }, + ) + + mailer.send_bulk_upload_complete_mail(user:, bulk_upload:) + end + end +end diff --git a/spec/services/bulk_upload/processor_spec.rb b/spec/services/bulk_upload/processor_spec.rb index aaff12678..27fd9edd3 100644 --- a/spec/services/bulk_upload/processor_spec.rb +++ b/spec/services/bulk_upload/processor_spec.rb @@ -16,19 +16,27 @@ RSpec.describe BulkUpload::Processor do ) end - it "persist the validation errors" do + before do allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) + end + it "persist the validation errors" do expect { processor.call }.to change(BulkUploadError, :count) end it "deletes the local file afterwards" do - allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) - processor.call expect(mock_downloader).to have_received(:delete_local_file!) end + + it "does not send success email" do + allow(BulkUploadMailer).to receive(:send_bulk_upload_complete_mail).and_call_original + + processor.call + + expect(BulkUploadMailer).not_to have_received(:send_bulk_upload_complete_mail) + end end context "when processing a bulk with perfect data" do @@ -59,15 +67,30 @@ RSpec.describe BulkUpload::Processor do ) end - it "creates logs" do + before do allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) allow(BulkUpload::Lettings::Validator).to receive(:new).and_return(mock_validator) allow(BulkUpload::Lettings::LogCreator).to receive(:new).with(bulk_upload:, path:).and_return(mock_creator) + end + it "creates logs" do processor.call expect(mock_creator).to have_received(:call) end + + it "sends success email" do + mail_double = instance_double("ActionMailer::MessageDelivery", deliver_later: nil) + + allow(BulkUploadMailer).to receive(:send_bulk_upload_complete_mail).and_return(mail_double) + + create(:lettings_log, :completed, bulk_upload:) + + processor.call + + expect(BulkUploadMailer).to have_received(:send_bulk_upload_complete_mail) + expect(mail_double).to have_received(:deliver_later) + end end end end