|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from fastapi import APIRouter, Depends, HTTPException, Query, Request |
| 4 | +from pydantic import BaseModel, Field |
| 5 | +from sqlalchemy.orm import Session |
| 6 | + |
| 7 | +from app.middleware.auth import has_roles |
| 8 | +from app.schemas.user import UserRole |
| 9 | +from app.services.implementations.ranking_service import RankingService |
| 10 | +from app.utilities.db_utils import get_db |
| 11 | + |
| 12 | + |
| 13 | +class StaticQualityOption(BaseModel): |
| 14 | + quality_id: int |
| 15 | + slug: str |
| 16 | + label: str |
| 17 | + allowed_scopes: List[str] | None = Field(default=None, description="Optional whitelisted scopes for this quality") |
| 18 | + |
| 19 | + |
| 20 | +class DynamicOption(BaseModel): |
| 21 | + kind: str # 'treatment' | 'experience' |
| 22 | + id: int |
| 23 | + name: str |
| 24 | + scope: str # 'self' | 'loved_one' |
| 25 | + |
| 26 | + |
| 27 | +class RankingOptionsResponse(BaseModel): |
| 28 | + static_qualities: List[StaticQualityOption] |
| 29 | + dynamic_options: List[DynamicOption] |
| 30 | + |
| 31 | + |
| 32 | +router = APIRouter(prefix="/ranking", tags=["ranking"]) |
| 33 | + |
| 34 | + |
| 35 | +@router.get("/options", response_model=RankingOptionsResponse) |
| 36 | +async def get_ranking_options( |
| 37 | + request: Request, |
| 38 | + target: str = Query(..., pattern="^(patient|caregiver)$"), |
| 39 | + db: Session = Depends(get_db), |
| 40 | + authorized: bool = has_roles([UserRole.PARTICIPANT, UserRole.ADMIN]), |
| 41 | +) -> RankingOptionsResponse: |
| 42 | + try: |
| 43 | + service = RankingService(db) |
| 44 | + user_auth_id = request.state.user_id |
| 45 | + options = service.get_options(user_auth_id=user_auth_id, target=target) |
| 46 | + return RankingOptionsResponse(**options) |
| 47 | + except HTTPException: |
| 48 | + raise |
| 49 | + except Exception as e: |
| 50 | + raise HTTPException(status_code=500, detail=str(e)) |
| 51 | + |
| 52 | + |
| 53 | +class PreferenceItem(BaseModel): |
| 54 | + kind: str # 'quality' | 'treatment' | 'experience' |
| 55 | + id: int |
| 56 | + scope: str # 'self' | 'loved_one' |
| 57 | + rank: int |
| 58 | + |
| 59 | + |
| 60 | +@router.put("/preferences", status_code=204) |
| 61 | +async def put_ranking_preferences( |
| 62 | + request: Request, |
| 63 | + target: str = Query(..., pattern="^(patient|caregiver)$"), |
| 64 | + items: List[PreferenceItem] = [], |
| 65 | + db: Session = Depends(get_db), |
| 66 | + authorized: bool = has_roles([UserRole.PARTICIPANT, UserRole.ADMIN]), |
| 67 | +) -> None: |
| 68 | + try: |
| 69 | + service = RankingService(db) |
| 70 | + user_auth_id = request.state.user_id |
| 71 | + # Convert Pydantic models to dicts |
| 72 | + payload = [i.model_dump() for i in items] |
| 73 | + service.save_preferences(user_auth_id=user_auth_id, target=target, items=payload) |
| 74 | + return None |
| 75 | + except HTTPException: |
| 76 | + raise |
| 77 | + except Exception as e: |
| 78 | + raise HTTPException(status_code=500, detail=str(e)) |
| 79 | + |
| 80 | + |
| 81 | +class CaseResponse(BaseModel): |
| 82 | + case: str |
| 83 | + has_blood_cancer: str | None = None |
| 84 | + caring_for_someone: str | None = None |
| 85 | + |
| 86 | + |
| 87 | +@router.get("/case", response_model=CaseResponse) |
| 88 | +async def get_participant_case( |
| 89 | + request: Request, |
| 90 | + db: Session = Depends(get_db), |
| 91 | + authorized: bool = has_roles([UserRole.PARTICIPANT, UserRole.ADMIN]), |
| 92 | +) -> CaseResponse: |
| 93 | + try: |
| 94 | + service = RankingService(db) |
| 95 | + user_auth_id = request.state.user_id |
| 96 | + result = service.get_case(user_auth_id) |
| 97 | + return CaseResponse(**result) |
| 98 | + except HTTPException: |
| 99 | + raise |
| 100 | + except Exception as e: |
| 101 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments