chore(email): add an observer which log the emails dispatched

This commit is contained in:
Colin Darie 2023-01-09 17:05:38 +01:00
parent 07167cead9
commit 6cb2389eef
5 changed files with 60 additions and 0 deletions

View file

@ -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