Class: CampaignDelivery

Inherits:
ApplicationRecord show all
Defined in:
app/models/campaign_delivery.rb

Overview

== Schema Information

Table name: campaign_deliveries
Database name: primary

id :integer not null, primary key
state :string
created_at :datetime not null
updated_at :datetime not null
campaign_email_id :integer
communication_recipient_id :integer
audience_member_id :integer

Indexes

index_campaign_deliveries_on_communication_recipient_id (communication_recipient_id) WHERE (communication_recipient_id IS NOT NULL) USING hash
index_campaign_deliveries_on_state (state)
index_campaign_deliveries_on_audience_member_id (audience_member_id)
unique_audience_member (campaign_email_id,audience_member_id) UNIQUE

Foreign Keys

fk_rails_... (campaign_email_id => campaign_actions.id)
fk_rails_... (communication_recipient_id => communication_recipients.id) ON DELETE => cascade
fk_rails_... (audience_member_id => audience_members.id)

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Has many collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#resend_deliveryObject

Returns the value of attribute resend_delivery.



41
42
43
# File 'app/models/campaign_delivery.rb', line 41

def resend_delivery
  @resend_delivery
end

Instance Method Details

#audience_memberAudienceMember?

Returns:



33
# File 'app/models/campaign_delivery.rb', line 33

belongs_to :audience_member

#audience_member_inactive?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'app/models/campaign_delivery.rb', line 140

def audience_member_inactive?
  Party.joins('left join parties customers on customers.id = parties.customer_id').where(id: ContactPoint.emails.where(detail: audience_member.email_address).select(:party_id)).where("parties.state in ('closed','bankrupt') or parties.inactive = true or customers.state in ('closed','bankrupt')").exists?
end

#audience_member_on_suppression_list?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/campaign_delivery.rb', line 130

def audience_member_on_suppression_list?
  EmailPreference.where(email: audience_member.email_address).where("disable_#{campaign_email.campaign.category} is true").exists?
end

#audience_member_previously_bounced?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/campaign_delivery.rb', line 135

def audience_member_previously_bounced?
  ContactPoint.exists?(category: 'email', detail: audience_member.email_address, state: 'failed')
end

#campaign_emailCampaignEmail?

Returns:



31
# File 'app/models/campaign_delivery.rb', line 31

belongs_to :campaign_email

#communication_recipientCommunicationRecipient?

Returns:



35
# File 'app/models/campaign_delivery.rb', line 35

belongs_to :communication_recipient, optional: true

#communicationsActiveRecord::Relation<Communication>

Returns:



37
# File 'app/models/campaign_delivery.rb', line 37

has_many :communications, as: :resource, dependent: :nullify

#generate_communicationvoid

This method returns an undefined value.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/campaign_delivery.rb', line 81

def generate_communication
  # Advisory lock guards against two CampaignDeliveryWorker jobs racing on
  # the same delivery — the in-memory `queued?` checks in the worker and
  # below don't survive concurrent loads, and `send_immediately: true`
  # transitions the Communication straight to `sent`, so a stale-state
  # second pass would happily build a duplicate. Non-blocking: if another
  # worker holds the lock it's already handling this delivery, skip.
  self.class.with_advisory_lock_result(
    "campaign_delivery_#{id}_generate_communication", timeout_seconds: 0
  ) do
    next if communications.reload.exists?

    comm = CommunicationBuilder.new(
      sender_party: campaign_email.sender,
      sender: campaign_email.sender_email,
      resource: self,
      source_id: campaign_email.source_id,
      email_template_id: campaign_email.email_template_id,
      recipient_party: audience_member.party,
      emails: audience_member.email_address,
      category: campaign_email.campaign.category
    ).create(send_immediately: true, skip_suppression_check: true)
    update(communication_recipient_id: comm.communication_recipients.first&.id)
  end
end

#has_duplicate_audience_member?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
# File 'app/models/campaign_delivery.rb', line 145

def has_duplicate_audience_member?
  # find any existing deliveries for the same email address which have already gone past the pending stage
  CampaignDelivery.joins(:audience_member).exists?([
                                                'audience_members.email_address = ? and campaign_deliveries.campaign_email_id = ? and campaign_deliveries.state != ?', audience_member.email_address, campaign_email_id, 'pending'
                                              ])
end

#invalid_email?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
# File 'app/models/campaign_delivery.rb', line 108

def invalid_email?
  return true if audience_member.email_address.blank?

  !Truemail.valid?(audience_member.email_address)
end

#post_communication_exception_hookvoid

This method returns an undefined value.



125
126
127
# File 'app/models/campaign_delivery.rb', line 125

def post_communication_exception_hook
  exception
end

#post_communication_sent_hookvoid

This method returns an undefined value.



115
116
117
# File 'app/models/campaign_delivery.rb', line 115

def post_communication_sent_hook
  sent
end

#post_communication_suppressed_hookvoid

This method returns an undefined value.



120
121
122
# File 'app/models/campaign_delivery.rb', line 120

def post_communication_suppressed_hook
  suppressed
end

#to_sString

Returns:

  • (String)


153
154
155
# File 'app/models/campaign_delivery.rb', line 153

def to_s
  "Campaign: #{campaign_email&.campaign&.name || 'missing'}"
end