From 9de949d21283ca4d9b983cd7a1b0e6043ea4fcce Mon Sep 17 00:00:00 2001 From: CC Worker Date: Tue, 2 Jun 2026 23:36:05 +0000 Subject: [PATCH] R6-E2: return empty collections when user has no school - _require_institute returns Optional[str] instead of raising 400 - list_classes / my_teaching_classes / my_student_classes / list_school_students now return empty arrays when school_id is missing --- routers/database/tools/classes_router.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/routers/database/tools/classes_router.py b/routers/database/tools/classes_router.py index a8b82d5..62af7b1 100644 --- a/routers/database/tools/classes_router.py +++ b/routers/database/tools/classes_router.py @@ -31,12 +31,9 @@ def _resolve_institute_id(user_id: str) -> Optional[str]: return None -def _require_institute(user_id: str) -> str: - """Return institute_id or raise 400.""" - institute_id = _resolve_institute_id(user_id) - if not institute_id: - raise HTTPException(status_code=400, detail="User is not linked to a school") - return institute_id +def _require_institute(user_id: str) -> Optional[str]: + """Return institute_id, or None if the user has no school membership.""" + return _resolve_institute_id(user_id) def _is_school_admin(user_id: str, institute_id: str) -> bool: @@ -105,6 +102,8 @@ async def list_classes( ) -> Dict[str, Any]: user_id = credentials.get("sub", "") institute_id = _require_institute(user_id) + if not institute_id: + return {"classes": [], "total": 0} sb = _sb() q = sb.supabase.table("classes").select("*", count="exact").eq("institute_id", institute_id) @@ -169,6 +168,8 @@ async def my_teaching_classes( ) -> Dict[str, Any]: user_id = credentials.get("sub", "") institute_id = _require_institute(user_id) + if not institute_id: + return {"classes": []} sb = _sb() assigned = ( @@ -204,6 +205,8 @@ async def my_student_classes( ) -> Dict[str, Any]: user_id = credentials.get("sub", "") institute_id = _require_institute(user_id) + if not institute_id: + return {"classes": []} sb = _sb() enrolled = ( @@ -237,6 +240,8 @@ async def list_school_students( """List all students in the caller's school. Used by admin to add students to a class.""" user_id = credentials.get("sub", "") institute_id = _require_institute(user_id) + if not institute_id: + return {"students": []} sb = _sb() members = ( sb.supabase.table("institute_memberships")