add email events

This commit is contained in:
Pete Matsyburka
2024-06-04 11:48:02 +03:00
parent db302858c4
commit cdda55d528
9 changed files with 151 additions and 1 deletions
+21
View File
@@ -6,11 +6,32 @@ class ApplicationMailer < ActionMailer::Base
register_interceptor ActionMailerConfigsInterceptor
register_observer ActionMailerEventsObserver
before_action do
ActiveStorage::Current.url_options = Docuseal.default_url_options
end
after_action :set_message_metadata
after_action :set_message_uuid
def default_url_options
Docuseal.default_url_options
end
def set_message_metadata
message.instance_variable_set(:@message_metadata, @message_metadata)
end
def set_message_uuid
message['X-Message-Uuid'] = SecureRandom.uuid
end
def assign_message_metadata(tag, record)
@message_metadata = {
'tag' => tag,
'record_id' => record.id,
'record_type' => record.class.name
}
end
end
+6
View File
@@ -26,6 +26,8 @@ class SubmitterMailer < ApplicationMailer
DEFAULT_INVITATION_SUBJECT
end
assign_message_metadata('submitter_invitation', @submitter)
mail(
to: @submitter.friendly_name,
from: from_address_for_submitter(submitter),
@@ -58,6 +60,8 @@ class SubmitterMailer < ApplicationMailer
%(#{submitter.submission.template.name} has been completed by #{submitters})
end
assign_message_metadata('submitter_completed', @submitter)
mail(from: from_address_for_submitter(submitter),
to: to || (user.role == 'integration' ? user.friendly_name.sub(/\+\w+@/, '@') : user.friendly_name),
subject:)
@@ -89,6 +93,8 @@ class SubmitterMailer < ApplicationMailer
'Your document copy'
end
assign_message_metadata('submitter_documents_copy', @submitter)
mail(from: from_address_for_submitter(submitter),
to: to || @submitter.friendly_name,
reply_to: @submitter.preferences['reply_to'].presence ||
+2
View File
@@ -6,6 +6,8 @@ class UserMailer < ApplicationMailer
@user = user
@token = @user.send(:set_reset_password_token)
assign_message_metadata('user_invitation', @user)
mail(to: @user.friendly_name,
subject: 'You are invited to DocuSeal')
end
+1
View File
@@ -31,6 +31,7 @@ class Account < ApplicationRecord
has_many :submissions, dependent: :destroy
has_many :submitters, through: :submissions
has_many :account_linked_accounts, dependent: :destroy
has_many :email_events, dependent: :destroy
has_many :account_testing_accounts, -> { testing }, dependent: :destroy,
class_name: 'AccountLinkedAccount',
inverse_of: :account
+42
View File
@@ -0,0 +1,42 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: email_events
#
# id :bigint not null, primary key
# data :text not null
# email :string not null
# emailable_type :string not null
# event_datetime :datetime not null
# event_type :string not null
# tag :string not null
# created_at :datetime not null
# account_id :bigint not null
# emailable_id :bigint not null
# message_id :string not null
#
# Indexes
#
# index_email_events_on_account_id (account_id)
# index_email_events_on_emailable (emailable_type,emailable_id)
# index_email_events_on_message_id (message_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
#
class EmailEvent < ApplicationRecord
belongs_to :emailable, polymorphic: true
belongs_to :account
attribute :data, :string, default: -> { {} }
serialize :data, coder: JSON
before_validation :maybe_set_account, on: :create
def maybe_set_account
self.account ||= emailable.account
end
end