Elffuss BraidHub

kikocis/utopia public

dev · 4.8 KiBRawPermalink
1//! 数据映射 API:口径的列表、改写与改版历史。
2//!
3//! 审批(`decide`)留在 `review_routes`——那条端点已经在写审计流水
4//! (`mapping.decided`),换个界面调它即可,没有理由为了搬页面而搬端点。
5
6use axum::extract::{Path, Query, State};
7use axum::Json;
8use serde::Deserialize;
9use serde_json::json;
10use utopia_core::models::Role;
11use utopia_core::AppError;
12use uuid::Uuid;
13
14use super::graph_routes::require_kb;
15use crate::auth::AuthUser;
16use crate::error::ApiResult;
17use crate::state::AppState;
18
19/// 一页多少条。口径比审阅队列密(一行就是一个定义),一页给得起 25 条
20const MAPPING_PAGE: i64 = 25;
21
22#[derive(Deserialize)]
23pub struct ListQuery {
24 /// proposed | confirmed | rejected;缺省 = 全部
25 status: Option<String>,
26 q: Option<String>,
27 limit: Option<i64>,
28 offset: Option<i64>,
29}
30
31/// 一页口径 + 三种状态各多少。
32///
33/// **Viewer 就能看。** 口径是「这个数怎么算」,问数的答案直接由它决定——
34/// 看得见答案却看不见口径,等于要人信一个不给看的算法。改(`revise`)
35/// 才要 Editor。
36pub async fn list(
37 State(state): State<AppState>,
38 AuthUser(user): AuthUser,
39 Path(kb_id): Path<Uuid>,
40 Query(q): Query<ListQuery>,
41) -> ApiResult<Json<serde_json::Value>> {
42 require_kb(&state, &user, kb_id, Role::Viewer).await?;
43 let status = q.status.as_deref().filter(|s| !s.is_empty());
44 if let Some(s) = status {
45 if !matches!(s, "proposed" | "confirmed" | "rejected") {
46 return Err(AppError::invalid(
47 "bad_status",
48 "status must be proposed, confirmed or rejected",
49 )
50 .into());
51 }
52 }
53 let needle = q.q.as_deref().map(str::trim).filter(|s| !s.is_empty());
54 let limit = q.limit.unwrap_or(MAPPING_PAGE).clamp(1, 200);
55 let offset = q.offset.unwrap_or(0).max(0);
56
57 let (items, total) =
58 utopia_store::mappings::page(&state.pool, kb_id, status, needle, limit, offset).await?;
59 let (proposed, confirmed, rejected) =
60 utopia_store::mappings::status_counts(&state.pool, kb_id).await?;
61 Ok(Json(json!({
62 "items": items,
63 "total": total,
64 "counts": { "proposed": proposed, "confirmed": confirmed, "rejected": rejected },
65 })))
66}
67
68#[derive(Deserialize)]
69pub struct ReviseReq {
70 table_name: Option<String>,
71 expr: Option<String>,
72 sql: Option<String>,
73 unit: Option<String>,
74 summary: Option<String>,
75 #[serde(default)]
76 derived: bool,
77}
78
79/// 改一条口径。
80///
81/// **在此之前 `mappings::revise` 是零调用的**:函数在、留痕表在、没有路由。
82/// 于是口径确认之后就再没人改得动,也没人看得见——问数照着它算,人却够不着。
83pub async fn revise(
84 State(state): State<AppState>,
85 AuthUser(user): AuthUser,
86 Path((kb_id, mapping_id)): Path<(Uuid, Uuid)>,
87 Json(req): Json<ReviseReq>,
88) -> ApiResult<Json<serde_json::Value>> {
89 require_kb(&state, &user, kb_id, Role::Editor).await?;
90 // 空白等于没填:前端清空一个输入框传来的是 "",落库该是 NULL 而不是空串,
91 // 否则「有没有配 expr」这个判断要同时问 IS NULL 和 = ''
92 let clean = |s: &Option<String>| -> Option<String> {
93 s.as_deref()
94 .map(str::trim)
95 .filter(|x| !x.is_empty())
96 .map(str::to_string)
97 };
98 let (table_name, expr, sql, unit, summary) = (
99 clean(&req.table_name),
100 clean(&req.expr),
101 clean(&req.sql),
102 clean(&req.unit),
103 clean(&req.summary),
104 );
105 if table_name.is_none() && expr.is_none() && sql.is_none() {
106 return Err(AppError::invalid(
107 "empty_mapping",
108 "A mapping needs at least one of table, expression or SQL",
109 )
110 .into());
111 }
112 utopia_store::mappings::revise(
113 &state.pool,
114 kb_id,
115 mapping_id,
116 table_name.as_deref(),
117 expr.as_deref(),
118 sql.as_deref(),
119 unit.as_deref(),
120 summary.as_deref(),
121 req.derived,
122 user.id,
123 )
124 .await?;
125 let _ = utopia_store::audit::record(
126 &state.pool,
127 Some(kb_id),
128 user.id,
129 "mapping.revised",
130 "concept_mapping",
131 Some(mapping_id),
132 json!({}),
133 )
134 .await;
135 Ok(Json(json!({ "ok": true })))
136}
137
138/// 一条口径的改版历史。0006 说留痕是为了答得出「上季度这个数是怎么算的」,
139/// 这是那句话的兑现处。
140pub async fn revisions(
141 State(state): State<AppState>,
142 AuthUser(user): AuthUser,
143 Path((kb_id, mapping_id)): Path<(Uuid, Uuid)>,
144) -> ApiResult<Json<serde_json::Value>> {
145 require_kb(&state, &user, kb_id, Role::Viewer).await?;
146 let rows = utopia_store::mappings::revisions(&state.pool, kb_id, mapping_id).await?;
147 Ok(Json(json!({ "revisions": rows })))
148}