feat: Assignable agents API (#2131)

`inboxes/id/assignable_agents` returns all users that have permission to a conversation happening in that inbox.
This commit is contained in:
Sojan Jose 2021-04-20 13:46:20 +05:30 committed by GitHub
parent fb582614f2
commit 4635e5bb44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 0 deletions

View file

@ -7,6 +7,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
@inboxes = policy_scope(Current.account.inboxes.order_by_name.includes(:channel, { avatar_attachment: [:blob] }))
end
def assignable_agents
@assignable_agents = (Current.account.users.where(id: @inbox.members.select(:user_id)) + Current.account.administrators).uniq
end
def create
ActiveRecord::Base.transaction do
channel = create_channel

View file

@ -23,6 +23,10 @@ class InboxPolicy < ApplicationPolicy
true
end
def assignable_agents?
true
end
def create?
@account_user.administrator?
end

View file

@ -0,0 +1,5 @@
json.payload do
json.array! @assignable_agents do |agent|
json.partial! 'api/v1/models/agent.json.jbuilder', resource: agent
end
end

View file

@ -88,6 +88,7 @@ Rails.application.routes.draw do
end
resources :inboxes, only: [:index, :create, :update, :destroy] do
get :assignable_agents, on: :member
post :set_agent_bot, on: :member
end
resources :inbox_members, only: [:create, :show], param: :inbox_id

View file

@ -42,6 +42,38 @@ RSpec.describe 'Inboxes API', type: :request do
end
end
describe 'GET /api/v1/accounts/{account.id}/inboxes/{inbox.id}/assignable_agents' do
let(:inbox) { create(:inbox, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/assignable_agents"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:admin) { create(:user, account: account, role: :administrator) }
before do
create(:inbox_member, user: agent, inbox: inbox)
end
it 'returns all assignable inbox members along with administrators' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}/assignable_agents",
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
response_data = JSON.parse(response.body, symbolize_names: true)[:payload]
expect(response_data.size).to eq(2)
expect(response_data.pluck(:role)).to include('agent', 'administrator')
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/inboxes/:id' do
let(:inbox) { create(:inbox, account: account) }