From 4db61d61d58457498e9f00e44b22addd8870862c Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Wed, 24 Jul 2024 15:18:07 +0530 Subject: [PATCH] feat: add assignee check before composing conversation --- app/listeners/notification_listener.rb | 3 ++ spec/listeners/notification_listener_spec.rb | 49 ++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/app/listeners/notification_listener.rb b/app/listeners/notification_listener.rb index 666bc4d13..d87bdb953 100644 --- a/app/listeners/notification_listener.rb +++ b/app/listeners/notification_listener.rb @@ -31,8 +31,11 @@ class NotificationListener < BaseListener conversation, account = extract_conversation_and_account(event) assignee = conversation.assignee return if event.data[:notifiable_assignee_change].blank? + return if conversation.pending? + return if assignee.blank? + NotificationBuilder.new( notification_type: 'conversation_assignment', user: assignee, diff --git a/spec/listeners/notification_listener_spec.rb b/spec/listeners/notification_listener_spec.rb index 1505b0f57..4f2746fe7 100644 --- a/spec/listeners/notification_listener_spec.rb +++ b/spec/listeners/notification_listener_spec.rb @@ -155,4 +155,53 @@ describe NotificationListener do end end end + + describe 'assignee_changed' do + let(:event_name) { :'conversation.assignee_changed' } + + context 'when assignee is changed' do + it 'creates notifications for inbox members who have notifications turned on' do + notification_setting = first_agent.notification_settings.first + notification_setting.selected_email_flags = [:email_conversation_assignment] + notification_setting.selected_push_flags = [] + notification_setting.save! + + create(:inbox_member, user: first_agent, inbox: inbox) + conversation.reload + + event = Events::Base.new(event_name, Time.zone.now, conversation: conversation, notifiable_assignee_change: true) + + listener.assignee_changed(event) + expect(user.notifications.count).to eq(1) + end + + it 'does not create notification for inbox members who have notifications turned off' do + notification_setting = agent_with_out_notification.notification_settings.first + notification_setting.unselect_all_email_flags + notification_setting.unselect_all_push_flags + notification_setting.save! + + create(:inbox_member, user: agent_with_out_notification, inbox: inbox) + conversation.reload + + event = Events::Base.new(event_name, Time.zone.now, conversation: conversation) + + listener.assignee_changed(event) + expect(notification_setting.user.notifications.count).to eq(0) + end + + it 'does not create notification when assignee is nil' do + create(:inbox_member, user: first_agent, inbox: inbox) + conversation.reload + + conversation.assignee = nil + conversation.save! + + event = Events::Base.new(event_name, Time.zone.now, conversation: conversation, notifiable_assignee_change: true, assignee: nil) + + listener.assignee_changed(event) + expect(user.notifications.count).to eq(0) + end + end + end end