chore: Add controllers for conversation participants (#6462)
Co-authored-by: Aswin Dev P.S <aswindevps@gmail.com> Co-authored-by: Sojan Jose <sojan@chatwoot.com>
This commit is contained in:
@@ -12,6 +12,10 @@ RSpec.describe 'Api::V1::Accounts::BulkActionsController', type: :request do
|
||||
create(:conversation, account_id: account.id, status: :open, team_id: team_1.id)
|
||||
create(:conversation, account_id: account.id, status: :open)
|
||||
create(:conversation, account_id: account.id, status: :open)
|
||||
Conversation.all.each do |conversation|
|
||||
create(:inbox_member, inbox: conversation.inbox, user: agent_1)
|
||||
create(:inbox_member, inbox: conversation.inbox, user: agent_2)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/bulk_action' do
|
||||
|
||||
@@ -56,8 +56,8 @@ RSpec.describe 'Conversation Assignment API', type: :request do
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
conversation.update!(assignee: agent)
|
||||
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
||||
conversation.update!(assignee: agent)
|
||||
end
|
||||
|
||||
it 'unassigns the assignee from the conversation' do
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Conversation Participants API', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:conversation) { create(:conversation, account: account) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:inbox_member, inbox: conversation.inbox, user: agent)
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/conversations/<id>/paricipants' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id)
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user with access to the conversation' do
|
||||
let(:participant1) { create(:user, account: account, role: :agent) }
|
||||
let(:participant2) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant1)
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant2)
|
||||
end
|
||||
|
||||
it 'returns all the partipants for the conversation' do
|
||||
create(:conversation_participant, conversation: conversation, user: participant1)
|
||||
create(:conversation_participant, conversation: conversation, user: participant2)
|
||||
get api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include(participant1.email)
|
||||
expect(response.body).to include(participant2.email)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/conversations/<id>/participants' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id)
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:participant) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant)
|
||||
end
|
||||
|
||||
it 'creates a new participants when its authorized agent' do
|
||||
params = { user_ids: [participant.id] }
|
||||
|
||||
expect(conversation.conversation_participants.count).to eq(0)
|
||||
post api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include(participant.email)
|
||||
expect(conversation.conversation_participants.count).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /api/v1/accounts/{account.id}/conversations/<id>/participants' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
put api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id)
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:participant) { create(:user, account: account, role: :agent) }
|
||||
let(:participant_to_be_added) { create(:user, account: account, role: :agent) }
|
||||
let(:participant_to_be_removed) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant)
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant_to_be_added)
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant_to_be_removed)
|
||||
end
|
||||
|
||||
it 'updates participants when its authorized agent' do
|
||||
params = { user_ids: [participant.id, participant_to_be_added.id] }
|
||||
create(:conversation_participant, conversation: conversation, user: participant)
|
||||
create(:conversation_participant, conversation: conversation, user: participant_to_be_removed)
|
||||
|
||||
expect(conversation.conversation_participants.count).to eq(2)
|
||||
put api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include(participant.email)
|
||||
expect(response.body).to include(participant_to_be_added.email)
|
||||
expect(conversation.conversation_participants.count).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /api/v1/accounts/{account.id}/conversations/<id>/participants' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
delete api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id)
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:participant) { create(:user, account: account, role: :agent) }
|
||||
|
||||
before do
|
||||
create(:inbox_member, inbox: conversation.inbox, user: participant)
|
||||
end
|
||||
|
||||
it 'deletes participants when its authorized agent' do
|
||||
params = { user_ids: [participant.id] }
|
||||
create(:conversation_participant, conversation: conversation, user: participant)
|
||||
|
||||
expect(conversation.conversation_participants.count).to eq(1)
|
||||
delete api_v1_account_conversation_participants_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.conversation_participants.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -111,11 +111,11 @@ RSpec.describe 'Reports API', type: :request do
|
||||
context 'when an agent1 associated to conversation having first reply from agent2' do
|
||||
let(:listener) { ReportingEventListener.instance }
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent2) { create(:user, account: account, role: :agent) }
|
||||
|
||||
it 'returns unattended conversation count zero for agent1' do
|
||||
agent1 = create(:user, account: account, role: :agent)
|
||||
agent2 = create(:user, account: account, role: :agent)
|
||||
create(:inbox_member, user: agent, inbox: inbox)
|
||||
create(:inbox_member, user: agent2, inbox: inbox)
|
||||
conversation = create(:conversation, account: account,
|
||||
inbox: inbox, assignee: agent2)
|
||||
|
||||
@@ -129,7 +129,7 @@ RSpec.describe 'Reports API', type: :request do
|
||||
event = Events::Base.new('first.reply.created', Time.zone.now, message: first_reply_message)
|
||||
listener.first_reply_created(event)
|
||||
|
||||
conversation.assignee_id = agent1.id
|
||||
conversation.assignee_id = agent.id
|
||||
conversation.save!
|
||||
|
||||
get "/api/v2/accounts/#{account.id}/reports/conversations",
|
||||
@@ -140,7 +140,7 @@ RSpec.describe 'Reports API', type: :request do
|
||||
as: :json
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
user_metrics = json_response.find { |item| item['name'] == agent1[:name] }
|
||||
user_metrics = json_response.find { |item| item['name'] == agent[:name] }
|
||||
expect(user_metrics.present?).to be true
|
||||
|
||||
expect(user_metrics['metric']['open']).to eq(1)
|
||||
|
||||
Reference in New Issue
Block a user