Sha256: 4b661451de7b6d48e8299058040cad6d2bbb9c80c79748fb1911b346e774b842

Contents?: true

Size: 1.39 KB

Versions: 20

Compression:

Stored size: 1.39 KB

Contents

##
## Sends a campaign.
## _vp_ 2023-02-02
##
class Ish::EmailCampaign
  include Mongoid::Document
  include Mongoid::Timestamps
  store_in collection: 'ish_email_campaigns'

  field :slug
  validates_uniqueness_of :slug, allow_nil: true

  PAGE_PARAM_NAME = 'email_contexts_page'

  field :from_email
  validates_presence_of :from_email
  FROM_EMAILS = %w|    hello@infiniteshelter.com no-reply@infiniteshelter.com
    piousbox@gmail.com hello@piousbox.com        no-reply@piousbox.com        victor@piousbox.com
    admin@wasya.co     hello@wasya.co            no-reply@wasya.co            victor@wasya.co |
  def self.from_email_list
    [ [nil, nil] ] + FROM_EMAILS.map { |i| [i, i] }
  end

  belongs_to :email_template
  def tmpl; email_template; end

  field :subject
  field :body

  field :sent_at, type: DateTime
  field :send_at, type: DateTime

  def campaign_leads
    return ::EmailCampaignLead.where( email_campaign_id: self.id.to_s ).includes( :lead )
  end

  def leads
    ::Lead.joins( :email_campaign_leads ).where( 'email_campaign_leads.email_campaign_id' => self.id.to_s )
  end

  ##
  ## For tracking
  ##
  attr_reader :tid

  def do_send
    leads.each do |lead|
      ctx = Ctx.create!({
        email_template: tmpl,
        from_email:     tmpl.from_email,
        lead_id:        lead.id,
        send_at:        Time.now,
        subject:        tmpl.subject,
      })
    end
  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ish_models-0.0.33.263 lib/ish/email_campaign.rb
ish_models-0.0.33.262 lib/ish/email_campaign.rb
ish_models-0.0.33.261 lib/ish/email_campaign.rb
ish_models-0.0.33.260 lib/ish/email_campaign.rb
ish_models-0.0.33.259 lib/ish/email_campaign.rb
ish_models-0.0.33.258 lib/ish/email_campaign.rb
ish_models-0.0.33.257 lib/ish/email_campaign.rb
ish_models-0.0.33.256 lib/ish/email_campaign.rb
ish_models-0.0.33.255 lib/ish/email_campaign.rb
ish_models-0.0.33.254 lib/ish/email_campaign.rb
ish_models-0.0.33.253 lib/ish/email_campaign.rb
ish_models-0.0.33.252 lib/ish/email_campaign.rb
ish_models-0.0.33.251 lib/ish/email_campaign.rb
ish_models-0.0.33.250 lib/ish/email_campaign.rb
ish_models-0.0.33.249 lib/ish/email_campaign.rb
ish_models-0.0.33.248 lib/ish/email_campaign.rb
ish_models-0.0.33.247 lib/ish/email_campaign.rb
ish_models-0.0.33.246 lib/ish/email_campaign.rb
ish_models-0.0.33.245 lib/ish/email_campaign.rb
ish_models-0.0.33.244 lib/ish/email_campaign.rb