From a7b916116dabed57823eb963b2fdbd0332a8c26e Mon Sep 17 00:00:00 2001 From: chihlasm Date: Sat, 21 Mar 2026 05:04:47 +0000 Subject: [PATCH] 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 --- backend/app/api/endpoints/ai_sessions.py | 11 ++++++++--- backend/app/api/endpoints/sidebar.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/backend/app/api/endpoints/ai_sessions.py b/backend/app/api/endpoints/ai_sessions.py index c41cd2cf..7fb7d519 100644 --- a/backend/app/api/endpoints/ai_sessions.py +++ b/backend/app/api/endpoints/ai_sessions.py @@ -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()) diff --git a/backend/app/api/endpoints/sidebar.py b/backend/app/api/endpoints/sidebar.py index 0772dd92..40baadc7 100644 --- a/backend/app/api/endpoints/sidebar.py +++ b/backend/app/api/endpoints/sidebar.py @@ -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", ) )