未知(继上次中断)

This commit is contained in:
Cheng Zhou
2026-02-04 10:52:34 +08:00
parent 8e258d21a7
commit 737f84bf54
99 changed files with 5497 additions and 2143 deletions
+48 -4
View File
@@ -1,13 +1,15 @@
import uuid
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_current_user, get_db_session, require_study_member, require_study_roles, require_study_not_locked
from app.core.deps import get_cra_site_scope, get_current_user, get_db_session, require_study_member, require_study_roles, require_study_not_locked
from app.crud import audit as audit_crud
from app.crud import site as site_crud
from app.crud import subject as subject_crud
from app.crud import study as study_crud
from app.models.ae import AdverseEvent
from app.schemas.subject import SubjectCreate, SubjectRead, SubjectUpdate
router = APIRouter()
@@ -41,6 +43,9 @@ async def create_subject(
current_user=Depends(get_current_user),
) -> SubjectRead:
await _ensure_study_exists(db, study_id)
cra_scope = await get_cra_site_scope(db, study_id, current_user)
if cra_scope and subject_in.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
try:
subject = await subject_crud.create_subject(db, study_id, subject_in)
except ValueError as exc:
@@ -67,10 +72,31 @@ async def list_subjects(
study_id: uuid.UUID,
site_id: uuid.UUID | None = None,
db: AsyncSession = Depends(get_db_session),
current_user=Depends(get_current_user),
) -> list[SubjectRead]:
await _ensure_study_exists(db, study_id)
subjects = await subject_crud.list_subjects(db, study_id, site_id=site_id)
return list(subjects)
cra_scope = await get_cra_site_scope(db, study_id, current_user)
site_ids = cra_scope[0] if cra_scope else None
if site_id and site_ids is not None and site_id not in site_ids:
return []
subjects = await subject_crud.list_subjects(db, study_id, site_id=site_id, site_ids=site_ids)
subject_list = list(subjects)
if not subject_list:
return []
subject_ids = [subject.id for subject in subject_list]
ae_rows = await db.execute(
select(AdverseEvent.subject_id).where(
AdverseEvent.study_id == study_id,
AdverseEvent.subject_id.in_(subject_ids),
)
)
ae_subject_ids = {row[0] for row in ae_rows.all() if row[0]}
result: list[SubjectRead] = []
for subject in subject_list:
data = SubjectRead.model_validate(subject)
data.has_ae = subject.id in ae_subject_ids
result.append(data)
return result
@router.get(
@@ -82,13 +108,25 @@ async def get_subject(
study_id: uuid.UUID,
subject_id: uuid.UUID,
db: AsyncSession = Depends(get_db_session),
current_user=Depends(get_current_user),
) -> SubjectRead:
await _ensure_study_exists(db, study_id)
subject = await subject_crud.get_subject(db, subject_id)
if not subject or subject.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
cra_scope = await get_cra_site_scope(db, study_id, current_user)
if cra_scope and subject.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await _ensure_subject_active(db, subject)
return subject
data = SubjectRead.model_validate(subject)
ae_row = await db.execute(
select(AdverseEvent.id).where(
AdverseEvent.study_id == study_id,
AdverseEvent.subject_id == subject.id,
).limit(1)
)
data.has_ae = ae_row.scalar_one_or_none() is not None
return data
@router.patch(
@@ -107,6 +145,9 @@ async def update_subject(
subject = await subject_crud.get_subject(db, subject_id)
if not subject or subject.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
cra_scope = await get_cra_site_scope(db, study_id, current_user)
if cra_scope and subject.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await _ensure_subject_active(db, subject)
old_status = subject.status
updated = await subject_crud.update_subject(db, subject, subject_in)
@@ -144,6 +185,9 @@ async def delete_subject(
subject = await subject_crud.get_subject(db, subject_id)
if not subject or subject.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="参与者不存在")
cra_scope = await get_cra_site_scope(db, study_id, current_user)
if cra_scope and subject.site_id not in cra_scope[0]:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="权限不足")
await subject_crud.delete_subject(db, subject)
await audit_crud.log_action(
db,