## Description Fixes a critical bug where conversations assigned to a team could be auto-assigned to agents outside that team when all team members were at capacity. ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes core assignment selection for both legacy and v2 flows; misconfiguration of `allow_auto_assign` or team membership could cause conversations to remain unassigned. > > **Overview** > Prevents auto-assignment from crossing team boundaries by filtering eligible agents to the conversation’s `team` members (and requiring `team.allow_auto_assign`) in both the legacy `AutoAssignmentHandler` path and the v2 `AutoAssignment::AssignmentService` (including the Enterprise override). > > Adds test coverage to ensure team-scoped conversations only assign to team members, and are skipped when team auto-assign is disabled or no team members are available; also updates the conversations controller spec setup to include team membership. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 67ed2bda0cd8ffd56c7e0253b86369dead2e6155. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
103 lines
2.6 KiB
Ruby
103 lines
2.6 KiB
Ruby
class AutoAssignment::AssignmentService
|
|
pattr_initialize [:inbox!]
|
|
|
|
def perform_bulk_assignment(limit: 100)
|
|
return 0 unless inbox.auto_assignment_v2_enabled?
|
|
return 0 unless inbox.enable_auto_assignment?
|
|
|
|
assigned_count = 0
|
|
|
|
unassigned_conversations(limit).each do |conversation|
|
|
assigned_count += 1 if perform_for_conversation(conversation)
|
|
end
|
|
|
|
assigned_count
|
|
end
|
|
|
|
private
|
|
|
|
def perform_for_conversation(conversation)
|
|
return false unless assignable?(conversation)
|
|
|
|
agent = find_available_agent(conversation)
|
|
return false unless agent
|
|
|
|
assign_conversation(conversation, agent)
|
|
end
|
|
|
|
def assignable?(conversation)
|
|
conversation.status == 'open' &&
|
|
conversation.assignee_id.nil?
|
|
end
|
|
|
|
def unassigned_conversations(limit)
|
|
scope = inbox.conversations.unassigned.open
|
|
|
|
# Apply conversation priority using assignment policy if available
|
|
policy = inbox.assignment_policy
|
|
scope = if policy&.longest_waiting?
|
|
scope.reorder(last_activity_at: :asc, created_at: :asc)
|
|
else
|
|
scope.reorder(created_at: :asc)
|
|
end
|
|
|
|
scope.limit(limit)
|
|
end
|
|
|
|
def find_available_agent(conversation = nil)
|
|
agents = filter_agents_by_team(inbox.available_agents, conversation)
|
|
return nil if agents.nil?
|
|
|
|
agents = filter_agents_by_rate_limit(agents)
|
|
return nil if agents.empty?
|
|
|
|
round_robin_selector.select_agent(agents)
|
|
end
|
|
|
|
def filter_agents_by_team(agents, conversation)
|
|
return agents if conversation&.team_id.blank?
|
|
|
|
team = conversation.team
|
|
return nil if team.blank? || team.allow_auto_assign.blank?
|
|
|
|
team_member_ids = team.members.ids
|
|
agents.where(user_id: team_member_ids)
|
|
end
|
|
|
|
def filter_agents_by_rate_limit(agents)
|
|
agents.select do |agent_member|
|
|
rate_limiter = build_rate_limiter(agent_member.user)
|
|
rate_limiter.within_limit?
|
|
end
|
|
end
|
|
|
|
def assign_conversation(conversation, agent)
|
|
conversation.update!(assignee: agent)
|
|
|
|
rate_limiter = build_rate_limiter(agent)
|
|
rate_limiter.track_assignment(conversation)
|
|
|
|
dispatch_assignment_event(conversation, agent)
|
|
true
|
|
end
|
|
|
|
def dispatch_assignment_event(conversation, agent)
|
|
Rails.configuration.dispatcher.dispatch(
|
|
Events::Types::ASSIGNEE_CHANGED,
|
|
Time.zone.now,
|
|
conversation: conversation,
|
|
user: agent
|
|
)
|
|
end
|
|
|
|
def build_rate_limiter(agent)
|
|
AutoAssignment::RateLimiter.new(inbox: inbox, agent: agent)
|
|
end
|
|
|
|
def round_robin_selector
|
|
@round_robin_selector ||= AutoAssignment::RoundRobinSelector.new(inbox: inbox)
|
|
end
|
|
end
|
|
|
|
AutoAssignment::AssignmentService.prepend_mod_with('AutoAssignment::AssignmentService')
|