Browse Source

Send organisation update email

pull/2596/head
Kat 2 years ago
parent
commit
00222f9cbf
  1. 24
      app/models/user.rb
  2. 63
      spec/models/user_spec.rb

24
app/models/user.rb

@ -170,6 +170,7 @@ class User < ApplicationRecord
USER_REACTIVATED_TEMPLATE_ID = "ac45a899-490e-4f59-ae8d-1256fc0001f9".freeze
FOR_OLD_EMAIL_CHANGED_BY_OTHER_USER_TEMPLATE_ID = "3eb80517-1051-4dfc-b4cc-cb18228a3829".freeze
FOR_NEW_EMAIL_CHANGED_BY_OTHER_USER_TEMPLATE_ID = "0cdd0be1-7fa5-4808-8225-ae4c5a002352".freeze
ORGANISATION_UPDATE_TEMPLATE_ID = "4b7716c0-cc5c-41dd-92e4-a0dff03bdf5e".freeze
def reset_password_notify_template
RESET_PASSWORD_TEMPLATE_ID
@ -311,6 +312,7 @@ class User < ApplicationRecord
unassign_organisations(lettings_logs_to_reassign, sales_logs_to_reassign, current_organisation)
end
send_organisation_change_email(current_organisation, new_organisation, log_reassignment, logs_count)
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error("User update failed with: #{e.message}")
Sentry.capture_exception(e)
@ -377,4 +379,26 @@ private
lettings_logs_to_reassign.update_all(assigned_to_id: unassigned_user.id, values_updated_at: Time.zone.now)
sales_logs_to_reassign.update_all(assigned_to_id: unassigned_user.id, values_updated_at: Time.zone.now)
end
def send_organisation_change_email(current_organisation, new_organisation, log_reassignment, logs_count)
reassigned_logs_text = ""
case log_reassignment
when "reassign_all"
reassigned_logs_text = "There are #{logs_count} logs assigned to you. The stock owner and managing agent on these logs has been changed from #{current_organisation.name} to #{new_organisation.name}."
when "reassign_stock_owner"
reassigned_logs_text = "There are #{logs_count} logs assigned to you. The stock owner on these logs has been changed from #{current_organisation.name} to #{new_organisation.name}."
when "reassign_managing_agent"
reassigned_logs_text = "There are #{logs_count} logs assigned to you. The managing agent on these logs has been changed from #{current_organisation.name} to #{new_organisation.name}."
when "unassign"
reassigned_logs_text = "There are #{logs_count} logs assigned to you. These have now been unassigned."
end
template_id = ORGANISATION_UPDATE_TEMPLATE_ID
personalisation = {
from_organisation: "#{current_organisation.name} (Organisation ID: #{current_organisation.id})",
to_organisation: "#{new_organisation.name} (Organisation ID: #{new_organisation.id})",
reassigned_logs_text:,
}
DeviseNotifyMailer.new.send_email(email, template_id, personalisation)
end
end

63
spec/models/user_spec.rb

@ -534,6 +534,14 @@ RSpec.describe User, type: :model do
let(:new_organisation) { create(:organisation) }
let!(:lettings_log) { create(:lettings_log, assigned_to: user) }
let!(:sales_log) { create(:sales_log, assigned_to: user) }
let(:notify_client) { instance_double(Notifications::Client) }
let(:devise_notify_mailer) { DeviseNotifyMailer.new }
before do
allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer)
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true)
end
context "when reassigning all orgs for logs" do
it "reassigns all logs to the new organisation" do
@ -552,6 +560,17 @@ RSpec.describe User, type: :model do
expect(user.organisation).to eq(new_organisation)
end
it "sends organisation update emails" do
expected_personalisation = {
from_organisation: "#{user.organisation.name} (Organisation ID: #{user.organisation_id})",
to_organisation: "#{new_organisation.name} (Organisation ID: #{new_organisation.id})",
reassigned_logs_text: "There are 2 logs assigned to you. The stock owner and managing agent on these logs has been changed from #{user.organisation.name} to #{new_organisation.name}.",
}
user.reassign_logs_and_update_organisation(new_organisation, "reassign_all")
expect(notify_client).to have_received(:send_email).with(email_address: user.email, template_id: User::ORGANISATION_UPDATE_TEMPLATE_ID, personalisation: expected_personalisation).once
end
context "and there is an error" do
before do
allow(user).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
@ -587,6 +606,17 @@ RSpec.describe User, type: :model do
expect(user.organisation).to eq(new_organisation)
end
it "sends organisation update emails" do
expected_personalisation = {
from_organisation: "#{user.organisation.name} (Organisation ID: #{user.organisation_id})",
to_organisation: "#{new_organisation.name} (Organisation ID: #{new_organisation.id})",
reassigned_logs_text: "There are 2 logs assigned to you. The stock owner on these logs has been changed from #{user.organisation.name} to #{new_organisation.name}.",
}
user.reassign_logs_and_update_organisation(new_organisation, "reassign_stock_owner")
expect(notify_client).to have_received(:send_email).with(email_address: user.email, template_id: User::ORGANISATION_UPDATE_TEMPLATE_ID, personalisation: expected_personalisation).once
end
context "and there is an error" do
before do
allow(user).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
@ -622,6 +652,17 @@ RSpec.describe User, type: :model do
expect(user.organisation).to eq(new_organisation)
end
it "sends organisation update emails" do
expected_personalisation = {
from_organisation: "#{user.organisation.name} (Organisation ID: #{user.organisation_id})",
to_organisation: "#{new_organisation.name} (Organisation ID: #{new_organisation.id})",
reassigned_logs_text: "There are 2 logs assigned to you. The managing agent on these logs has been changed from #{user.organisation.name} to #{new_organisation.name}.",
}
user.reassign_logs_and_update_organisation(new_organisation, "reassign_managing_agent")
expect(notify_client).to have_received(:send_email).with(email_address: user.email, template_id: User::ORGANISATION_UPDATE_TEMPLATE_ID, personalisation: expected_personalisation).once
end
context "and there is an error" do
before do
allow(user).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
@ -659,6 +700,17 @@ RSpec.describe User, type: :model do
expect(user.organisation).to eq(new_organisation)
end
it "sends organisation update emails" do
expected_personalisation = {
from_organisation: "#{user.organisation.name} (Organisation ID: #{user.organisation_id})",
to_organisation: "#{new_organisation.name} (Organisation ID: #{new_organisation.id})",
reassigned_logs_text: "There are 2 logs assigned to you. These have now been unassigned.",
}
user.reassign_logs_and_update_organisation(new_organisation, "unassign")
expect(notify_client).to have_received(:send_email).with(email_address: user.email, template_id: User::ORGANISATION_UPDATE_TEMPLATE_ID, personalisation: expected_personalisation).once
end
context "and there is an error" do
before do
allow(user).to receive(:update!).and_raise(ActiveRecord::RecordInvalid)
@ -728,6 +780,17 @@ RSpec.describe User, type: :model do
expect(user_without_logs.organisation).not_to eq(new_organisation)
end
end
it "sends organisation update emails" do
expected_personalisation = {
from_organisation: "#{user_without_logs.organisation.name} (Organisation ID: #{user_without_logs.organisation_id})",
to_organisation: "#{new_organisation.name} (Organisation ID: #{new_organisation.id})",
reassigned_logs_text: "",
}
user_without_logs.reassign_logs_and_update_organisation(new_organisation, nil)
expect(notify_client).to have_received(:send_email).with(email_address: user_without_logs.email, template_id: User::ORGANISATION_UPDATE_TEMPLATE_ID, personalisation: expected_personalisation).once
end
end
context "and user has logs" do

Loading…
Cancel
Save