fix(health): 修复审计发现的 10 个 CRITICAL 问题
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

权限与安全:
- 为全部 51 个 handler 端点添加 require_permission 权限检查
- 修复 CAS 预约操作中 doctor_id 为 None 时使用 Uuid::nil() 的问题

状态机修复:
- 预约初始状态从 "scheduled" 改为 "pending"(匹配设计规格)
- 排班状态从 "active" 改为 "enabled"
- 咨询会话添加 waiting→active 自动触发(首条消息时)
- 新增 create_session 端点和 DTO

数据完整性:
- doctor_profile 表添加 name 列(entity + migration + service)
- lab_report/health_trend 的 json 列改为 json_binary(支持 GIN 索引)
- 添加关键索引:patient.id_number UNIQUE、patient_tag UNIQUE、
  doctor_schedule 唯一排班槽位、health_trend、doctor_profile.name
- 随访记录完成后自动检查 next_follow_up_date 创建后续任务

事件总线:
- 实现 10 种核心事件发布(patient/appointment/follow_up/consultation/lab_report)
- 实现 workflow.task.completed 和 message.sent 事件订阅框架

种子数据:
- 实现 seed_tenant_health(8 个默认患者标签)
- 实现 soft_delete_tenant_data(16 张表级联软删除)
This commit is contained in:
iven
2026-04-23 23:25:53 +08:00
parent d6678d001e
commit 2e9eb55f2c
18 changed files with 423 additions and 31 deletions

View File

@@ -93,6 +93,7 @@ impl MigrationTrait for Migration {
.table(Patient::Table)
.col(Patient::TenantId)
.col(Patient::IdNumber)
.unique()
.to_owned(),
)
.await?;
@@ -266,6 +267,20 @@ impl MigrationTrait for Migration {
)
.await?;
// patient_tag 唯一名称索引
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_patient_tag_tenant_name_unique")
.table(PatientTag::Table)
.col(PatientTag::TenantId)
.col(PatientTag::Name)
.unique()
.to_owned(),
)
.await?;
// 5. doctor_profile — 医护档案
manager
.create_table(
@@ -275,6 +290,7 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(DoctorProfile::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(DoctorProfile::TenantId).uuid().not_null())
.col(ColumnDef::new(DoctorProfile::UserId).uuid().null())
.col(ColumnDef::new(DoctorProfile::Name).string_len(100).not_null())
.col(ColumnDef::new(DoctorProfile::Department).string_len(100).null())
.col(ColumnDef::new(DoctorProfile::Title).string_len(50).null())
.col(ColumnDef::new(DoctorProfile::Specialty).string_len(200).null())
@@ -311,6 +327,19 @@ impl MigrationTrait for Migration {
)
.await?;
// doctor_profile 名称搜索索引
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_doctor_profile_tenant_name")
.table(DoctorProfile::Table)
.col(DoctorProfile::TenantId)
.col(DoctorProfile::Name)
.to_owned(),
)
.await?;
// 6. patient_doctor_relation — 医患关系
manager
.create_table(
@@ -529,8 +558,8 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(LabReport::PatientId).uuid().not_null())
.col(ColumnDef::new(LabReport::ReportDate).date().not_null())
.col(ColumnDef::new(LabReport::ReportType).string_len(50).not_null())
.col(ColumnDef::new(LabReport::Indicators).json().null())
.col(ColumnDef::new(LabReport::ImageUrls).json().null())
.col(ColumnDef::new(LabReport::Indicators).json_binary().null())
.col(ColumnDef::new(LabReport::ImageUrls).json_binary().null())
.col(ColumnDef::new(LabReport::DoctorInterpretation).text().null())
.col(
ColumnDef::new(LabReport::CreatedAt)
@@ -587,8 +616,8 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(HealthTrend::PatientId).uuid().not_null())
.col(ColumnDef::new(HealthTrend::PeriodStart).date().not_null())
.col(ColumnDef::new(HealthTrend::PeriodEnd).date().not_null())
.col(ColumnDef::new(HealthTrend::IndicatorSummary).json().null())
.col(ColumnDef::new(HealthTrend::AbnormalItems).json().null())
.col(ColumnDef::new(HealthTrend::IndicatorSummary).json_binary().null())
.col(ColumnDef::new(HealthTrend::AbnormalItems).json_binary().null())
.col(
ColumnDef::new(HealthTrend::GenerationType)
.string_len(20)
@@ -795,6 +824,22 @@ impl MigrationTrait for Migration {
)
.await?;
// doctor_schedule 唯一约束:同一医生同一天同一时段不能重复排班
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_doctor_schedule_unique_slot")
.table(DoctorSchedule::Table)
.col(DoctorSchedule::TenantId)
.col(DoctorSchedule::DoctorId)
.col(DoctorSchedule::ScheduleDate)
.col(DoctorSchedule::StartTime)
.unique()
.to_owned(),
)
.await?;
// 13. follow_up_task — 随访任务
manager
.create_table(
@@ -1130,6 +1175,19 @@ impl MigrationTrait for Migration {
)
.await?;
// health_trend 索引
manager
.create_index(
Index::create()
.if_not_exists()
.name("idx_health_trend_tenant_patient")
.table(HealthTrend::Table)
.col(HealthTrend::TenantId)
.col(HealthTrend::PatientId)
.to_owned(),
)
.await?;
Ok(())
}
@@ -1237,6 +1295,7 @@ enum DoctorProfile {
Id,
TenantId,
UserId,
Name,
Department,
Title,
Specialty,