mirror of https://github.com/docusealco/docuseal
				
				
				
			
							parent
							
								
									db302858c4
								
							
						
					
					
						commit
						cdda55d528
					
				| @ -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 | ||||||
| @ -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 | ||||||
| @ -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 | ||||||
					Loading…
					
					
				
		Reference in new issue