Step 7:AE + 风险/问题管理(PV)
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
import uuid
|
||||
from datetime import date
|
||||
|
||||
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
|
||||
from app.crud import ae as ae_crud
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import member as member_crud
|
||||
from app.crud import study as study_crud
|
||||
from app.schemas.ae import AECreate, AERead, AEUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
ALLOWED_CREATE_ROLES = {"PM", "CRA", "PV"}
|
||||
ALLOWED_UPDATE_ROLES = {"PM", "PV"}
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
async def _get_member_role(db: AsyncSession, study_id: uuid.UUID, user_id: uuid.UUID) -> str | None:
|
||||
member = await member_crud.get_member(db, study_id, user_id)
|
||||
return member.role_in_study if member else None
|
||||
|
||||
|
||||
def _is_overdue(ae: AERead) -> bool:
|
||||
return bool(ae.report_due_date and date.today() > ae.report_due_date and ae.status != "CLOSED")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/",
|
||||
response_model=AERead,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def create_ae(
|
||||
study_id: uuid.UUID,
|
||||
ae_in: AECreate,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> AERead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
member_role = await _get_member_role(db, study_id, current_user.id)
|
||||
if current_user.role != "ADMIN" and member_role not in ALLOWED_CREATE_ROLES:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions")
|
||||
try:
|
||||
ae = await ae_crud.create_ae(db, study_id, ae_in, created_by=current_user.id)
|
||||
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="ae",
|
||||
entity_id=ae.id,
|
||||
action="CREATE_AE",
|
||||
detail=f"AE {ae.id} created",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
data = AERead.model_validate(ae)
|
||||
data.is_overdue = _is_overdue(data)
|
||||
return data
|
||||
|
||||
|
||||
@router.get(
|
||||
"/",
|
||||
response_model=list[AERead],
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def list_ae(
|
||||
study_id: uuid.UUID,
|
||||
status_filter: str | None = None,
|
||||
seriousness: str | None = None,
|
||||
site_id: uuid.UUID | None = None,
|
||||
subject_id: uuid.UUID | None = None,
|
||||
overdue: bool | None = None,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
) -> list[AERead]:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
aes = await ae_crud.list_ae(db, study_id, status=status_filter, seriousness=seriousness, site_id=site_id, subject_id=subject_id, overdue=overdue)
|
||||
result: list[AERead] = []
|
||||
for item in aes:
|
||||
obj = AERead.model_validate(item)
|
||||
obj.is_overdue = _is_overdue(obj)
|
||||
result.append(obj)
|
||||
return result
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{ae_id}",
|
||||
response_model=AERead,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def get_ae(
|
||||
study_id: uuid.UUID,
|
||||
ae_id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
) -> AERead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
ae = await ae_crud.get_ae(db, ae_id)
|
||||
if not ae or ae.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="AE not found")
|
||||
data = AERead.model_validate(ae)
|
||||
data.is_overdue = _is_overdue(data)
|
||||
return data
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/{ae_id}",
|
||||
response_model=AERead,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def update_ae(
|
||||
study_id: uuid.UUID,
|
||||
ae_id: uuid.UUID,
|
||||
ae_in: AEUpdate,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> AERead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
ae = await ae_crud.get_ae(db, ae_id)
|
||||
if not ae or ae.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="AE not found")
|
||||
|
||||
member_role = await _get_member_role(db, study_id, current_user.id)
|
||||
if current_user.role == "ADMIN":
|
||||
pass
|
||||
elif member_role in ALLOWED_UPDATE_ROLES:
|
||||
pass
|
||||
elif member_role == "CRA":
|
||||
if ae.created_by != current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Only creator can update AE")
|
||||
if ae_in.status == "CLOSED":
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="CRA cannot close AE")
|
||||
else:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions")
|
||||
|
||||
old_status = ae.status
|
||||
updated = await ae_crud.update_ae(db, ae, ae_in)
|
||||
|
||||
detail = None
|
||||
action = "UPDATE_AE"
|
||||
if ae_in.status and ae_in.status != old_status:
|
||||
detail = f"AE {ae_id} status {old_status} -> {ae_in.status}"
|
||||
action = "AE_STATUS_CHANGE"
|
||||
await audit_crud.log_action(
|
||||
db,
|
||||
study_id=study_id,
|
||||
entity_type="ae",
|
||||
entity_id=ae_id,
|
||||
action=action,
|
||||
detail=detail or "AE updated",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
data = AERead.model_validate(updated)
|
||||
data.is_overdue = _is_overdue(data)
|
||||
return data
|
||||
@@ -0,0 +1,156 @@
|
||||
import uuid
|
||||
from datetime import date
|
||||
|
||||
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
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import issue as issue_crud
|
||||
from app.crud import member as member_crud
|
||||
from app.crud import study as study_crud
|
||||
from app.schemas.issue import IssueCreate, IssueRead, IssueUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
ALLOWED_EDIT_ROLES = {"PM", "PV"}
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
async def _get_member_role(db: AsyncSession, study_id: uuid.UUID, user_id: uuid.UUID) -> str | None:
|
||||
member = await member_crud.get_member(db, study_id, user_id)
|
||||
return member.role_in_study if member else None
|
||||
|
||||
|
||||
def _is_overdue(issue: IssueRead) -> bool:
|
||||
return bool(issue.due_date and date.today() > issue.due_date and issue.status != "CLOSED")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/",
|
||||
response_model=IssueRead,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def create_issue(
|
||||
study_id: uuid.UUID,
|
||||
issue_in: IssueCreate,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> IssueRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
member_role = await _get_member_role(db, study_id, current_user.id)
|
||||
if current_user.role != "ADMIN" and member_role not in ALLOWED_EDIT_ROLES:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions")
|
||||
try:
|
||||
issue = await issue_crud.create_issue(db, study_id, issue_in, created_by=current_user.id)
|
||||
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="issue",
|
||||
entity_id=issue.id,
|
||||
action="CREATE_ISSUE",
|
||||
detail=f"Issue {issue.id} created",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
data = IssueRead.model_validate(issue)
|
||||
data.is_overdue = _is_overdue(data)
|
||||
return data
|
||||
|
||||
|
||||
@router.get(
|
||||
"/",
|
||||
response_model=list[IssueRead],
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def list_issues(
|
||||
study_id: uuid.UUID,
|
||||
status_filter: str | None = None,
|
||||
level: str | None = None,
|
||||
category: str | None = None,
|
||||
overdue: bool | None = None,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
) -> list[IssueRead]:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
issues = await issue_crud.list_issues(db, study_id, status=status_filter, level=level, category=category, overdue=overdue)
|
||||
result: list[IssueRead] = []
|
||||
for item in issues:
|
||||
obj = IssueRead.model_validate(item)
|
||||
obj.is_overdue = _is_overdue(obj)
|
||||
result.append(obj)
|
||||
return result
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{issue_id}",
|
||||
response_model=IssueRead,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def get_issue(
|
||||
study_id: uuid.UUID,
|
||||
issue_id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
) -> IssueRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
issue = await issue_crud.get_issue(db, issue_id)
|
||||
if not issue or issue.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Issue not found")
|
||||
data = IssueRead.model_validate(issue)
|
||||
data.is_overdue = _is_overdue(data)
|
||||
return data
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/{issue_id}",
|
||||
response_model=IssueRead,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def update_issue(
|
||||
study_id: uuid.UUID,
|
||||
issue_id: uuid.UUID,
|
||||
issue_in: IssueUpdate,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> IssueRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
issue = await issue_crud.get_issue(db, issue_id)
|
||||
if not issue or issue.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Issue not found")
|
||||
|
||||
member_role = await _get_member_role(db, study_id, current_user.id)
|
||||
if current_user.role != "ADMIN" and member_role not in ALLOWED_EDIT_ROLES:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions")
|
||||
|
||||
old_status = issue.status
|
||||
old_level = issue.level
|
||||
updated = await issue_crud.update_issue(db, issue, issue_in)
|
||||
|
||||
detail = None
|
||||
action = "UPDATE_ISSUE"
|
||||
if issue_in.status and issue_in.status != old_status:
|
||||
detail = f"Issue {issue_id} status {old_status} -> {issue_in.status}"
|
||||
action = "ISSUE_STATUS_CHANGE"
|
||||
elif issue_in.level and issue_in.level != old_level:
|
||||
detail = f"Issue {issue_id} level {old_level} -> {issue_in.level}"
|
||||
await audit_crud.log_action(
|
||||
db,
|
||||
study_id=study_id,
|
||||
entity_type="issue",
|
||||
entity_id=issue_id,
|
||||
action=action,
|
||||
detail=detail or "Issue updated",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
data = IssueRead.model_validate(updated)
|
||||
data.is_overdue = _is_overdue(data)
|
||||
return data
|
||||
@@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1 import auth, users, studies, sites, members, comments, attachments, audit_logs, milestones, tasks, dashboard, subjects, visits
|
||||
from app.api.v1 import auth, users, studies, sites, members, comments, attachments, audit_logs, milestones, tasks, dashboard, subjects, visits, aes, issues
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
||||
@@ -16,3 +16,5 @@ api_router.include_router(tasks.router, prefix="/studies/{study_id}/tasks", tags
|
||||
api_router.include_router(dashboard.router, prefix="/studies/{study_id}/dashboard", tags=["dashboard"])
|
||||
api_router.include_router(subjects.router, prefix="/studies/{study_id}/subjects", tags=["subjects"])
|
||||
api_router.include_router(visits.router, prefix="/studies/{study_id}/subjects/{subject_id}/visits", tags=["visits"])
|
||||
api_router.include_router(aes.router, prefix="/studies/{study_id}/aes", tags=["aes"])
|
||||
api_router.include_router(issues.router, prefix="/studies/{study_id}/issues", tags=["issues"])
|
||||
|
||||
Reference in New Issue
Block a user