Sha256: 486a6cf2fbec0f92fb5df4ca0a1743f6b8a2083d9c0e9bee272195e51b81181d

Contents?: true

Size: 795 Bytes

Versions: 8

Compression:

Stored size: 795 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # Custom ApplicationJob scoped to the admin panel.
    #
    class NewsletterJob < ApplicationJob
      queue_as :newsletter

      def perform(newsletter)
        newsletter.with_lock do
          raise "Newsletter already sent" if newsletter.sent?

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

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

      private

      def recipients
        @recipients ||= User.where(newsletter_notifications: true).where.not(email: nil, confirmed_at: nil)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
decidim-admin-0.3.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-0.3.2 decidim-admin/app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.3.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-0.3.1 decidim-admin/app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.3.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-0.3.0 decidim-admin/app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.2.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-0.2.0 decidim-admin/app/jobs/decidim/admin/newsletter_job.rb