Browse Source

Send success email to merged users

pull/2078/head
Kat 2 years ago
parent
commit
2a34529e73
  1. 17
      app/mailers/merge_completion_mailer.rb
  2. 9
      app/services/merge/merge_organisations_service.rb
  3. 26
      spec/mailers/merge_completion_mailer_spec.rb
  4. 14
      spec/services/merge/merge_organisations_service_spec.rb

17
app/mailers/merge_completion_mailer.rb

@ -0,0 +1,17 @@
class MergeCompletionMailer < NotifyMailer
MERGE_COMPLETION_TEMPLATE_ID = "xxx".freeze
def send_merge_completion_mail(email, merged_organisation_name, absorbing_organisation_name, merge_date, username)
send_email(
email,
MERGE_COMPLETION_TEMPLATE_ID,
{
merged_organisation_name:,
absorbing_organisation_name:,
merge_date:,
email:,
username:,
},
)
end
end

9
app/services/merge/merge_organisations_service.rb

@ -22,6 +22,7 @@ class Merge::MergeOrganisationsService
end end
@absorbing_organisation.available_from = @merge_date if @absorbing_organisation_active_from_merge_date @absorbing_organisation.available_from = @merge_date if @absorbing_organisation_active_from_merge_date
@absorbing_organisation.save! @absorbing_organisation.save!
send_success_emails
log_success_message log_success_message
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
Rails.logger.error("Organisation merge failed with: #{e.message}") Rails.logger.error("Organisation merge failed with: #{e.message}")
@ -151,6 +152,14 @@ private
end end
end end
def send_success_emails
@merged_users.each do |organisation_name, users|
users.each do |user|
MergeCompletionMailer.send_merge_completion_mail(user[:email], organisation_name, @absorbing_organisation.name, @merge_date, user[:name]).deliver_later
end
end
end
def merge_boolean_organisation_attribute(attribute) def merge_boolean_organisation_attribute(attribute)
@absorbing_organisation[attribute] ||= @merging_organisations.any? { |merging_organisation| merging_organisation[attribute] } @absorbing_organisation[attribute] ||= @merging_organisations.any? { |merging_organisation| merging_organisation[attribute] }
end end

26
spec/mailers/merge_completion_mailer_spec.rb

@ -0,0 +1,26 @@
require "rails_helper"
RSpec.describe MergeCompletionMailer do
let(:notify_client) { instance_double(Notifications::Client) }
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_merge_completion_mail" do
let(:merge_date) { Time.zone.today }
it "sends a merge completion E-mail via notify" do
expect(notify_client).to receive(:send_email).with(hash_including({ personalisation: hash_including({
merged_organisation_name: "merged organisation",
absorbing_organisation_name: "absorbing organisation",
merge_date:,
email: "user@example.com",
username: "user",
}) }))
described_class.new.send_merge_completion_mail("user@example.com", "merged organisation", "absorbing organisation", merge_date, "user")
end
end
end

14
spec/services/merge/merge_organisations_service_spec.rb

@ -2,10 +2,15 @@ require "rails_helper"
RSpec.describe Merge::MergeOrganisationsService do RSpec.describe Merge::MergeOrganisationsService do
describe "#call" do describe "#call" do
before do
mail_double = instance_double("ActionMailer::MessageDelivery", deliver_later: nil)
allow(MergeCompletionMailer).to receive(:send_merge_completion_mail).and_return(mail_double)
end
context "when merging a single organisation into an existing organisation" do context "when merging a single organisation into an existing organisation" do
subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids], merge_date: nil) } subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids], merge_date: nil) }
let(:absorbing_organisation) { create(:organisation, holds_own_stock: false) } let(:absorbing_organisation) { create(:organisation, holds_own_stock: false, name: "absorbing org") }
let(:absorbing_organisation_user) { create(:user, organisation: absorbing_organisation) } let(:absorbing_organisation_user) { create(:user, organisation: absorbing_organisation) }
let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org") } let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org") }
@ -831,6 +836,13 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(absorbing_organisation.available_from.to_date).to eq(Time.zone.today) expect(absorbing_organisation.available_from.to_date).to eq(Time.zone.today)
end end
end end
it "sends a merge completion E-mail to the merged organisation users" do
expect(MergeCompletionMailer).to receive(:send_merge_completion_mail).with(merging_organisation_user.email, "fake org", "absorbing org", Time.zone.today, merging_organisation_user.name).once
expect(MergeCompletionMailer).to receive(:send_merge_completion_mail).with(merging_organisation.data_protection_officers.first.email, "fake org", "absorbing org", Time.zone.today, merging_organisation.data_protection_officers.first.name).once
merge_organisations_service.call
end
end end
context "when merging a multiple organisations into an existing organisation" do context "when merging a multiple organisations into an existing organisation" do

Loading…
Cancel
Save