alert_handler 的 AlertListQuery 新增 doctor_id 参数。 alert_service::list_alerts 先查询 patient_doctor_relation 获取该医生负责的患者列表,再用 patient_id.is_in() 过滤。 医生无管床患者时直接返回空结果。新增 2 个单元测试。
102 lines
3.0 KiB
Rust
102 lines
3.0 KiB
Rust
use axum::extract::{FromRef, Path, Query, State};
|
|
use axum::response::IntoResponse;
|
|
use axum::Extension;
|
|
use serde::Deserialize;
|
|
use utoipa::IntoParams;
|
|
use uuid::Uuid;
|
|
|
|
use erp_core::error::AppError;
|
|
use erp_core::rbac::require_permission;
|
|
use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
|
|
|
use crate::dto::alert_dto::AcknowledgeAlertRequest;
|
|
use crate::service::alert_service;
|
|
use crate::state::HealthState;
|
|
|
|
#[derive(Debug, Deserialize, IntoParams)]
|
|
pub struct AlertListQuery {
|
|
pub patient_id: Option<Uuid>,
|
|
pub doctor_id: Option<Uuid>,
|
|
pub status: Option<String>,
|
|
pub page: Option<u64>,
|
|
pub page_size: Option<u64>,
|
|
}
|
|
|
|
pub async fn list_alerts<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Query(query): Query<AlertListQuery>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.alerts.list")?;
|
|
let page = query.page.unwrap_or(1);
|
|
let page_size = query.page_size.unwrap_or(20);
|
|
|
|
let (items, total) = alert_service::list_alerts(
|
|
&state, ctx.tenant_id, query.patient_id, query.doctor_id, query.status.as_deref(),
|
|
page, page_size,
|
|
).await?;
|
|
|
|
Ok(axum::Json(ApiResponse::ok(PaginatedResponse {
|
|
data: items,
|
|
total,
|
|
page,
|
|
page_size,
|
|
total_pages: total.div_ceil(page_size.max(1)),
|
|
})))
|
|
}
|
|
|
|
pub async fn acknowledge<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
axum::Json(body): axum::Json<AcknowledgeAlertRequest>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.alerts.manage")?;
|
|
let alert = alert_service::acknowledge_alert(
|
|
&state, ctx.tenant_id, id, ctx.user_id, body.version,
|
|
).await?;
|
|
Ok(axum::Json(ApiResponse::ok(alert)))
|
|
}
|
|
|
|
pub async fn dismiss<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
axum::Json(body): axum::Json<AcknowledgeAlertRequest>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.alerts.manage")?;
|
|
let alert = alert_service::dismiss_alert(
|
|
&state, ctx.tenant_id, id, ctx.user_id, body.version,
|
|
).await?;
|
|
Ok(axum::Json(ApiResponse::ok(alert)))
|
|
}
|
|
|
|
pub async fn resolve<S>(
|
|
State(state): State<HealthState>,
|
|
Extension(ctx): Extension<TenantContext>,
|
|
Path(id): Path<Uuid>,
|
|
axum::Json(body): axum::Json<AcknowledgeAlertRequest>,
|
|
) -> Result<impl IntoResponse, AppError>
|
|
where
|
|
HealthState: FromRef<S>,
|
|
S: Clone + Send + Sync + 'static,
|
|
{
|
|
require_permission(&ctx, "health.alerts.manage")?;
|
|
let alert = alert_service::resolve_alert(
|
|
&state, ctx.tenant_id, id, body.version,
|
|
).await?;
|
|
Ok(axum::Json(ApiResponse::ok(alert)))
|
|
}
|