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:
parent
f203f376e9
commit
9de949d212
@ -31,12 +31,9 @@ def _resolve_institute_id(user_id: str) -> Optional[str]:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _require_institute(user_id: str) -> str:
|
def _require_institute(user_id: str) -> Optional[str]:
|
||||||
"""Return institute_id or raise 400."""
|
"""Return institute_id, or None if the user has no school membership."""
|
||||||
institute_id = _resolve_institute_id(user_id)
|
return _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 _is_school_admin(user_id: str, institute_id: str) -> bool:
|
def _is_school_admin(user_id: str, institute_id: str) -> bool:
|
||||||
@ -105,6 +102,8 @@ async def list_classes(
|
|||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
user_id = credentials.get("sub", "")
|
user_id = credentials.get("sub", "")
|
||||||
institute_id = _require_institute(user_id)
|
institute_id = _require_institute(user_id)
|
||||||
|
if not institute_id:
|
||||||
|
return {"classes": [], "total": 0}
|
||||||
sb = _sb()
|
sb = _sb()
|
||||||
|
|
||||||
q = sb.supabase.table("classes").select("*", count="exact").eq("institute_id", institute_id)
|
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]:
|
) -> Dict[str, Any]:
|
||||||
user_id = credentials.get("sub", "")
|
user_id = credentials.get("sub", "")
|
||||||
institute_id = _require_institute(user_id)
|
institute_id = _require_institute(user_id)
|
||||||
|
if not institute_id:
|
||||||
|
return {"classes": []}
|
||||||
sb = _sb()
|
sb = _sb()
|
||||||
|
|
||||||
assigned = (
|
assigned = (
|
||||||
@ -204,6 +205,8 @@ async def my_student_classes(
|
|||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
user_id = credentials.get("sub", "")
|
user_id = credentials.get("sub", "")
|
||||||
institute_id = _require_institute(user_id)
|
institute_id = _require_institute(user_id)
|
||||||
|
if not institute_id:
|
||||||
|
return {"classes": []}
|
||||||
sb = _sb()
|
sb = _sb()
|
||||||
|
|
||||||
enrolled = (
|
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."""
|
"""List all students in the caller's school. Used by admin to add students to a class."""
|
||||||
user_id = credentials.get("sub", "")
|
user_id = credentials.get("sub", "")
|
||||||
institute_id = _require_institute(user_id)
|
institute_id = _require_institute(user_id)
|
||||||
|
if not institute_id:
|
||||||
|
return {"students": []}
|
||||||
sb = _sb()
|
sb = _sb()
|
||||||
members = (
|
members = (
|
||||||
sb.supabase.table("institute_memberships")
|
sb.supabase.table("institute_memberships")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user