4ea0e88c98
- 新增中心联系人展示名解析服务,支持从逗号分隔用户 ID 解析姓名\n- 中心列表接口返回 contact_display,避免前端依赖项目成员权限才能展示联系人姓名\n- 中心管理和立项会议授权页面优先使用后端联系人展示名\n- 补充联系人解析服务测试和立项会议授权页面展示顺序测试
214 lines
7.0 KiB
Python
214 lines
7.0 KiB
Python
import uuid
|
|
import json
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import get_operator_role_label, get_cra_site_scope, get_current_user, get_db_session, require_study_member, require_api_permission, require_study_not_locked
|
|
from app.core.decorators import register_api_endpoint
|
|
from app.crud import audit as audit_crud
|
|
from app.crud import site as site_crud
|
|
from app.crud import startup as startup_crud
|
|
from app.crud import study as study_crud
|
|
from app.crud import user as user_crud
|
|
from app.schemas.site import SiteCreate, SiteRead, SiteUpdate
|
|
from app.schemas.startup import StartupEthicsCreate, StartupFeasibilityCreate
|
|
from app.services.site_contact_display import build_contact_display, parse_contact_user_ids
|
|
|
|
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="项目不存在")
|
|
return study
|
|
|
|
|
|
@router.post(
|
|
"/",
|
|
response_model=SiteRead,
|
|
status_code=status.HTTP_201_CREATED,
|
|
dependencies=[Depends(require_api_permission("sites:create")), Depends(require_study_not_locked())],
|
|
)
|
|
@register_api_endpoint(
|
|
endpoint_key="sites:create",
|
|
module="sites",
|
|
action="write",
|
|
description="创建中心",
|
|
default_roles=["PM"],
|
|
)
|
|
async def create_site(
|
|
study_id: uuid.UUID,
|
|
site_in: SiteCreate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> SiteRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
site = await site_crud.create_site(db, study_id, site_in)
|
|
await startup_crud.create_feasibility(
|
|
db,
|
|
study_id,
|
|
StartupFeasibilityCreate(site_id=site.id),
|
|
created_by=getattr(current_user, "id", None),
|
|
)
|
|
await startup_crud.create_ethics(
|
|
db,
|
|
study_id,
|
|
StartupEthicsCreate(site_id=site.id),
|
|
created_by=getattr(current_user, "id", None),
|
|
)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="site",
|
|
entity_id=site.id,
|
|
action="SITE_CREATED",
|
|
detail=json.dumps({"targetName": site.name, "after": {"name": site.name, "is_active": site.is_active}}, ensure_ascii=False),
|
|
operator_id=current_user.id,
|
|
operator_role=await get_operator_role_label(db, study_id, current_user),
|
|
)
|
|
return site
|
|
|
|
|
|
@router.get(
|
|
"/",
|
|
response_model=list[SiteRead],
|
|
dependencies=[Depends(require_api_permission("sites:read")), Depends(require_study_member())],
|
|
)
|
|
@register_api_endpoint(
|
|
endpoint_key="sites:read",
|
|
module="sites",
|
|
action="read",
|
|
description="查询中心列表",
|
|
default_roles=["PM", "CRA", "PV", "QA", "CTA"],
|
|
)
|
|
async def list_sites(
|
|
study_id: uuid.UUID,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
include_inactive: bool = False,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> list[SiteRead]:
|
|
await _ensure_study_exists(db, study_id)
|
|
cra_scope = await get_cra_site_scope(db, study_id, current_user)
|
|
site_ids = cra_scope[0] if cra_scope else None
|
|
sites = await site_crud.list_by_study(
|
|
db,
|
|
study_id,
|
|
skip=skip,
|
|
limit=limit,
|
|
include_inactive=include_inactive,
|
|
site_ids=site_ids,
|
|
)
|
|
contact_user_ids: set[uuid.UUID] = set()
|
|
for site in sites:
|
|
contact_user_ids.update(parse_contact_user_ids(site.contact))
|
|
users_map = await user_crud.get_users_by_ids(db, contact_user_ids)
|
|
|
|
result: list[SiteRead] = []
|
|
for site in sites:
|
|
item = SiteRead.model_validate(site)
|
|
item.contact_display = build_contact_display(site.contact, users_map)
|
|
result.append(item)
|
|
return result
|
|
|
|
|
|
@router.patch(
|
|
"/{site_id}",
|
|
response_model=SiteRead,
|
|
dependencies=[Depends(require_api_permission("sites:update")), Depends(require_study_not_locked())],
|
|
)
|
|
@register_api_endpoint(
|
|
endpoint_key="sites:update",
|
|
module="sites",
|
|
action="write",
|
|
description="更新中心",
|
|
default_roles=["PM"],
|
|
)
|
|
async def update_site(
|
|
study_id: uuid.UUID,
|
|
site_id: uuid.UUID,
|
|
site_in: SiteUpdate,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> SiteRead:
|
|
await _ensure_study_exists(db, study_id)
|
|
site = await site_crud.get_site(db, site_id)
|
|
if not site or site.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="分中心不存在")
|
|
before_data = {
|
|
"name": site.name,
|
|
"city": site.city,
|
|
"pi_name": site.pi_name,
|
|
"phone": site.phone,
|
|
"contact": site.contact,
|
|
"is_active": site.is_active,
|
|
}
|
|
updated = await site_crud.update_site(db, site, site_in)
|
|
after_data = {
|
|
"name": updated.name,
|
|
"city": updated.city,
|
|
"pi_name": updated.pi_name,
|
|
"phone": updated.phone,
|
|
"contact": updated.contact,
|
|
"is_active": updated.is_active,
|
|
}
|
|
action = "SITE_STATUS_CHANGED" if before_data.get("is_active") != after_data.get("is_active") else "SITE_UPDATED"
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="site",
|
|
entity_id=updated.id,
|
|
action=action,
|
|
detail=json.dumps({"targetName": updated.name, "before": before_data, "after": after_data}, ensure_ascii=False),
|
|
operator_id=current_user.id,
|
|
operator_role=await get_operator_role_label(db, study_id, current_user),
|
|
)
|
|
return updated
|
|
|
|
|
|
@router.delete(
|
|
"/{site_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
dependencies=[Depends(require_api_permission("sites:delete")), Depends(require_study_not_locked())],
|
|
)
|
|
@register_api_endpoint(
|
|
endpoint_key="sites:delete",
|
|
module="sites",
|
|
action="write",
|
|
description="删除中心",
|
|
default_roles=["PM"],
|
|
)
|
|
async def delete_site(
|
|
study_id: uuid.UUID,
|
|
site_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db_session),
|
|
current_user=Depends(get_current_user),
|
|
) -> None:
|
|
await _ensure_study_exists(db, study_id)
|
|
site = await site_crud.get_site(db, site_id)
|
|
if not site or site.study_id != study_id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="分中心不存在")
|
|
before_data = {
|
|
"name": site.name,
|
|
"city": site.city,
|
|
"pi_name": site.pi_name,
|
|
"phone": site.phone,
|
|
"contact": site.contact,
|
|
"is_active": site.is_active,
|
|
}
|
|
await site_crud.delete_site_and_related(db, site)
|
|
await audit_crud.log_action(
|
|
db,
|
|
study_id=study_id,
|
|
entity_type="site",
|
|
entity_id=site_id,
|
|
action="SITE_DELETED",
|
|
detail=json.dumps({"targetName": site.name, "before": before_data, "after": {"deleted": True}}, ensure_ascii=False),
|
|
operator_id=current_user.id,
|
|
operator_role=await get_operator_role_label(db, study_id, current_user),
|
|
)
|
|
return None
|