Files
hms/crates/erp-health/src/handler/follow_up_template_handler.rs
iven d70b027f20 fix(health): 全 handler page_size 上限 100 防止 DoS
22 个 handler 文件统一添加 .min(100) 限制分页大小
2026-05-21 22:38:29 +08:00

133 lines
3.8 KiB
Rust

use axum::Extension;
use axum::extract::{FromRef, Json, Path, Query, State};
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::DeleteWithVersion;
use crate::dto::follow_up_template_dto::*;
use crate::service::follow_up_template_service;
use crate::state::HealthState;
#[derive(Debug, Deserialize, IntoParams)]
pub struct FollowUpTemplateListParams {
pub page: Option<u64>,
pub page_size: Option<u64>,
pub follow_up_type: Option<String>,
pub status: Option<String>,
}
#[derive(Debug, serde::Deserialize, utoipa::ToSchema)]
pub struct UpdateFollowUpTemplateWithVersion {
#[serde(flatten)]
pub data: UpdateFollowUpTemplateReq,
pub version: i32,
}
pub async fn list_templates<S>(
State(state): State<HealthState>,
Extension(ctx): Extension<TenantContext>,
Query(params): Query<FollowUpTemplateListParams>,
) -> Result<Json<ApiResponse<PaginatedResponse<FollowUpTemplateListItemResp>>>, AppError>
where
HealthState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "health.follow-up-templates.list")?;
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(20).min(100);
let result = follow_up_template_service::list_templates(
&state,
ctx.tenant_id,
page,
page_size,
params.follow_up_type,
params.status,
)
.await?;
Ok(Json(ApiResponse::ok(result)))
}
pub async fn get_template<S>(
State(state): State<HealthState>,
Extension(ctx): Extension<TenantContext>,
Path(id): Path<Uuid>,
) -> Result<Json<ApiResponse<FollowUpTemplateResp>>, AppError>
where
HealthState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "health.follow-up-templates.list")?;
let result = follow_up_template_service::get_template(&state, ctx.tenant_id, id).await?;
Ok(Json(ApiResponse::ok(result)))
}
pub async fn create_template<S>(
State(state): State<HealthState>,
Extension(ctx): Extension<TenantContext>,
Json(req): Json<CreateFollowUpTemplateReq>,
) -> Result<Json<ApiResponse<FollowUpTemplateResp>>, AppError>
where
HealthState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "health.follow-up-templates.manage")?;
let mut req = req;
req.sanitize();
let result =
follow_up_template_service::create_template(&state, ctx.tenant_id, Some(ctx.user_id), req)
.await?;
Ok(Json(ApiResponse::ok(result)))
}
pub async fn update_template<S>(
State(state): State<HealthState>,
Extension(ctx): Extension<TenantContext>,
Path(id): Path<Uuid>,
Json(req): Json<UpdateFollowUpTemplateWithVersion>,
) -> Result<Json<ApiResponse<FollowUpTemplateResp>>, AppError>
where
HealthState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "health.follow-up-templates.manage")?;
let mut data = req.data;
data.sanitize();
let result = follow_up_template_service::update_template(
&state,
ctx.tenant_id,
id,
Some(ctx.user_id),
data,
req.version,
)
.await?;
Ok(Json(ApiResponse::ok(result)))
}
pub async fn delete_template<S>(
State(state): State<HealthState>,
Extension(ctx): Extension<TenantContext>,
Path(id): Path<Uuid>,
Json(req): Json<DeleteWithVersion>,
) -> Result<Json<ApiResponse<()>>, AppError>
where
HealthState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "health.follow-up-templates.manage")?;
follow_up_template_service::delete_template(
&state,
ctx.tenant_id,
id,
Some(ctx.user_id),
req.version,
)
.await?;
Ok(Json(ApiResponse::ok(())))
}