Sha256: fdbb3082366a4eb6dcd42d6a46ebe8db4e39fbf5ac214df1785c88b6de98d03f

Contents?: true

Size: 1.29 KB

Versions: 51

Compression:

Stored size: 1.29 KB

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, 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

      private

      def extended_data
        {
          send_to_all_users: @form["send_to_all_users"],
          send_to_followers: @form["send_to_followers"],
          send_to_participants: @form["send_to_participants"],
          participatory_space_types: @form["participatory_space_types"],
          scope_ids: @form["scope_ids"]
        }
      end

      def recipients
        @recipients ||= User.where(organization: @newsletter.organization)
                            .where(id: @recipients_ids)
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
decidim-admin-0.27.9 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.8 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.7 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.6 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.10 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.9 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.5 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.8 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.4 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.3 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.7 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.5 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.4 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.0 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.3 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.0.rc2 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.27.0.rc1 app/jobs/decidim/admin/newsletter_job.rb
decidim-admin-0.26.2 app/jobs/decidim/admin/newsletter_job.rb