From 73f2aea51411fc4fede966cb4281cdce2a6961f7 Mon Sep 17 00:00:00 2001 From: Tanmay Sharma Date: Thu, 11 Sep 2025 13:38:22 +0530 Subject: [PATCH] fix user duplication issue for assignment user list --- .../users_controller.rb | 4 ++-- .../users_controller_spec.rb | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) 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/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