Class: Decidim::Admin::NewsletterJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/decidim/admin/newsletter_job.rb

Overview

Custom ApplicationJob scoped to the admin panel.

Instance Method Summary collapse

Instance Method Details

#perform(newsletter, form, recipients_ids) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/jobs/decidim/admin/newsletter_job.rb', line 10

def perform(newsletter, form, recipients_ids)
  @newsletter = newsletter
  @form = form
  @recipients_ids = recipients_ids

  @newsletter.with_lock do
    raise "Newsletter already sent" if @newsletter.sent?

    @newsletter.update!(
      sent_at: Time.current,
      extended_data: extended_data,
      total_recipients: recipients.count,
      total_deliveries: 0
    )
  end

  recipients.find_each do |user|
    NewsletterDeliveryJob.perform_later(user, @newsletter)
  end
end