chore: Refactor the notification service for participants (#6461)

This commit is contained in:
Pranav Raj S
2023-02-15 14:14:56 -08:00
committed by GitHub
parent d6baa5db85
commit 949ddf68ba
8 changed files with 215 additions and 45 deletions

View File

@@ -0,0 +1,65 @@
require 'rails_helper'
describe Messages::MentionService do
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
let!(:first_agent) { create(:user, account: account) }
let!(:second_agent) { create(:user, account: account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user) }
let(:builder) { double }
before do
create(:inbox_member, user: first_agent, inbox: inbox)
create(:inbox_member, user: second_agent, inbox: inbox)
conversation.reload
allow(NotificationBuilder).to receive(:new).and_return(builder)
allow(builder).to receive(:perform)
end
context 'when message contains mention' do
it 'creates notifications for inbox member who was mentioned' do
message = build(
:message,
conversation: conversation,
account: account,
content: "hi [#{first_agent.name}](mention://user/#{first_agent.id}/#{first_agent.name})",
private: true
)
described_class.new(message: message).perform
expect(NotificationBuilder).to have_received(:new).with(notification_type: 'conversation_mention',
user: first_agent,
account: account,
primary_actor: message)
end
end
context 'when message contains multiple mentions' do
let(:message) do
build(
:message,
conversation: conversation,
account: account,
content: "hey [#{second_agent.name}](mention://user/#{second_agent.id}/#{second_agent.name})/
[#{first_agent.name}](mention://user/#{first_agent.id}/#{first_agent.name}),
please look in to this?",
private: true
)
end
it 'creates notifications for inbox member who was mentioned' do
described_class.new(message: message).perform
expect(NotificationBuilder).to have_received(:new).with(notification_type: 'conversation_mention',
user: second_agent,
account: account,
primary_actor: message)
expect(NotificationBuilder).to have_received(:new).with(notification_type: 'conversation_mention',
user: first_agent,
account: account,
primary_actor: message)
end
end
end

View File

@@ -0,0 +1,72 @@
require 'rails_helper'
describe Messages::NewMessageNotificationService do
context 'when message is not notifiable' do
it 'will not create any notifications' do
message = build(:message, message_type: :activity)
expect(NotificationBuilder).not_to receive(:new)
described_class.new(message: message).perform
end
end
context 'when message is notifiable' do
let(:account) { create(:account) }
let(:assignee) { create(:user, account: account) }
let(:participating_agent_1) { create(:user, account: account) }
let(:inbox) { create(:inbox, account: account) }
let(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: assignee) }
let(:builder) { double }
before do
create(:inbox_member, inbox: inbox, user: participating_agent_1)
create(:inbox_member, inbox: inbox, user: assignee)
allow(NotificationBuilder).to receive(:new).and_return(builder)
allow(builder).to receive(:perform)
end
context 'when message is created by a participant' do
let(:message) { create(:message, conversation: conversation, account: account, sender: participating_agent_1) }
before do
described_class.new(message: message).perform
end
it 'creates notifications for assignee' do
expect(NotificationBuilder).to have_received(:new).with(notification_type: 'assigned_conversation_new_message',
user: assignee,
account: account,
primary_actor: message)
end
end
context 'when message is created by a contact' do
let(:message) { create(:message, conversation: conversation, account: account) }
before do
described_class.new(message: message).perform
end
it 'creates notifications for assignee' do
expect(NotificationBuilder).to have_received(:new).with(notification_type: 'assigned_conversation_new_message',
user: assignee,
account: account,
primary_actor: message)
end
end
context 'when message is created by assignee' do
let(:message) { create(:message, conversation: conversation, account: account, sender: assignee) }
before do
described_class.new(message: message).perform
end
it 'will not create notifications for the user who created the message' do
expect(NotificationBuilder).not_to have_received(:new).with(notification_type: 'assigned_conversation_new_message',
user: assignee,
account: account,
primary_actor: message)
end
end
end
end