细节优化——1
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
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, require_study_roles
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import attachment as attachment_crud
|
||||
from app.crud import milestone as milestone_crud
|
||||
from app.crud import study as study_crud
|
||||
from app.crud import user as user_crud
|
||||
@@ -36,6 +38,8 @@ async def create_milestone(
|
||||
current_user=Depends(get_current_user),
|
||||
) -> MilestoneRead:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
if milestone_in.planned_date and milestone_in.planned_date < date.today():
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="计划日期不得早于今天")
|
||||
milestone = await milestone_crud.create(db, study_id, milestone_in)
|
||||
owner = None
|
||||
site = None
|
||||
@@ -127,6 +131,15 @@ async def update_milestone(
|
||||
milestone = await milestone_crud.get(db, milestone_id)
|
||||
if not milestone or milestone.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Milestone not found")
|
||||
if milestone_in.planned_date and milestone_in.planned_date < date.today():
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="计划日期不得早于今天")
|
||||
if milestone_in.actual_date and milestone_in.actual_date < date.today():
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="实际日期不得早于今天")
|
||||
if milestone_in.actual_date:
|
||||
attachments = await attachment_crud.list_attachments(db, study_id, "ethics_node", milestone_id)
|
||||
if not attachments:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="请先上传伦理批件后再更新实际日期")
|
||||
milestone_in.status = "DONE"
|
||||
old_status = milestone.status
|
||||
updated = await milestone_crud.update(db, milestone, milestone_in)
|
||||
owner = None
|
||||
@@ -164,3 +177,32 @@ async def update_milestone(
|
||||
created_at=updated.created_at,
|
||||
updated_at=updated.updated_at,
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/{milestone_id}",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
dependencies=[Depends(require_study_member())],
|
||||
)
|
||||
async def delete_milestone(
|
||||
study_id: uuid.UUID,
|
||||
milestone_id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
current_user=Depends(get_current_user),
|
||||
) -> None:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
milestone = await milestone_crud.get(db, milestone_id)
|
||||
if not milestone or milestone.study_id != study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Milestone not found")
|
||||
await milestone_crud.delete_milestone(db, milestone)
|
||||
await audit_crud.log_action(
|
||||
db,
|
||||
study_id=study_id,
|
||||
entity_type="milestone",
|
||||
entity_id=milestone_id,
|
||||
action="DELETE_MILESTONE",
|
||||
detail=f"Milestone {milestone_id} deleted",
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user