diff --git a/app/listeners/notification_listener.rb b/app/listeners/notification_listener.rb index 666bc4d13..461f1c211 100644 --- a/app/listeners/notification_listener.rb +++ b/app/listeners/notification_listener.rb @@ -30,6 +30,12 @@ class NotificationListener < BaseListener def assignee_changed(event) conversation, account = extract_conversation_and_account(event) assignee = conversation.assignee + + # NOTE: The issue was that when a team change results in an assignee being set to nil, + # the system was still trying to create a notification about the assignment change, + # but there was no assignee to notify, causing potential issues in the notification system. + # We need to debug this properly, but for now no need to pollute the jobs + return if assignee.blank? return if event.data[:notifiable_assignee_change].blank? return if conversation.pending? diff --git a/spec/listeners/notification_listener_spec.rb b/spec/listeners/notification_listener_spec.rb index 2291c401a..76e458b8e 100644 --- a/spec/listeners/notification_listener_spec.rb +++ b/spec/listeners/notification_listener_spec.rb @@ -200,4 +200,28 @@ describe NotificationListener do end end end + + describe 'assignee_changed' do + let(:event_name) { :'conversation.assignee_changed' } + + context 'when notifiable_assignee_change is true but assignee is nil' do + it 'does not create a notification' do + conversation_with_nil_assignee = create(:conversation, account: account, inbox: inbox, assignee: nil) + + notification_builder_mock = instance_double(NotificationBuilder) + allow(NotificationBuilder).to receive(:new).and_return(notification_builder_mock) + + event = Events::Base.new( + event_name, + Time.zone.now, + conversation: conversation_with_nil_assignee, + data: { notifiable_assignee_change: true } + ) + + expect(notification_builder_mock).not_to receive(:perform) + + listener.assignee_changed(event) + end + end + end end