fix(escalations): fall back to account_id when team_id is null

Users without team_id (solo accounts, pro plans) couldn't see
escalations because the query filtered by team_id which was NULL.
Now falls back to account_id scoping for both the escalation queue
endpoint and the sidebar badge count.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-21 05:04:47 +00:00
parent cf23107735
commit a7b916116d
2 changed files with 17 additions and 5 deletions

View File

@@ -406,14 +406,19 @@ async def get_escalation_queue(
db: Annotated[AsyncSession, Depends(get_db)],
_: None = Depends(require_engineer_or_admin),
):
"""List sessions requesting escalation for the current user's team."""
if not current_user.team_id:
"""List sessions requesting escalation for the current user's team/account."""
# Match by team_id if available, otherwise fall back to account_id
if current_user.team_id:
scope_filter = AISession.team_id == current_user.team_id
elif current_user.account_id:
scope_filter = AISession.account_id == current_user.account_id
else:
return []
result = await db.execute(
select(AISession)
.where(
AISession.team_id == current_user.team_id,
scope_filter,
AISession.status == "requesting_escalation",
)
.order_by(AISession.created_at.desc())

View File

@@ -147,13 +147,20 @@ async def get_sidebar_stats(
for row in recent_result.all()
]
# --- Escalation count (team-wide pending escalations) ---
# --- Escalation count (team/account-wide pending escalations) ---
escalation_count = 0
if current_user.team_id:
esc_scope = AISession.team_id == current_user.team_id
elif current_user.account_id:
esc_scope = AISession.account_id == current_user.account_id
else:
esc_scope = None
if esc_scope is not None:
esc_result = await db.execute(
select(func.count()).where(
and_(
AISession.team_id == current_user.team_id,
esc_scope,
AISession.status == "requesting_escalation",
)
)