Step 8:数据管理(Queries + SDV/SDR 进度)
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.deps import get_current_user, get_db_session, require_study_member, require_study_roles
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import verification as verification_crud
|
||||
from app.crud import study as study_crud
|
||||
from app.schemas.verification import VerificationCreate, VerificationRead, VerificationUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
|
||||
study = await study_crud.get(db, study_id)
|
||||
if not study:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Study not found")
|
||||
return study
|
||||
|
||||
|
||||
@router.put(
|
||||
"/",
|
||||
response_model=VerificationRead,
|
||||
status_code=status.HTTP_200_OK,
|
||||
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
|
||||
)
|
||||
async def upsert_verification(
|
||||
study_id: uuid.UUID,
|
||||
payload: VerificationCreate,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> VerificationRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
try:
|
||||
vp = await verification_crud.upsert_progress(db, study_id, payload)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
await audit_crud.log_action(
|
||||
db,
|
||||
study_id=study_id,
|
||||
entity_type="verification",
|
||||
entity_id=vp.id,
|
||||
action="UPSERT_VERIFICATION",
|
||||
detail=f"{payload.level} progress subject {payload.subject_id} upserted",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
return VerificationRead.model_validate(vp)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/",
|
||||
response_model=list[VerificationRead],
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def list_verifications(
|
||||
study_id: uuid.UUID,
|
||||
site_id: uuid.UUID | None = None,
|
||||
subject_id: uuid.UUID | None = None,
|
||||
level: str | None = None,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
) -> list[VerificationRead]:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
items = await verification_crud.list_progress(db, study_id, site_id=site_id, subject_id=subject_id, level=level)
|
||||
return [VerificationRead.model_validate(i) for i in items]
|
||||
|
||||
|
||||
@router.get(
|
||||
"/subjects/{subject_id}/{level}",
|
||||
response_model=VerificationRead,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def get_verification(
|
||||
study_id: uuid.UUID,
|
||||
subject_id: uuid.UUID,
|
||||
level: str,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
) -> VerificationRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
vp = await verification_crud.get_progress(db, study_id, subject_id, level)
|
||||
if not vp:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Verification not found")
|
||||
return VerificationRead.model_validate(vp)
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/{verification_id}",
|
||||
response_model=VerificationRead,
|
||||
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
|
||||
)
|
||||
async def update_verification(
|
||||
study_id: uuid.UUID,
|
||||
verification_id: uuid.UUID,
|
||||
payload: VerificationUpdate,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> VerificationRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
vp = await verification_crud.get_by_id(db, verification_id)
|
||||
if not vp or vp.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Verification not found")
|
||||
|
||||
updated = await verification_crud.update_progress(db, vp, payload)
|
||||
await audit_crud.log_action(
|
||||
db,
|
||||
study_id=study_id,
|
||||
entity_type="verification",
|
||||
entity_id=verification_id,
|
||||
action="UPSERT_VERIFICATION",
|
||||
detail=f"{updated.level} progress subject {updated.subject_id} updated",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
return VerificationRead.model_validate(updated)
|
||||
Reference in New Issue
Block a user