修复前后端显示异常问题(受试者、中心、负责人、日期等)
This commit is contained in:
@@ -8,7 +8,9 @@ from app.crud import audit as audit_crud
|
||||
from app.crud import milestone as milestone_crud
|
||||
from app.crud import study as study_crud
|
||||
from app.crud import task as task_crud
|
||||
from app.crud import user as user_crud
|
||||
from app.schemas.task import TaskCreate, TaskRead, TaskUpdate
|
||||
from app.schemas.user import UserDisplay
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -47,6 +49,9 @@ async def create_task(
|
||||
await _ensure_study_exists(db, study_id)
|
||||
await _validate_milestone(db, study_id, task_in.milestone_id)
|
||||
task = await task_crud.create(db, study_id, task_in, created_by=current_user.id)
|
||||
assignee = None
|
||||
if task.assignee_id:
|
||||
assignee = await user_crud.get_by_id(db, task.assignee_id)
|
||||
await audit_crud.log_action(
|
||||
db,
|
||||
study_id=study_id,
|
||||
@@ -57,7 +62,7 @@ async def create_task(
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
return task
|
||||
return _to_task_read(task, assignee)
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -74,7 +79,13 @@ async def list_tasks(
|
||||
) -> list[TaskRead]:
|
||||
await _ensure_study_exists(db, study_id)
|
||||
tasks = await task_crud.list_tasks(db, study_id, milestone_id=milestone_id, assignee_id=assignee_id, status=status)
|
||||
return list(tasks)
|
||||
assignee_ids = {t.assignee_id for t in tasks if t.assignee_id}
|
||||
users_map = await user_crud.get_users_by_ids(db, assignee_ids)
|
||||
result: list[TaskRead] = []
|
||||
for t in tasks:
|
||||
user = users_map.get(t.assignee_id)
|
||||
result.append(_to_task_read(t, user))
|
||||
return result
|
||||
|
||||
|
||||
@router.patch(
|
||||
@@ -98,6 +109,9 @@ async def update_task(
|
||||
|
||||
old_status = task.status
|
||||
updated = await task_crud.update(db, task, task_in)
|
||||
assignee = None
|
||||
if updated.assignee_id:
|
||||
assignee = await user_crud.get_by_id(db, updated.assignee_id)
|
||||
detail = None
|
||||
if task_in.status and task_in.status != old_status:
|
||||
detail = f"task {task_id} status {old_status} -> {task_in.status}"
|
||||
@@ -111,4 +125,23 @@ async def update_task(
|
||||
operator_id=current_user.id,
|
||||
operator_role=current_user.role,
|
||||
)
|
||||
return updated
|
||||
return _to_task_read(updated, assignee)
|
||||
|
||||
|
||||
def _to_task_read(task, assignee) -> TaskRead:
|
||||
return TaskRead(
|
||||
id=task.id,
|
||||
study_id=task.study_id,
|
||||
milestone_id=task.milestone_id,
|
||||
title=task.title,
|
||||
description=task.description,
|
||||
assignee_id=task.assignee_id,
|
||||
assignee=UserDisplay.model_validate(assignee) if assignee else None,
|
||||
priority=task.priority,
|
||||
due_date=task.due_date,
|
||||
status=task.status,
|
||||
completed_at=task.completed_at,
|
||||
created_by=task.created_by,
|
||||
created_at=task.created_at,
|
||||
updated_at=task.updated_at,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user