diff --git a/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies/users_controller.rb b/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies/users_controller.rb index a49b4f00f..f8b085732 100644 --- a/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies/users_controller.rb +++ b/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies/users_controller.rb @@ -4,8 +4,8 @@ class Api::V1::Accounts::AgentCapacityPolicies::UsersController < Api::V1::Accou before_action :fetch_user, only: [:destroy] def index - @users = Current.account.users.joins(:account_users) - .where(account_users: { agent_capacity_policy_id: @agent_capacity_policy.id }) + @users = User.joins(:account_users) + .where(account_users: { account_id: Current.account.id, agent_capacity_policy_id: @agent_capacity_policy.id }) end def create diff --git a/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies_controller.rb b/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies_controller.rb index d6d166ee5..9ece78762 100644 --- a/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies_controller.rb +++ b/enterprise/app/controllers/api/v1/accounts/agent_capacity_policies_controller.rb @@ -27,7 +27,7 @@ class Api::V1::Accounts::AgentCapacityPoliciesController < Api::V1::Accounts::En params.require(:agent_capacity_policy).permit( :name, :description, - exclusion_rules: [:overall_capacity, { hours: [], days: [] }] + exclusion_rules: [:exclude_older_than_hours, { excluded_labels: [] }] ) end diff --git a/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb index be25151ae..9ed837107 100644 --- a/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies/users_controller_spec.rb @@ -19,6 +19,26 @@ RSpec.describe 'Agent Capacity Policy Users API', type: :request do expect(response).to have_http_status(:success) expect(response.parsed_body.first['id']).to eq(user.id) end + + it 'returns each user only once without duplicates' do + # Assign multiple users to the same policy + user.account_users.first.update!(agent_capacity_policy: agent_capacity_policy) + agent.account_users.first.update!(agent_capacity_policy: agent_capacity_policy) + + get "/api/v1/accounts/#{account.id}/agent_capacity_policies/#{agent_capacity_policy.id}/users", + headers: administrator.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + + # Check that we have exactly 2 users + expect(response.parsed_body.length).to eq(2) + + # Check that each user appears only once + user_ids = response.parsed_body.map { |u| u['id'] } + expect(user_ids).to contain_exactly(user.id, agent.id) + expect(user_ids.uniq).to eq(user_ids) # No duplicates + end end end diff --git a/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies_controller_spec.rb index d6b171fe8..691891027 100644 --- a/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/agent_capacity_policies_controller_spec.rb @@ -103,7 +103,10 @@ RSpec.describe 'Agent Capacity Policies API', type: :request do agent_capacity_policy: { name: 'Test Policy', description: 'Test Description', - exclusion_rules: { overall_capacity: 10 } + exclusion_rules: { + excluded_labels: %w[urgent spam], + exclude_older_than_hours: 24 + } } } @@ -115,6 +118,10 @@ RSpec.describe 'Agent Capacity Policies API', type: :request do expect(response).to have_http_status(:success) expect(response.parsed_body['name']).to eq('Test Policy') expect(response.parsed_body['description']).to eq('Test Description') + expect(response.parsed_body['exclusion_rules']).to eq({ + 'excluded_labels' => %w[urgent spam], + 'exclude_older_than_hours' => 24 + }) end it 'returns validation errors for invalid data' do @@ -165,6 +172,28 @@ RSpec.describe 'Agent Capacity Policies API', type: :request do expect(response).to have_http_status(:success) expect(response.parsed_body['name']).to eq('Updated Policy') end + + it 'updates exclusion rules when administrator' do + params = { + agent_capacity_policy: { + exclusion_rules: { + excluded_labels: %w[vip priority], + exclude_older_than_hours: 48 + } + } + } + + put "/api/v1/accounts/#{account.id}/agent_capacity_policies/#{agent_capacity_policy.id}", + params: params, + headers: administrator.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + expect(response.parsed_body['exclusion_rules']).to eq({ + 'excluded_labels' => %w[vip priority], + 'exclude_older_than_hours' => 48 + }) + end end end