Expose a permission-filtered approvals inbox and readable order summaries

This commit is contained in:
2026-11-17 22:38:14 +01:00
parent 18b31df91b
commit bc8100602b
6 changed files with 288 additions and 13 deletions
+20 -13
View File
@@ -65,7 +65,8 @@ pub(crate) async fn principal(
.map_err(|s| Error(s, crate::auth::error_code(s)))
}
pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
r.route("/api/v1/tenants/{tenant}/operation-plans", post(plan_http))
r.route("/api/v1/approvals", get(crate::approvals::inbox))
.route("/api/v1/tenants/{tenant}/operation-plans", post(plan_http))
.route(
"/api/v1/tenants/{tenant}/operation-plans/{plan}",
get(plan_get_http),
@@ -1089,7 +1090,7 @@ pub async fn operation(
approval = "expired".into();
sqlx::query("UPDATE approval_requests SET approval_state='expired' WHERE operation_id=$1 AND approval_state='pending_approval'").bind(id).execute(&mut *tx).await?;
}
let jobs=sqlx::query("SELECT job_id,attempt_id,state,cancel_requested,revision,pending_reason FROM jobs WHERE operation_id=$1 ORDER BY job_id LIMIT 101").bind(id).fetch_all(&mut *tx).await?;
let jobs=sqlx::query("SELECT asset_id,agent_id,job_id,attempt_id,state,cancel_requested,revision,pending_reason FROM jobs WHERE operation_id=$1 ORDER BY job_id LIMIT 101").bind(id).fetch_all(&mut *tx).await?;
let mut counts = serde_json::Map::new();
for j in &jobs {
let s: String = j.get("state");
@@ -1126,10 +1127,15 @@ pub async fn operation(
})
};
let items = jobs.iter().map(job_summary).collect::<Vec<_>>();
let mut result = crate::approvals::summary(&mut tx, p, plan.plan_id).await?;
result["state"] = json!(approval);
result["planned_job_count"] = json!(plan.jobs.len());
result["job_count"] = json!(n);
result["execution_state"] = json!(execution);
result["counts"] = json!(counts);
result["jobs"] = json!(items);
tx.commit().await?;
Ok(
json!({"operation_id":id,"plan_id":plan.plan_id,"plan_hash":row.get::<String,_>("plan_hash"),"approval_state":approval,"state":approval,"planned_job_count":plan.jobs.len(),"job_count":n,"execution_state":execution,"counts":counts,"jobs":items}),
)
Ok(result)
}
fn job_view(r: &sqlx::postgres::PgRow) -> Value {
let mut result = job_summary(r);
@@ -1137,14 +1143,14 @@ fn job_view(r: &sqlx::postgres::PgRow) -> Value {
result
}
fn job_summary(r: &sqlx::postgres::PgRow) -> Value {
json!({"job_id":r.get::<Uuid,_>("job_id"),"attempt_id":r.get::<Uuid,_>("attempt_id"),"state":r.get::<String,_>("state"),"cancel_requested":r.get::<bool,_>("cancel_requested"),"revision":r.get::<i64,_>("revision"),"pending_reason":r.get::<Option<String>,_>("pending_reason")})
json!({"asset_id":r.get::<Uuid,_>("asset_id"),"agent_id":r.get::<Uuid,_>("agent_id"),"job_id":r.get::<Uuid,_>("job_id"),"attempt_id":r.get::<Uuid,_>("attempt_id"),"state":r.get::<String,_>("state"),"cancel_requested":r.get::<bool,_>("cancel_requested"),"revision":r.get::<i64,_>("revision"),"pending_reason":r.get::<Option<String>,_>("pending_reason")})
}
pub async fn job(state: &crate::State, p: &Principal, tenant: Uuid, id: Uuid) -> Result<Value> {
scope(p, tenant, false)?;
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
crate::permissions::require_principal(&mut tx, p, "operations:read").await?;
let r = sqlx::query(
"SELECT job_id,attempt_id,state,cancel_requested,revision,result,pending_reason FROM jobs WHERE job_id=$1",
"SELECT asset_id,agent_id,job_id,attempt_id,state,cancel_requested,revision,result,pending_reason FROM jobs WHERE job_id=$1",
)
.bind(id)
.fetch_optional(&mut *tx)
@@ -1210,13 +1216,14 @@ async fn plan_get_http(
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
let (plan, hash, bytes) = load_plan(&mut tx, id).await?;
crate::permissions::require_principal(&mut tx, &p, "operations:read").await?;
let approval: Option<String> =
sqlx::query_scalar("SELECT approval_state FROM approval_requests WHERE plan_id=$1")
.bind(id)
.fetch_optional(&mut *tx)
.await?;
let metadata = crate::approvals::summary(&mut tx, &p, id).await?;
let approval = metadata["approval_state"].as_str().map(str::to_owned);
let mut view = plan_view(&plan, &hash, approval, &bytes)?;
for (key, value) in metadata.as_object().into_iter().flatten() {
view[key] = value.clone();
}
tx.commit().await?;
Ok(Json(plan_view(&plan, &hash, approval, &bytes)?))
Ok(Json(view))
}
async fn operation_http(
State(s): State<crate::State>,