Sha256: 682f2d43b5676ec386bd088c85d1bd31116f0c6212c1a142509d3d837e043ce6

Contents?: true

Size: 934 Bytes

Versions: 15

Compression:

Stored size: 934 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(organization: @newsletter.organization)
                            .where.not(newsletter_notifications_at: nil, email: nil, confirmed_at: nil)
                            .not_deleted
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
decidim-admin-0.17.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.17.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.16.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.17.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.16.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.15.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.15.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.15.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.14.4 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.14.3 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.14.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.14.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.13.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.13.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.13.0.pre1 app/jobs/decidim/admin/newsletter_job.rb