Class: CommunicationRecipient

Inherits:
ApplicationRecord show all
Includes:
ActionView::Helpers::NumberHelper, Models::Auditable
Defined in:
app/models/communication_recipient.rb

Overview

== Schema Information

Table name: communication_recipients
Database name: primary

id :integer not null, primary key
category :string(255)
detail :string(255)
email_method :string(3)
ip_address :string(255)
name :string(255)
state :string(25)
state_response :text
state_updated_at :datetime
user_agent :text
created_at :datetime not null
updated_at :datetime not null
communication_id :integer
contact_point_id :integer

Indexes

idx_comm_rp_contact_point_id (contact_point_id)
idx_communication_id_contact_point_id (communication_id,contact_point_id)
idx_communication_id_email_method (communication_id,email_method)
idx_communication_recipients_unique (communication_id,category,detail) UNIQUE
index_communication_recipients_on_state (state)
index_communication_recipients_on_state_updated_at (state_updated_at) USING brin

Foreign Keys

communication_recipients_communication_id_fk (communication_id => communications.id) ON DELETE => cascade

Constant Summary collapse

UNDELIVERED =
%w[dropped deferred bounced].freeze
DELIVERED =
%w[processed delivered opened clicked spammed unsubscribed].freeze
UNWANTED =
%w[spammed unsubscribed].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has one collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#categoryObject (readonly)



120
# File 'app/models/communication_recipient.rb', line 120

validates :category, presence: true, inclusion: { in: [ContactPoint::FAX, ContactPoint::EMAIL] }

#combo_categoryObject

Returns the value of attribute combo_category.



50
51
52
# File 'app/models/communication_recipient.rb', line 50

def combo_category
  @combo_category
end

#detailObject (readonly)



121
# File 'app/models/communication_recipient.rb', line 121

validates :detail, presence: true

#ignore_global_unsubscribeObject

Returns the value of attribute ignore_global_unsubscribe.



51
52
53
# File 'app/models/communication_recipient.rb', line 51

def ignore_global_unsubscribe
  @ignore_global_unsubscribe
end

Class Method Details

.emailsActiveRecord::Relation<CommunicationRecipient>

A relation of CommunicationRecipients that are emails. Active Record Scope

Returns:

See Also:



112
# File 'app/models/communication_recipient.rb', line 112

scope :emails, -> { where(category: ContactPoint::EMAIL) }

.faxesActiveRecord::Relation<CommunicationRecipient>

A relation of CommunicationRecipients that are faxes. Active Record Scope

Returns:

See Also:



113
# File 'app/models/communication_recipient.rb', line 113

scope :faxes, -> { where(category: ContactPoint::FAX) }

.states_for_selectObject



131
132
133
# File 'app/models/communication_recipient.rb', line 131

def self.states_for_select
  state_machines[:state].states.map(&:name)
end

.undeliveredActiveRecord::Relation<CommunicationRecipient>

A relation of CommunicationRecipients that are undelivered. Active Record Scope

Returns:

See Also:



114
# File 'app/models/communication_recipient.rb', line 114

scope :undelivered, -> { where(state: UNDELIVERED) }

.unwantedActiveRecord::Relation<CommunicationRecipient>

A relation of CommunicationRecipients that are unwanted. Active Record Scope

Returns:

See Also:



115
# File 'app/models/communication_recipient.rb', line 115

scope :unwanted, -> { where(state: UNWANTED) }

.with_partyActiveRecord::Relation<CommunicationRecipient>

A relation of CommunicationRecipients that are with party. Active Record Scope

Returns:

See Also:



111
# File 'app/models/communication_recipient.rb', line 111

scope :with_party, -> { joins(contact_point: :party) }

Instance Method Details

#campaign_deliveryCampaignDelivery



45
# File 'app/models/communication_recipient.rb', line 45

has_one :campaign_delivery

#communicationCommunication



43
# File 'app/models/communication_recipient.rb', line 43

belongs_to :communication, inverse_of: :communication_recipients, optional: true

Returns:

See Also:



47
# File 'app/models/communication_recipient.rb', line 47

has_many :communication_recipient_email_links

#contact_pointContactPoint



44
# File 'app/models/communication_recipient.rb', line 44

belongs_to :contact_point, inverse_of: :communication_recipients, optional: true

#customerObject



189
190
191
# File 'app/models/communication_recipient.rb', line 189

def customer
  party.try(:customer)
end

#deep_dupObject



125
126
127
128
129
# File 'app/models/communication_recipient.rb', line 125

def deep_dup
  deep_clone(except: %i[state_updated_at user_agent ip_address state_response]) do |_original, copy|
    copy.state = 'ok' if copy.is_a?(CommunicationRecipient)
  end
end

#display_formatObject



197
198
199
200
201
202
203
204
# File 'app/models/communication_recipient.rb', line 197

def display_format
  case category
  when ContactPoint::FAX
    PhoneNumber.parse_and_format(detail)
  else
    detail
  end
end

#display_nameObject



193
194
195
# File 'app/models/communication_recipient.rb', line 193

def display_name
  name || contact_point.try(:party).try(:full_name)
end

Returns:

See Also:



48
# File 'app/models/communication_recipient.rb', line 48

has_many :email_links, -> { distinct }, through: :communication_recipient_email_links

#formatted_email_stringObject



175
176
177
178
179
180
181
182
183
# File 'app/models/communication_recipient.rb', line 175

def formatted_email_string
  case category
  when ContactPoint::FAX
    n = PhoneNumber.parse_and_format(detail, display_format: :fax_dial)
    "#{n}@fax.tc"
  when ContactPoint::EMAIL
    detail
  end
end

#is_email?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/communication_recipient.rb', line 167

def is_email?
  category == ContactPoint::EMAIL
end

#is_fax?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'app/models/communication_recipient.rb', line 171

def is_fax?
  category == ContactPoint::FAX
end

#is_undelivered?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'app/models/communication_recipient.rb', line 159

def is_undelivered?
  UNDELIVERED.include? state
end

#is_unwanted?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'app/models/communication_recipient.rb', line 163

def is_unwanted?
  UNWANTED.include? state
end

#normalize_formatObject (protected)



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/communication_recipient.rb', line 208

def normalize_format
  return if detail.blank?

  # Remove invalid characters, stick to ascii
  self.detail = detail.gsub(/\P{ASCII}/u, '').strip.downcase
  if RFC822::EMAIL.match?(detail)
    self.category = ContactPoint::EMAIL
  elsif p = PhoneNumber.parse(detail)
    self.category = ContactPoint::FAX
    self.detail = p.to_s
  end
  true
end

#notify_rep_email_bouncedObject



135
136
137
138
139
# File 'app/models/communication_recipient.rb', line 135

def notify_rep_email_bounced
  return unless communication.important?

  InternalMailer.email_address_bounced_notification(self).deliver_later
end

#partyObject



185
186
187
# File 'app/models/communication_recipient.rb', line 185

def party
  contact_point.try(:party)
end

#webhook_eventsActiveRecord::Relation<WebhookEvent>

Returns:

See Also:



46
# File 'app/models/communication_recipient.rb', line 46

has_many :webhook_events