feat: Week 4 收尾 + 架构治理 — 搜索/家长中心/Feature Flag/Docker/环境配置
架构治理: - Feature Flag 落地: Cargo.toml [features] default=["diary"] + main.rs cfg 条件编译 - 环境配置统一: AppConfig 类 + --dart-define 注入 + SSE 端口 8080→3000 修复 搜索替代方案 (无 FTS): - SearchBloc + 标签/心情筛选接入后端 API - JournalRepository 扩展 mood/tag 筛选参数 - 搜索页 UI 接入实际数据(替换占位文本) 家长中心最小集 (PIPL 合规): - 后端: parent_service (绑定/查看/导出/删除/解绑) + parent_handler (6 个 API 端点) - 前端: ParentBloc + ParentPage 功能完整实现 - 绑定孩子、只读查看日记、导出数据、删除数据、解绑 Docker 部署: - verify.sh 健康检查脚本 (Axum/PG/Redis/OpenAPI 四项检查) 测试修复: - home_bloc_test / calendar_bloc_test 适配 JournalRepository 新参数 验证: flutter test 84/84 pass, cargo test 76/76 pass, cargo check pass
This commit is contained in:
@@ -10,3 +10,4 @@ pub mod sticker_service;
|
||||
pub mod achievement_service;
|
||||
pub mod mood_stats_service;
|
||||
pub mod content_safety_service;
|
||||
pub mod parent_service;
|
||||
|
||||
295
crates/erp-diary/src/service/parent_service.rs
Normal file
295
crates/erp-diary/src/service/parent_service.rs
Normal file
@@ -0,0 +1,295 @@
|
||||
// 家长-孩子绑定服务 — PIPL 合规: 数据查阅/导出/删除
|
||||
|
||||
use chrono::Utc;
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, PaginatorTrait,
|
||||
QueryFilter, QueryOrder, Set,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::entity::journal_entry;
|
||||
use crate::entity::parent_child_binding;
|
||||
use crate::error::{DiaryError, DiaryResult};
|
||||
use erp_core::events::{DomainEvent, EventBus};
|
||||
|
||||
/// 家长中心服务 — 绑定管理、数据查阅、导出、删除
|
||||
pub struct ParentService;
|
||||
|
||||
impl ParentService {
|
||||
/// 绑定孩子 — 家长通过孩子用户 ID 建立绑定关系
|
||||
///
|
||||
/// 检查是否已存在有效绑定,避免重复绑定。
|
||||
/// 插入后发布 `diary.parent.child_bound` 事件。
|
||||
pub async fn bind_child(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
child_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<parent_child_binding::Model> {
|
||||
// 检查是否已绑定
|
||||
let existing = parent_child_binding::Entity::find()
|
||||
.filter(parent_child_binding::Column::ParentId.eq(parent_id))
|
||||
.filter(parent_child_binding::Column::ChildId.eq(child_id))
|
||||
.filter(parent_child_binding::Column::TenantId.eq(tenant_id))
|
||||
.filter(parent_child_binding::Column::Status.ne("revoked"))
|
||||
.one(db)
|
||||
.await?;
|
||||
|
||||
if existing.is_some() {
|
||||
return Err(DiaryError::BadRequest("已绑定该孩子".to_string()));
|
||||
}
|
||||
|
||||
let now = Utc::now();
|
||||
let id = Uuid::now_v7();
|
||||
|
||||
let model = parent_child_binding::ActiveModel {
|
||||
id: Set(id),
|
||||
tenant_id: Set(tenant_id),
|
||||
parent_id: Set(parent_id),
|
||||
child_id: Set(child_id),
|
||||
verification_method: Set("manual".to_string()),
|
||||
verified_at: Set(Some(now)),
|
||||
status: Set("verified".to_string()),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(parent_id),
|
||||
updated_by: Set(parent_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
|
||||
let inserted = model.insert(db).await?;
|
||||
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.parent.child_bound",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"parent_id": parent_id,
|
||||
"child_id": child_id,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(inserted)
|
||||
}
|
||||
|
||||
/// 获取家长绑定的孩子列表
|
||||
///
|
||||
/// 只返回 status=verified 且未软删除的绑定。
|
||||
pub async fn list_children(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<Vec<parent_child_binding::Model>> {
|
||||
let bindings = parent_child_binding::Entity::find()
|
||||
.filter(parent_child_binding::Column::TenantId.eq(tenant_id))
|
||||
.filter(parent_child_binding::Column::ParentId.eq(parent_id))
|
||||
.filter(parent_child_binding::Column::Status.eq("verified"))
|
||||
.filter(parent_child_binding::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(bindings)
|
||||
}
|
||||
|
||||
/// 查看孩子日记 — 只读,家长只能查看已绑定的孩子的日记
|
||||
///
|
||||
/// 先验证绑定关系,再分页查询孩子的日记列表。
|
||||
pub async fn get_child_journals(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
child_id: Uuid,
|
||||
page: u64,
|
||||
page_size: u64,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<(Vec<journal_entry::Model>, u64)> {
|
||||
// 验证绑定关系
|
||||
Self::verify_binding(tenant_id, parent_id, child_id, db).await?;
|
||||
|
||||
let page_size = page_size.min(100).max(1);
|
||||
let page = page.max(1);
|
||||
|
||||
let paginator = journal_entry::Entity::find()
|
||||
.filter(journal_entry::Column::TenantId.eq(tenant_id))
|
||||
.filter(journal_entry::Column::AuthorId.eq(child_id))
|
||||
.filter(journal_entry::Column::DeletedAt.is_null())
|
||||
.order_by_desc(journal_entry::Column::Date)
|
||||
.order_by_desc(journal_entry::Column::CreatedAt)
|
||||
.paginate(db, page_size);
|
||||
|
||||
let total = paginator.num_items().await?;
|
||||
let journals = paginator.fetch_page(page.saturating_sub(1)).await?;
|
||||
|
||||
Ok((journals, total))
|
||||
}
|
||||
|
||||
/// 导出孩子数据 — 返回所有日记的 JSON(PIPL 数据可携带权)
|
||||
///
|
||||
/// 先验证绑定关系,再查询孩子全部未删除的日记。
|
||||
pub async fn export_child_data(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
child_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<serde_json::Value> {
|
||||
Self::verify_binding(tenant_id, parent_id, child_id, db).await?;
|
||||
|
||||
let journals = journal_entry::Entity::find()
|
||||
.filter(journal_entry::Column::TenantId.eq(tenant_id))
|
||||
.filter(journal_entry::Column::AuthorId.eq(child_id))
|
||||
.filter(journal_entry::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
Ok(serde_json::json!({
|
||||
"child_id": child_id,
|
||||
"exported_at": Utc::now(),
|
||||
"journals": journals,
|
||||
}))
|
||||
}
|
||||
|
||||
/// 删除孩子数据 — 软删除所有日记(PIPL 删除权)
|
||||
///
|
||||
/// 软删除孩子全部未删除的日记,逐条设置 deleted_at。
|
||||
/// 发布 `diary.parent.data_deleted` 事件记录操作。
|
||||
pub async fn delete_child_data(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
child_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
event_bus: &EventBus,
|
||||
) -> DiaryResult<usize> {
|
||||
Self::verify_binding(tenant_id, parent_id, child_id, db).await?;
|
||||
|
||||
let journals = journal_entry::Entity::find()
|
||||
.filter(journal_entry::Column::TenantId.eq(tenant_id))
|
||||
.filter(journal_entry::Column::AuthorId.eq(child_id))
|
||||
.filter(journal_entry::Column::DeletedAt.is_null())
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
let count = journals.len();
|
||||
let now = Utc::now();
|
||||
|
||||
for journal in journals {
|
||||
let current_version = journal.version;
|
||||
let mut active: journal_entry::ActiveModel = journal.into();
|
||||
active.deleted_at = Set(Some(now));
|
||||
active.updated_at = Set(now);
|
||||
active.updated_by = Set(parent_id);
|
||||
active.version = Set(current_version + 1);
|
||||
active.update(db).await?;
|
||||
}
|
||||
|
||||
event_bus
|
||||
.publish(
|
||||
DomainEvent::new(
|
||||
"diary.parent.data_deleted",
|
||||
tenant_id,
|
||||
serde_json::json!({
|
||||
"parent_id": parent_id,
|
||||
"child_id": child_id,
|
||||
"deleted_count": count,
|
||||
}),
|
||||
),
|
||||
db,
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
/// 解绑孩子 — 将绑定状态设为 revoked
|
||||
pub async fn unbind_child(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
child_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<()> {
|
||||
let binding = parent_child_binding::Entity::find()
|
||||
.filter(parent_child_binding::Column::TenantId.eq(tenant_id))
|
||||
.filter(parent_child_binding::Column::ParentId.eq(parent_id))
|
||||
.filter(parent_child_binding::Column::ChildId.eq(child_id))
|
||||
.filter(parent_child_binding::Column::Status.eq("verified"))
|
||||
.filter(parent_child_binding::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DiaryError::NotFound("绑定关系不存在".to_string()))?;
|
||||
|
||||
let current_version = binding.version;
|
||||
let now = Utc::now();
|
||||
let mut active: parent_child_binding::ActiveModel = binding.into();
|
||||
active.status = Set("revoked".to_string());
|
||||
active.updated_at = Set(now);
|
||||
active.updated_by = Set(parent_id);
|
||||
active.version = Set(current_version + 1);
|
||||
active.update(db).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 验证家长-孩子绑定关系
|
||||
///
|
||||
/// 查询是否存在 status=verified 且未删除的绑定记录,
|
||||
/// 不存在则返回 Forbidden 错误。
|
||||
async fn verify_binding(
|
||||
tenant_id: Uuid,
|
||||
parent_id: Uuid,
|
||||
child_id: Uuid,
|
||||
db: &DatabaseConnection,
|
||||
) -> DiaryResult<()> {
|
||||
let exists = parent_child_binding::Entity::find()
|
||||
.filter(parent_child_binding::Column::TenantId.eq(tenant_id))
|
||||
.filter(parent_child_binding::Column::ParentId.eq(parent_id))
|
||||
.filter(parent_child_binding::Column::ChildId.eq(child_id))
|
||||
.filter(parent_child_binding::Column::Status.eq("verified"))
|
||||
.filter(parent_child_binding::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?;
|
||||
|
||||
if exists.is_none() {
|
||||
return Err(DiaryError::Forbidden);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// 验证 verify_binding 在无匹配记录时的错误类型
|
||||
/// 由于没有数据库连接,这里只测试错误类型枚举匹配
|
||||
#[test]
|
||||
fn forbidden_error_is_correct_variant() {
|
||||
let err = DiaryError::Forbidden;
|
||||
match err {
|
||||
DiaryError::Forbidden => {}
|
||||
other => panic!("Expected Forbidden, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_request_error_carries_message() {
|
||||
let err = DiaryError::BadRequest("已绑定该孩子".to_string());
|
||||
match err {
|
||||
DiaryError::BadRequest(msg) => assert_eq!(msg, "已绑定该孩子"),
|
||||
other => panic!("Expected BadRequest, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_found_error_carries_message() {
|
||||
let err = DiaryError::NotFound("绑定关系不存在".to_string());
|
||||
match err {
|
||||
DiaryError::NotFound(msg) => assert_eq!(msg, "绑定关系不存在"),
|
||||
other => panic!("Expected NotFound, got {:?}", other),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user