Skip to content

Commit

Permalink
Fix newsletter delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
ahukkanen committed Dec 9, 2024
1 parent 18bea7c commit 3c33492
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions app/models/concerns/decidim/privacy/newsletter_extensions.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/decidim/privacy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class Engine < ::Rails::Engine
Decidim::ParticipatorySpacePrivateUser.include(Decidim::Privacy::ParticipatorySpacePrivateUserExtensions)
Decidim::EditorImage.include(Decidim::Privacy::EditorImageExtensions)
Decidim::Proposals::ProposalVote.include(Decidim::Privacy::ProposalVoteExtensions)
Decidim::Newsletter.include(Decidim::Privacy::NewsletterExtensions)

# forms
Decidim::AccountForm.include(Decidim::Privacy::AccountFormExtensions)
Expand Down Expand Up @@ -215,6 +216,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
Expand Down
67 changes: 67 additions & 0 deletions spec/jobs/decidim/admin/newsletter_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require "spec_helper"

describe Decidim::Admin::NewsletterJob do
let!(:newsletter) { create(:newsletter, organization: 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: 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: organization) }
let!(:non_deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: nil, organization: organization) }
let!(:deleted_user) { create(:user, :confirmed, :deleted, newsletter_notifications_at: Time.current, organization: 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_all_users,
send_to_followers: send_to_followers,
send_to_participants: send_to_participants,
participatory_space_types: participatory_space_types,
scope_ids: 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

0 comments on commit 3c33492

Please sign in to comment.