Production
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import client_ip, get_current_user, require_role
|
||||
from app.common.enums import Role
|
||||
from app.database.session import get_db
|
||||
from app.doctors.models import Doctor
|
||||
from app.doctors.schemas import AssignToMR, DoctorCreate, DoctorRead, DoctorUpdate
|
||||
from app.doctors.service import DoctorService
|
||||
from app.users.models import User
|
||||
|
||||
router = APIRouter(prefix="/doctors", tags=["doctors"])
|
||||
|
||||
|
||||
@router.get("", response_model=list[DoctorRead])
|
||||
async def list_doctors(
|
||||
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
||||
) -> list[Doctor]:
|
||||
"""S5 — an MR gets only their assigned doctors; managers get their HQ scope."""
|
||||
return await DoctorService(db).list_for(current_user)
|
||||
|
||||
|
||||
@router.post("", response_model=DoctorRead, status_code=status.HTTP_201_CREATED)
|
||||
async def create_doctor(
|
||||
payload: DoctorCreate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Doctor:
|
||||
"""Only an Administrator can create a doctor (MR, AM and GM cannot)."""
|
||||
return await DoctorService(db).create(payload, current_user, client_ip(request))
|
||||
|
||||
|
||||
@router.patch("/{doctor_id}", response_model=DoctorRead)
|
||||
async def update_doctor(
|
||||
doctor_id: uuid.UUID,
|
||||
payload: DoctorUpdate,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Doctor:
|
||||
return await DoctorService(db).update(doctor_id, payload, current_user, client_ip(request))
|
||||
|
||||
|
||||
@router.post("/{doctor_id}/assign", status_code=status.HTTP_201_CREATED)
|
||||
async def assign_doctor(
|
||||
doctor_id: uuid.UUID,
|
||||
payload: AssignToMR,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.GM, Role.AM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""An AM may assign only within their own headquarters — enforced by data scope."""
|
||||
assignment = await DoctorService(db).assign_to_mr(
|
||||
doctor_id, payload.mr_user_id, payload.effective_from, current_user, client_ip(request)
|
||||
)
|
||||
return {
|
||||
"id": assignment.id,
|
||||
"doctor_id": assignment.doctor_id,
|
||||
"mr_user_id": assignment.mr_user_id,
|
||||
"effective_from": assignment.effective_from,
|
||||
}
|
||||
Reference in New Issue
Block a user