chore: Add Assignable Agents API (#4722)

This commit is contained in:
Pranav Raj S 2022-05-23 19:24:07 +05:30 committed by GitHub
parent 79280ae372
commit 56cd9bc0b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 134 additions and 15 deletions

View file

@ -0,0 +1,24 @@
class Api::V1::Accounts::AssignableAgentsController < Api::V1::Accounts::BaseController
before_action :fetch_inboxes
def index
agent_ids = @inboxes.map do |inbox|
authorize inbox, :show?
member_ids = inbox.members.pluck(:user_id)
member_ids
end
agent_ids = agent_ids.inject(:&)
agents = Current.account.users.where(id: agent_ids)
@assignable_agents = (agents + Current.account.administrators).uniq
end
private
def fetch_inboxes
@inboxes = Current.account.inboxes.find(permitted_params[:inbox_ids])
end
def permitted_params
params.permit(inbox_ids: [])
end
end

View file

@ -12,6 +12,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
def show; end
# Deprecated: This API will be removed in 2.7.0
def assignable_agents
@assignable_agents = (Current.account.users.where(id: @inbox.members.select(:user_id)) + Current.account.administrators).uniq
end

View file

@ -0,0 +1,16 @@
/* global axios */
import ApiClient from './ApiClient';
class AssignableAgents extends ApiClient {
constructor() {
super('assignable_agents', { accountScoped: true });
}
get(inboxIds) {
return axios.get(this.url, {
params: { inbox_ids: inboxIds },
});
}
}
export default new AssignableAgents();

View file

@ -6,10 +6,6 @@ class Inboxes extends ApiClient {
super('inboxes', { accountScoped: true });
}
getAssignableAgents(inboxId) {
return axios.get(`${this.url}/${inboxId}/assignable_agents`);
}
getCampaigns(inboxId) {
return axios.get(`${this.url}/${inboxId}/campaigns`);
}

View file

@ -0,0 +1,18 @@
import assignableAgentsAPI from '../assignableAgents';
import describeWithAPIMock from './apiSpecHelper';
describe('#AssignableAgentsAPI', () => {
describeWithAPIMock('API calls', context => {
it('#getAssignableAgents', () => {
assignableAgentsAPI.get([1]);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/assignable_agents',
{
params: {
inbox_ids: [1],
},
}
);
});
});
});

View file

@ -10,17 +10,9 @@ describe('#InboxesAPI', () => {
expect(inboxesAPI).toHaveProperty('create');
expect(inboxesAPI).toHaveProperty('update');
expect(inboxesAPI).toHaveProperty('delete');
expect(inboxesAPI).toHaveProperty('getAssignableAgents');
expect(inboxesAPI).toHaveProperty('getCampaigns');
});
describeWithAPIMock('API calls', context => {
it('#getAssignableAgents', () => {
inboxesAPI.getAssignableAgents(1);
expect(context.axiosMock.get).toHaveBeenCalledWith(
'/api/v1/inboxes/1/assignable_agents'
);
});
it('#getCampaigns', () => {
inboxesAPI.getCampaigns(2);
expect(context.axiosMock.get).toHaveBeenCalledWith(

View file

@ -1,6 +1,6 @@
import Vue from 'vue';
import InboxesAPI from 'dashboard/api/inboxes.js';
import AssignableAgentsAPI from '../../api/assignableAgents';
const state = {
records: {},
@ -31,7 +31,7 @@ export const actions = {
try {
const {
data: { payload },
} = await InboxesAPI.getAssignableAgents(inboxId);
} = await AssignableAgentsAPI.get([inboxId]);
commit(types.SET_INBOX_ASSIGNABLE_AGENTS, { inboxId, members: payload });
} catch (error) {
throw new Error(error);

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

@ -43,7 +43,7 @@ Rails.application.routes.draw do
resource :bulk_actions, only: [:create]
resources :agents, only: [:index, :create, :update, :destroy]
resources :agent_bots, only: [:index, :create, :show, :update, :destroy]
resources :assignable_agents, only: [:index]
resources :callbacks, only: [] do
collection do
post :register_facebook_page

View file

@ -0,0 +1,67 @@
require 'rails_helper'
RSpec.describe 'Assignable Agents API', type: :request do
let(:account) { create(:account) }
let(:agent1) { create(:user, account: account, role: :agent) }
let!(:agent2) { create(:user, account: account, role: :agent) }
let!(:admin) { create(:user, account: account, role: :administrator) }
describe 'GET /api/v1/accounts/{account.id}/assignable_agents' do
let(:inbox1) { create(:inbox, account: account) }
let(:inbox2) { create(:inbox, account: account) }
before do
create(:inbox_member, user: agent1, inbox: inbox1)
create(:inbox_member, user: agent1, inbox: inbox2)
end
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/assignable_agents"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when the user is not part of an inbox' do
context 'when the user is an admininstrator' do
it 'returns all assignable inbox members along with administrators' do
get "/api/v1/accounts/#{account.id}/assignable_agents",
params: { inbox_ids: [inbox1.id, inbox2.id] },
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
context 'when the user is an agent' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/assignable_agents",
params: { inbox_ids: [inbox1.id, inbox2.id] },
headers: agent2.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
end
context 'when the user is part of the inbox' do
it 'returns all assignable inbox members along with administrators' do
get "/api/v1/accounts/#{account.id}/assignable_agents",
params: { inbox_ids: [inbox1.id, inbox2.id] },
headers: agent1.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
end