forked from docusealco/docuseal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateEmailEvents < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :email_events do |t| | ||
t.references :account, null: false, foreign_key: true, index: true | ||
t.references :emailable, polymorphic: true, index: true, null: false | ||
t.string :message_id, null: false, index: true | ||
t.string :tag, null: false | ||
t.string :event_type, null: false | ||
t.string :email, null: false | ||
t.text :data, null: false | ||
t.datetime :event_datetime, null: false | ||
t.datetime :created_at, null: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActionMailerEventsObserver | ||
module_function | ||
|
||
def delivered_email(mail) | ||
data = mail.instance_variable_get(:@message_metadata) | ||
|
||
return if data.blank? | ||
|
||
tag, emailable_id, emailable_type = data.values_at('tag', 'record_id', 'record_type') | ||
|
||
return if tag.blank? || emailable_type.blank? || emailable_id.blank? | ||
|
||
message_id = fetch_message_id(mail) | ||
|
||
all_emails(mail).each do |email| | ||
EmailEvent.create!( | ||
tag:, | ||
message_id:, | ||
emailable_id:, | ||
emailable_type:, | ||
event_type: :send, | ||
email:, | ||
data: { method: mail.delivery_method.class.name.underscore }, | ||
event_datetime: Time.current | ||
) | ||
end | ||
rescue StandardError => e | ||
Rollbar.error(e) if defined?(Rollbar) | ||
|
||
raise if Rails.env.local? | ||
end | ||
|
||
def fetch_message_id(mail) | ||
mail['X-Message-Uuid']&.value || SecureRandom.uuid | ||
end | ||
|
||
def all_emails(mail) | ||
mail.to.to_a + mail.cc.to_a + mail.bcc.to_a | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters