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
This commit is contained in:
CC Worker 2026-06-02 23:36:05 +00:00
parent f203f376e9
commit 9de949d212

View File

@ -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")