From 05042ed54a4d24d38ceb4b395095c8a9d49b15ba Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Fri, 13 Feb 2026 09:22:51 +0100 Subject: [PATCH] Fix crash when a notification is deleted while a bundle is being sent --- app/mailers/notification/bundle_mailer.rb | 10 +++++++--- test/mailers/notification/bundle_mailer_test.rb | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/mailers/notification/bundle_mailer.rb b/app/mailers/notification/bundle_mailer.rb index 89fea208a..d0998fa23 100644 --- a/app/mailers/notification/bundle_mailer.rb +++ b/app/mailers/notification/bundle_mailer.rb @@ -7,10 +7,14 @@ class Notification::BundleMailer < ApplicationMailer @user = bundle.user @bundle = bundle @notifications = bundle.notifications + .preload(:card, :creator, source: [ :board, :creator ]) + .reject { |n| n.source.nil? || n.card.nil? } @unsubscribe_token = @user.generate_token_for(:unsubscribe) - mail \ - to: bundle.user.identity.email_address, - subject: "Fizzy#{ " (#{ Current.account.name })" if @user.identity.accounts.many? }: New notifications" + if @notifications.any? + mail \ + to: bundle.user.identity.email_address, + subject: "Fizzy#{ " (#{ Current.account.name })" if @user.identity.accounts.many? }: New notifications" + end end end diff --git a/test/mailers/notification/bundle_mailer_test.rb b/test/mailers/notification/bundle_mailer_test.rb index 6d7fcb751..8aca366c9 100644 --- a/test/mailers/notification/bundle_mailer_test.rb +++ b/test/mailers/notification/bundle_mailer_test.rb @@ -97,6 +97,15 @@ class Notification::BundleMailerTest < ActionMailer::TestCase assert_equal "#1 Fix the bug in production", title_link.inner_html 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 def create_notification(user, source: events(:logo_published)) Notification.create!(user: user, creator: user, source: source, created_at: 30.minutes.ago)