chore(email): add an observer which log the emails dispatched
This commit is contained in:
parent
07167cead9
commit
6cb2389eef
5 changed files with 60 additions and 0 deletions
|
@ -15,4 +15,31 @@ class EmailEvent < ApplicationRecord
|
|||
enum status: {
|
||||
dispatched: 'dispatched'
|
||||
}
|
||||
class << self
|
||||
def create_from_message!(message, status:)
|
||||
message.to.each do |recipient|
|
||||
EmailEvent.create!(
|
||||
to: pseudonymize_email(recipient),
|
||||
subject: message.subject,
|
||||
processed_at: message.date,
|
||||
method: ActionMailer::Base.delivery_methods.key(message.delivery_method.class),
|
||||
status:
|
||||
)
|
||||
rescue StandardError => error
|
||||
Sentry.capture_exception(error, extra: { subject: message.subject, status: })
|
||||
end
|
||||
end
|
||||
|
||||
def pseudonymize_email(email)
|
||||
username, domain_name = email.split("@")
|
||||
|
||||
username_masked = if username.length > 3
|
||||
username[0..1] + "*" * (username.length - 3) + username[-1]
|
||||
else
|
||||
"*" * username.length
|
||||
end
|
||||
|
||||
"#{username_masked}@#{domain_name}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
7
app/services/email_delivery_observer.rb
Normal file
7
app/services/email_delivery_observer.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class EmailDeliveryObserver
|
||||
def self.delivered_email(message)
|
||||
return if message.to.nil?
|
||||
|
||||
EmailEvent.create_from_message!(message, status: "dispatched")
|
||||
end
|
||||
end
|
3
config/initializers/mail_observers.rb
Normal file
3
config/initializers/mail_observers.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
Rails.application.configure do
|
||||
config.action_mailer.observers = ['EmailDeliveryObserver']
|
||||
end
|
|
@ -25,4 +25,20 @@ RSpec.describe ApplicationMailer, type: :mailer do
|
|||
it { expect(subject.message).not_to be_an_instance_of(ActionMailer::Base::NullMail) }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'EmailDeliveryObserver is invoked' do
|
||||
let(:user1) { create(:user) }
|
||||
let(:user2) { create(:user, email: "thisisyour@email.com") }
|
||||
|
||||
it 'creates a new EmailEvent record with the correct information' do
|
||||
expect { UserMailer.ask_for_merge(user1, user2.email).deliver_now }.to change { EmailEvent.count }.by(1)
|
||||
event = EmailEvent.last
|
||||
|
||||
expect(event.to).to eq("th*******r@email.com")
|
||||
expect(event.method).to eq("test")
|
||||
expect(event.subject).to eq('Fusion de compte')
|
||||
expect(event.processed_at).to be_within(1.second).of(Time.zone.now)
|
||||
expect(event.status).to eq('dispatched')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
7
spec/models/email_event_spec.rb
Normal file
7
spec/models/email_event_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
RSpec.describe EmailEvent, type: :model do
|
||||
describe "#pseudonymize_email" do
|
||||
it { expect(EmailEvent.pseudonymize_email("example@rspec.com")).to eq("ex****e@rspec.com") }
|
||||
it { expect(EmailEvent.pseudonymize_email("exa@rspec.com")).to eq("***@rspec.com") }
|
||||
it { expect(EmailEvent.pseudonymize_email("e@rspec.com")).to eq("*@rspec.com") }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue