Fix crash when a notification is deleted while a bundle is being sent

This commit is contained in:
Stanko K.R.
2026-02-13 09:22:51 +01:00
parent ae3d277868
commit 05042ed54a
2 changed files with 16 additions and 3 deletions
@@ -7,10 +7,14 @@ class Notification::BundleMailer < ApplicationMailer
@user = bundle.user @user = bundle.user
@bundle = bundle @bundle = bundle
@notifications = bundle.notifications @notifications = bundle.notifications
.preload(:card, :creator, source: [ :board, :creator ])
.reject { |n| n.source.nil? || n.card.nil? }
@unsubscribe_token = @user.generate_token_for(:unsubscribe) @unsubscribe_token = @user.generate_token_for(:unsubscribe)
if @notifications.any?
mail \ mail \
to: bundle.user.identity.email_address, to: bundle.user.identity.email_address,
subject: "Fizzy#{ " (#{ Current.account.name })" if @user.identity.accounts.many? }: New notifications" subject: "Fizzy#{ " (#{ Current.account.name })" if @user.identity.accounts.many? }: New notifications"
end end
end
end end
@@ -97,6 +97,15 @@ class Notification::BundleMailerTest < ActionMailer::TestCase
assert_equal "#1 Fix the <code>bug</code> in production", title_link.inner_html assert_equal "#1 Fix the <code>bug</code> in production", title_link.inner_html
end end
test "skips notifications whose source event was deleted" do
notification = create_notification(@user)
notification.source.destroy
email = Notification::BundleMailer.notification(@bundle)
assert_not email.respond_to?(:deliver) && email.message.is_a?(Mail::Message),
"Should not generate a real email when all notifications are stale"
end
private private
def create_notification(user, source: events(:logo_published)) def create_notification(user, source: events(:logo_published))
Notification.create!(user: user, creator: user, source: source, created_at: 30.minutes.ago) Notification.create!(user: user, creator: user, source: source, created_at: 30.minutes.ago)