feat: Add team assignment & filter APIs (#1712)

This commit is contained in:
Sojan Jose 2021-01-31 12:40:02 +05:30 committed by GitHub
parent b0563a666e
commit 0ff81e3b53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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'] } }