diff --git a/app/jobs/concerns/decidim/privacy/admin_newsletter_job_extensions.rb b/app/jobs/concerns/decidim/privacy/admin_newsletter_job_extensions.rb new file mode 100644 index 0000000..fa465e6 --- /dev/null +++ b/app/jobs/concerns/decidim/privacy/admin_newsletter_job_extensions.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Decidim + module Privacy + module AdminNewsletterJobExtensions + extend ActiveSupport::Concern + + included do + private + + def recipients + @recipients ||= Decidim::User.entire_collection + .where(organization: @newsletter.organization) + .where(id: @recipients_ids) + end + end + end + end +end diff --git a/app/models/concerns/decidim/privacy/newsletter_extensions.rb b/app/models/concerns/decidim/privacy/newsletter_extensions.rb new file mode 100644 index 0000000..e03eadf --- /dev/null +++ b/app/models/concerns/decidim/privacy/newsletter_extensions.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Decidim + module Privacy + module NewsletterExtensions + extend ActiveSupport::Concern + + included do + belongs_to :author, -> { entire_collection }, foreign_key: :author_id, class_name: "Decidim::User" + end + end + end +end diff --git a/lib/decidim/privacy/engine.rb b/lib/decidim/privacy/engine.rb index 0fb8d54..4d20b8f 100644 --- a/lib/decidim/privacy/engine.rb +++ b/lib/decidim/privacy/engine.rb @@ -165,6 +165,7 @@ class Engine < ::Rails::Engine Decidim::EditorImage.include(Decidim::Privacy::EditorImageExtensions) Decidim::Proposals::ProposalVote.include(Decidim::Privacy::ProposalVoteExtensions) Decidim::InitiativesCommitteeMember.include(Decidim::Privacy::InitiativesCommitteeMemberExtensions) + Decidim::Newsletter.include(Decidim::Privacy::NewsletterExtensions) # forms Decidim::AccountForm.include(Decidim::Privacy::AccountFormExtensions) @@ -206,6 +207,11 @@ class Engine < ::Rails::Engine Decidim::Privacy::AdminNewsletterRecipientsExtensions ) + # Jobs + Decidim::Admin::NewsletterJob.include( + Decidim::Privacy::AdminNewsletterJobExtensions + ) + # Initialize concerns for each installed Decidim-module if Decidim.module_installed? :budgets # services diff --git a/spec/jobs/decidim/admin/newsletter_job_spec.rb b/spec/jobs/decidim/admin/newsletter_job_spec.rb new file mode 100644 index 0000000..ad01829 --- /dev/null +++ b/spec/jobs/decidim/admin/newsletter_job_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Decidim::Admin::NewsletterJob do + let!(:newsletter) { create(:newsletter, organization:, total_deliveries: 0) } + let!(:organization) { create(:organization) } + let!(:another_organization) { create(:organization) } + let!(:deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: Time.current, organization:) } + let!(:another_deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: Time.current, organization: another_organization) } + let!(:undeliverable_user) { create(:user, newsletter_notifications_at: Time.current, organization:) } + let!(:non_deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: nil, organization:) } + let!(:deleted_user) { create(:user, :confirmed, :deleted, newsletter_notifications_at: Time.current, organization:) } + let(:send_to_all_users) { true } + let(:send_to_followers) { false } + let(:send_to_participants) { false } + let(:participatory_space_types) { [] } + let(:scope_ids) { [] } + + let(:form_params) do + { + send_to_all_users:, + send_to_followers:, + send_to_participants:, + participatory_space_types:, + scope_ids: + } + end + + let(:form) do + Decidim::Admin::SelectiveNewsletterForm.from_params( + form_params + ).with_context( + current_organization: organization + ) + end + + let!(:recipients_ids) { [deliverable_user.id] } + + it "delivers a newsletter to a the eligible users" do + expect(Decidim::Admin::NewsletterDeliveryJob).to receive(:perform_later).with(deliverable_user, newsletter) + expect(Decidim::Admin::NewsletterDeliveryJob).not_to receive(:perform_later).with(undeliverable_user, newsletter) + + Decidim::Admin::NewsletterJob.perform_now(newsletter, form.as_json, recipients_ids) + end + + it "updates the recipients count" do + Decidim::Admin::NewsletterJob.perform_now(newsletter, form.as_json, recipients_ids) + expect(newsletter.reload.total_recipients).to eq(1) + end + + it "updates the deliveries count" do + Decidim::Admin::NewsletterJob.perform_now(newsletter, form.as_json, recipients_ids) + expect(newsletter.reload.total_deliveries).to eq(0) + end + + it "updates the extended data" do + Decidim::Admin::NewsletterJob.perform_now(newsletter, form.as_json, recipients_ids) + expect(newsletter.reload.extended_data).to eq( + "send_to_all_users" => true, + "send_to_followers" => false, + "send_to_participants" => false, + "participatory_space_types" => [], + "scope_ids" => [] + ) + end +end