Coverage for api/admin_attention.py: 90.91%
31 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:10 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 15:10 +0000
1"""
2Admin attention badge API (SB-4X).
4Powers the red dot + hover tooltip on the global top-nav "Admin" link.
5One endpoint, one round trip per poll — aggregates pending counts across
6the Access sub-sections that actually have a request queue.
8Live Match Notifications is excluded — it's a per-club config surface
9with no pending state to count.
10"""
12from __future__ import annotations
14import os
15import sys
16from typing import Any
18import structlog
19from fastapi import APIRouter, Depends
20from pydantic import BaseModel
22from supabase import create_client
24sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
26from auth import require_admin
27from dao.admin_attention_dao import AdminAttentionDAO
28from dao.match_dao import SupabaseConnection as DbConnectionHolder
30logger = structlog.get_logger(__name__)
32# Service-role connection — require_admin is the trust gate, same pattern
33# as backend/api/admin_emails.py.
34_supabase_url = os.getenv("SUPABASE_URL", "")
35_service_key = os.getenv("SUPABASE_SERVICE_KEY")
36if _service_key: 36 ↛ 41line 36 didn't jump to line 41 because the condition on line 36 was always true
37 _service_client = create_client(_supabase_url, _service_key)
38 _conn_holder = DbConnectionHolder()
39 _conn_holder.client = _service_client
40else:
41 _conn_holder = DbConnectionHolder()
43_dao = AdminAttentionDAO(_conn_holder)
45router = APIRouter(prefix="/api/admin/attention", tags=["admin-attention"])
48class AttentionCounts(BaseModel):
49 invite_requests: int
50 channel_requests: int
51 support_inbox: int
52 content_reports: int
53 total: int
56@router.get("/counts", response_model=AttentionCounts)
57async def get_attention_counts(
58 _admin: dict[str, Any] = Depends(require_admin),
59) -> dict[str, int]:
60 """Per-queue pending counts for the admin nav badge.
62 Cached 30s via Redis when CACHE_ENABLED — repeat polls from multiple
63 admin sessions share one DB fan-out per window. With Redis off, every
64 call hits the DB fresh (still cheap — three count queries).
65 """
66 return _dao.get_counts()