Browse Source

bulk upload sends success email (#1261)

- this happens if the csv is perfect and all logs are created as
  completed
pull/1264/head
Phil Lee 2 years ago committed by GitHub
parent
commit
90e0bad2b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      app/mailers/bulk_upload_mailer.rb
  2. 8
      app/models/bulk_upload.rb
  3. 11
      app/services/bulk_upload/processor.rb
  4. 32
      spec/mailers/bulk_upload_mailer_spec.rb
  5. 31
      spec/services/bulk_upload/processor_spec.rb

26
app/mailers/bulk_upload_mailer.rb

@ -1,20 +1,34 @@
class BulkUploadMailer < NotifyMailer class BulkUploadMailer < NotifyMailer
include ActionView::Helpers::TextHelper
BULK_UPLOAD_COMPLETE_TEMPLATE_ID = "83279578-c890-4168-838b-33c9cf0dc9f0".freeze 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_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_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_FAILED_SERVICE_ERROR_TEMPLATE_ID = "c3f6288c-7a74-4e77-99ee-6c4a0f6e125a".freeze
BULK_UPLOAD_WITH_ERRORS_TEMPLATE_ID = "eb539005-6234-404e-812d-167728cf4274".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( send_email(
user.email, user.email,
BULK_UPLOAD_COMPLETE_TEMPLATE_ID, BULK_UPLOAD_COMPLETE_TEMPLATE_ID,
{ {
title: "[#{bulk_upload} title]", title:,
filename: "[#{bulk_upload} filename]", filename: bulk_upload.filename,
upload_timestamp: "[#{bulk_upload} upload_timestamp]", upload_timestamp: bulk_upload.created_at,
success_description: "[#{bulk_upload} success_description]", success_description:,
logs_link: "[#{bulk_upload} logs_link]", logs_link: url,
}, },
) )
end end

8
app/models/bulk_upload.rb

@ -14,6 +14,14 @@ class BulkUpload < ApplicationRecord
"#{year}/#{year - 2000 + 1}" "#{year}/#{year - 2000 + 1}"
end end
def logs
if lettings?
lettings_logs
else
sales_logs
end
end
private private
def generate_identifier def generate_identifier

11
app/services/bulk_upload/processor.rb

@ -9,12 +9,23 @@ class BulkUpload::Processor
download download
validator.call validator.call
create_logs if validator.create_logs? create_logs if validator.create_logs?
send_success_mail
ensure ensure
downloader.delete_local_file! downloader.delete_local_file!
end end
private 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 def create_logs
log_creator_class.new( log_creator_class.new(
bulk_upload:, bulk_upload:,

32
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

31
spec/services/bulk_upload/processor_spec.rb

@ -16,19 +16,27 @@ RSpec.describe BulkUpload::Processor do
) )
end end
it "persist the validation errors" do before do
allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) 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) expect { processor.call }.to change(BulkUploadError, :count)
end end
it "deletes the local file afterwards" do it "deletes the local file afterwards" do
allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader)
processor.call processor.call
expect(mock_downloader).to have_received(:delete_local_file!) expect(mock_downloader).to have_received(:delete_local_file!)
end 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 end
context "when processing a bulk with perfect data" do context "when processing a bulk with perfect data" do
@ -59,15 +67,30 @@ RSpec.describe BulkUpload::Processor do
) )
end end
it "creates logs" do before do
allow(BulkUpload::Downloader).to receive(:new).with(bulk_upload:).and_return(mock_downloader) 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::Validator).to receive(:new).and_return(mock_validator)
allow(BulkUpload::Lettings::LogCreator).to receive(:new).with(bulk_upload:, path:).and_return(mock_creator) allow(BulkUpload::Lettings::LogCreator).to receive(:new).with(bulk_upload:, path:).and_return(mock_creator)
end
it "creates logs" do
processor.call processor.call
expect(mock_creator).to have_received(:call) expect(mock_creator).to have_received(:call)
end 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 end
end end

Loading…
Cancel
Save