Skip to content

Commit

Permalink
fix: Send push notification on bot_handoff (#7636)
Browse files Browse the repository at this point in the history
Send a 'Conversation Created' push notification when the bot does a hand_off.
fixes: #7587

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
  • Loading branch information
Jatin-MYST and muhsin-k committed Feb 7, 2024
1 parent 1b21e0d commit 98eddd0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/listeners/notification_listener.rb
@@ -1,4 +1,18 @@
class NotificationListener < BaseListener
def conversation_bot_handoff(event)
conversation, account = extract_conversation_and_account(event)
return if conversation.pending?

conversation.inbox.members.each do |agent|
NotificationBuilder.new(
notification_type: 'conversation_creation',
user: agent,
account: account,
primary_actor: conversation
).perform
end
end

def conversation_created(event)
conversation, account = extract_conversation_and_account(event)
return if conversation.pending?
Expand Down
36 changes: 36 additions & 0 deletions spec/listeners/notification_listener_spec.rb
Expand Up @@ -119,4 +119,40 @@
end
end
end

describe 'conversation_bot_handoff' do
let(:event_name) { :'conversation.bot_handoff' }

context 'when conversation is bot handoff' 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_creation]
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)

listener.conversation_bot_handoff(event)
expect(notification_setting.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.conversation_bot_handoff(event)
expect(notification_setting.user.notifications.count).to eq(0)
end
end
end
end

0 comments on commit 98eddd0

Please sign in to comment.