Files
celtic-life-science/app/doctors/router.py
T
Shubhankar45 0d30c58bf0 Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during
server cleanup. .env files, node_modules, builds, logs and media were excluded.
2026-11-07 05:43:12 +00:00

113 lines
4.3 KiB
Python

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, RejectMaster
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[DoctorRead]:
"""S5 — an MR gets only their assigned doctors; managers get their HQ scope."""
rows = await DoctorService(db).list_for(current_user)
return [
DoctorRead.model_validate(doctor).model_copy(
update={"assigned_mr_name": assigned_mr_name, "assigned_mr_id": mr_id}
)
for doctor, assigned_mr_name, mr_id in rows
]
@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, Role.MR)),
db: AsyncSession = Depends(get_db),
) -> Doctor:
"""An Administrator, or an MR within their own headquarters (ABM and AGM 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.delete("/{doctor_id}/assign", status_code=status.HTTP_204_NO_CONTENT)
async def unassign_doctor(
doctor_id: uuid.UUID,
request: Request,
current_user: User = Depends(require_role(Role.ADMIN)),
db: AsyncSession = Depends(get_db),
) -> None:
"""Administrator only: leave the doctor with no MR."""
await DoctorService(db).unassign(doctor_id, 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.AGM, Role.ABM)),
db: AsyncSession = Depends(get_db),
):
"""An ABM 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,
}
@router.get("/requests", response_model=list[DoctorRead])
async def list_doctor_requests(
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
) -> list[Doctor]:
"""Doctors an MR added that wait for the Area Business Manager: an MR sees their own (with any
rejected and why), an Area Business Manager what waits for them, the administrator and AGM all."""
return await DoctorService(db).requests_for(current_user)
@router.post("/{doctor_id}/approve", response_model=DoctorRead)
async def approve_doctor(
doctor_id: uuid.UUID,
request: Request,
current_user: User = Depends(require_role(Role.ABM, Role.ADMIN)),
db: AsyncSession = Depends(get_db),
) -> Doctor:
"""The Area Business Manager of its headquarters adds it to the list, assigned to the MR who asked."""
return await DoctorService(db).review(doctor_id, True, None, current_user, client_ip(request))
@router.post("/{doctor_id}/reject", response_model=DoctorRead)
async def reject_doctor(
doctor_id: uuid.UUID,
payload: RejectMaster,
request: Request,
current_user: User = Depends(require_role(Role.ABM, Role.ADMIN)),
db: AsyncSession = Depends(get_db),
) -> Doctor:
return await DoctorService(db).review(doctor_id, False, payload.reason, current_user, client_ip(request))