File size: 2,035 Bytes
71c1ad2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# app/api/v1/users.py
# User profile and parent-child management endpoints

from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel
from typing import Optional
from app.core.dependencies import get_current_user
from app.db.models.user import UserDocument, UserRole

router = APIRouter(prefix="/users", tags=["Users"])


@router.get("/me")
async def get_profile(user: UserDocument = Depends(get_current_user)):
    return {"success": True, "user": user.to_public()}


class UpdateProfileRequest(BaseModel):
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    phone: Optional[str] = None


@router.patch("/me")
async def update_profile(
    body: UpdateProfileRequest,
    user: UserDocument = Depends(get_current_user),
):
    if body.first_name:
        user.first_name = body.first_name
    if body.last_name:
        user.last_name = body.last_name
    if body.phone is not None:
        user.phone = body.phone
    await user.save()
    return {"success": True, "user": user.to_public()}


@router.get("/children")
async def list_children(user: UserDocument = Depends(get_current_user)):
    """Parent: list all linked child accounts."""
    if user.role != UserRole.PARENT:
        raise HTTPException(status_code=403, detail="Only parents can list children")
    children = await UserDocument.find(
        UserDocument.parent_id == str(user.id)
    ).to_list()
    return {"success": True, "children": [c.to_public() for c in children]}


@router.get("/children/{child_id}")
async def get_child(
    child_id: str,
    user: UserDocument = Depends(get_current_user),
):
    """Parent: get a specific child's profile."""
    if user.role != UserRole.PARENT:
        raise HTTPException(status_code=403, detail="Only parents can view child profiles")
    child = await UserDocument.get(child_id)
    if not child or child.parent_id != str(user.id):
        raise HTTPException(status_code=404, detail="Child not found")
    return {"success": True, "child": child.to_public()}