Sha256: 22f906cab1a6d3c2ccf87c37c1c1ff0f8f488aac05eae8919a7ec9da88cd91c1

Contents?: true

Size: 932 Bytes

Versions: 9

Compression:

Stored size: 932 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 = newsletter

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

          @newsletter.update!(
            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, organization: @newsletter.organization)
                            .where.not(email: nil, confirmed_at: nil)
                            .not_deleted
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-admin-0.12.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.12.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.12.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.11.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.12.0.pre app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.11.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.11.0.pre1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.10.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.10.0 app/jobs/decidim/admin/newsletter_job.rb