diff --git a/app/controllers/api/v1/accounts/conversations/assignments_controller.rb b/app/controllers/api/v1/accounts/conversations/assignments_controller.rb index 58fbc5b4e..670abf37a 100644 --- a/app/controllers/api/v1/accounts/conversations/assignments_controller.rb +++ b/app/controllers/api/v1/accounts/conversations/assignments_controller.rb @@ -1,9 +1,20 @@ class Api::V1::Accounts::Conversations::AssignmentsController < Api::V1::Accounts::Conversations::BaseController - # assign agent to a conversation + # assigns agent/team to a conversation def create + set_assignee + render json: @assignee + end + + private + + def set_assignee # if params[:assignee_id] is not a valid id, it will set to nil, hence unassigning the conversation - assignee = Current.account.users.find_by(id: params[:assignee_id]) - @conversation.update_assignee(assignee) - render json: assignee + if params.key?(:assignee_id) + @assignee = Current.account.users.find_by(id: params[:assignee_id]) + @conversation.update_assignee(@assignee) + elsif params.key?(:team_id) + @assignee = Current.account.teams.find_by(id: params[:team_id]) + @conversation.update!(team: @assignee) + end end end diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index 2aa7cb321..4336aff11 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -22,10 +22,12 @@ class ConversationFinder def perform set_inboxes + set_team set_assignee_type find_all_conversations filter_by_status unless params[:q] + filter_by_team if @team filter_by_labels if params[:labels] filter_by_query if params[:q] @@ -61,6 +63,10 @@ class ConversationFinder @assignee_type = params[:assignee_type] end + def set_team + @team = current_account.teams.find(params[:team_id]) if params[:team_id] + end + def find_all_conversations @conversations = current_account.conversations.where(inbox_id: @inbox_ids) end @@ -87,6 +93,10 @@ class ConversationFinder @conversations = @conversations.where(status: params[:status] || DEFAULT_STATUS) end + def filter_by_team + @conversations = @conversations.where(team: @team) + end + def filter_by_labels @conversations = @conversations.tagged_with(params[:labels], any: true) end diff --git a/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb index 859798530..ad79a04ff 100644 --- a/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations/assignments_controller_spec.rb @@ -16,6 +16,7 @@ RSpec.describe 'Conversation Assignment API', type: :request do context 'when it is an authenticated user' do let(:agent) { create(:user, account: account, role: :agent) } + let(:team) { create(:team, account: account) } it 'assigns a user to the conversation' do params = { assignee_id: agent.id } @@ -28,6 +29,18 @@ RSpec.describe 'Conversation Assignment API', type: :request do expect(response).to have_http_status(:success) expect(conversation.reload.assignee).to eq(agent) end + + it 'assigns a team to the conversation' do + params = { team_id: team.id } + + post api_v1_account_conversation_assignments_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.reload.team).to eq(team) + end end context 'when conversation already has an assignee' do @@ -37,7 +50,7 @@ RSpec.describe 'Conversation Assignment API', type: :request do conversation.update!(assignee: agent) end - it 'unassigns a user from the conversation' do + it 'unassigns the assignee from the conversation' do params = { assignee_id: 0 } post api_v1_account_conversation_assignments_url(account_id: account.id, conversation_id: conversation.display_id), params: params, @@ -49,5 +62,25 @@ RSpec.describe 'Conversation Assignment API', type: :request do expect(conversation.messages.last.content).to eq("Conversation unassigned by #{agent.name}") end end + + context 'when conversation already has a team' do + let(:agent) { create(:user, account: account, role: :agent) } + let(:team) { create(:team, account: account) } + + before do + conversation.update!(team: team) + end + + it 'unassigns the team from the conversation' do + params = { team_id: 0 } + post api_v1_account_conversation_assignments_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.reload.team).to eq(nil) + end + end end end diff --git a/spec/finders/conversation_finder_spec.rb b/spec/finders/conversation_finder_spec.rb index 5a0a01403..d10aa1e1d 100644 --- a/spec/finders/conversation_finder_spec.rb +++ b/spec/finders/conversation_finder_spec.rb @@ -37,6 +37,17 @@ describe ::ConversationFinder do end end + context 'with team' do + let(:team) { create(:team, account: account) } + let(:params) { { team_id: team.id } } + + it 'filter conversations by team' do + create(:conversation, account: account, inbox: inbox, team: team) + result = conversation_finder.perform + expect(result[:conversations].count).to be 1 + end + end + context 'with labels' do let(:params) { { labels: ['resolved'] } }