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