AgentBot-owned conversations are now treated as assigned across conversation lists, counts, pagination, permissions, unread membership, human auto-assignment, and advanced assignee filters. ## Closes - https://linear.app/chatwoot/issue/CW-7689/align-agent-bot-ownership-with-unassigned-counts-and-pagination ## Follow-ups - https://linear.app/chatwoot/issue/CW-7870/refresh-agentbot-ownership-state-when-deleting-an-agent-bot tracks ownership refresh during AgentBot deletion. - https://linear.app/chatwoot/issue/CW-7899/refresh-saved-filter-totals-after-live-conversation-ownership-changes tracks the existing saved-filter header count refresh gap. ## Why The backend treated every conversation without a human assignee as unassigned, even when an AgentBot owned it. The frontend already hid AgentBot-owned conversations from the Unassigned list, so counts, pagination, filters, unread membership, direct-access permissions, and auto-assignment could disagree with the visible queue. ## What changed - Treat conversations with either a human assignee or AgentBot owner as assigned. - Keep conversation counts, pagination, unread memberships, advanced filters, and automation assignee conditions aligned with the shared ownership semantics. - Keep human assignee equality and not-equality filters human-only, even when a human and AgentBot have the same numeric ID. - Exclude AgentBot-owned conversations from both legacy and V2 human auto-assignment, and from unassigned-only Enterprise access. - Preserve AgentBot ownership when Twilio or WhatsApp call flows reuse or accept an assigned conversation. - Emit ownership-change updates when only the AgentBot owner changes, so connected clients refresh queue state. ## Validation - AgentBot assignment changed ownership to the bot, moved the conversation to pending, and removed it from the open queue. - Assignee "is present" returned human- and AgentBot-owned conversations; "is not present" returned only genuinely unassigned conversations. - Automation assignee presence conditions treated AgentBot ownership as present and did not execute the absent-owner path. - Live human-assignee equality and not-equality filters excluded AgentBot-owned conversations, including numeric ID collisions. - A 32-conversation pending queue loaded across pagination with matching totals and no missing or duplicate rows. - AgentBot ownership changes and human takeover updated filtered rows immediately without a reload. - Human takeover opened the conversation and restored the public reply composer; subsequent unassignment kept the conversation open. - An unassigned-only custom-role agent saw only genuinely unassigned conversations and could not see AgentBot-owned conversations. - AgentBot-owned conversations showed the handled-by-bot banner, Take over action, and disabled public reply composer. - New conversations in the connected inbox were assigned to the AgentBot and excluded from human auto-assignment. - Opening an AgentBot-owned conversation did not let the legacy assignment callback or its locked recheck replace the bot. - Moving an AgentBot-owned conversation to an auto-assigning team preserved the bot and did not create a second human owner. - Twilio conference pickup, Twilio outbound reuse, WhatsApp outbound reuse, and inbound WhatsApp acceptance preserved existing AgentBot owners. - Focused ownership, filters, pagination, permissions, unread-count, auto-assignment, frontend, and lint checks passed locally. - GitHub Actions, Docker builds, CircleCI, security checks, and the final Codex review are green on the final head.
51 lines
2.0 KiB
Ruby
51 lines
2.0 KiB
Ruby
module AutoAssignmentHandler
|
|
extend ActiveSupport::Concern
|
|
include Events::Types
|
|
|
|
included do
|
|
after_save :run_auto_assignment
|
|
end
|
|
|
|
private
|
|
|
|
def run_auto_assignment
|
|
# Assignment V2: Also trigger assignment when conversation is resolved or snoozed,
|
|
# bypassing the open-only condition so the AssignmentJob can redistribute capacity.
|
|
return unless conversation_status_changed_to_open? || conversation_status_changed_to_resolved_or_snoozed?
|
|
return unless should_run_auto_assignment?
|
|
|
|
if inbox.auto_assignment_v2_enabled?
|
|
# Coalesces bursts of triggers per inbox. Fine if the job runs even when the
|
|
# surrounding save rolls back: it only scans the inbox's current unassigned
|
|
# conversations, so running it for an uncommitted change is harmless.
|
|
AutoAssignment::AssignmentJob.enqueue_for_inbox(inbox.id)
|
|
else
|
|
# Use legacy assignment system
|
|
# If conversation has a team, only consider team members for assignment
|
|
allowed_agent_ids = team_id.present? ? team_member_ids_with_capacity : inbox.member_ids_with_assignment_capacity
|
|
AutoAssignment::AgentAssignmentService.new(conversation: self, allowed_agent_ids: allowed_agent_ids).perform
|
|
end
|
|
end
|
|
|
|
def conversation_status_changed_to_resolved_or_snoozed?
|
|
inbox.auto_assignment_v2_enabled? && saved_change_to_status? && (resolved? || snoozed?)
|
|
end
|
|
|
|
def team_member_ids_with_capacity
|
|
return [] if team.blank? || team.allow_auto_assign.blank?
|
|
|
|
inbox.member_ids_with_assignment_capacity & team.members.ids
|
|
end
|
|
|
|
def should_run_auto_assignment?
|
|
return false unless inbox.enable_auto_assignment?
|
|
# Assignment V2: Resolved/snoozed conversations still have an assignee, so bypass the
|
|
# assignee-blank check below. The AssignmentJob needs to run to rebalance assignments.
|
|
return true if conversation_status_changed_to_resolved_or_snoozed?
|
|
return false if assignee_agent_bot_id.present?
|
|
|
|
# run only if assignee is blank or doesn't have access to inbox
|
|
assignee.blank? || inbox.members.exclude?(assignee)
|
|
end
|
|
end
|