From 4635e5bb446d8458848f8130e062f1df8e15c06f Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 20 Apr 2021 13:46:20 +0530 Subject: [PATCH] feat: Assignable agents API (#2131) `inboxes/id/assignable_agents` returns all users that have permission to a conversation happening in that inbox. --- .../api/v1/accounts/inboxes_controller.rb | 4 +++ app/policies/inbox_policy.rb | 4 +++ .../inboxes/assignable_agents.json.jbuilder | 5 +++ config/routes.rb | 1 + .../v1/accounts/inboxes_controller_spec.rb | 32 +++++++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 app/views/api/v1/accounts/inboxes/assignable_agents.json.jbuilder diff --git a/app/controllers/api/v1/accounts/inboxes_controller.rb b/app/controllers/api/v1/accounts/inboxes_controller.rb index 7928a3d41..c8d01a1c7 100644 --- a/app/controllers/api/v1/accounts/inboxes_controller.rb +++ b/app/controllers/api/v1/accounts/inboxes_controller.rb @@ -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 diff --git a/app/policies/inbox_policy.rb b/app/policies/inbox_policy.rb index 230a4c131..2968129db 100644 --- a/app/policies/inbox_policy.rb +++ b/app/policies/inbox_policy.rb @@ -23,6 +23,10 @@ class InboxPolicy < ApplicationPolicy true end + def assignable_agents? + true + end + def create? @account_user.administrator? end diff --git a/app/views/api/v1/accounts/inboxes/assignable_agents.json.jbuilder b/app/views/api/v1/accounts/inboxes/assignable_agents.json.jbuilder new file mode 100644 index 000000000..af71dea1e --- /dev/null +++ b/app/views/api/v1/accounts/inboxes/assignable_agents.json.jbuilder @@ -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 diff --git a/config/routes.rb b/config/routes.rb index d7fe02f1a..74a778d55 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb index 0c52e869c..76d2d9f00 100644 --- a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb @@ -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) }