valence_core/deletion/
service.rs1use chrono::Utc;
4use serde_json::{json, Value};
5use uuid::Uuid;
6
7use crate::actor::Actor;
8use crate::error::{Error, Result};
9use crate::query::QueryCore;
10use crate::runtime::Valence;
11
12fn system_valence(v: &Valence) -> Valence {
13 v.with_actor(Actor::System {
14 operation: "valence_deletion_run".to_string(),
15 })
16}
17
18pub struct DeletionService;
19
20impl DeletionService {
21 pub async fn create_run(
22 root_table: &str,
23 root_record_id: &str,
24 actor_json: Value,
25 v: &Valence,
26 ) -> Result<String> {
27 let run_id = Uuid::new_v4().to_string();
28 let requested_by = actor_json.to_string();
29 let sys = system_valence(v);
30 let backend = sys.backend_for_table("valence_deletion_run")?;
31 let row = json!({
32 "id": run_id,
33 "root_table": root_table,
34 "root_record_id": root_record_id,
35 "status": "queued",
36 "total_steps": 0,
37 "completed_steps": 0,
38 "failed_steps": 0,
39 "requested_by": requested_by,
40 "requested_at": Utc::now(),
41 });
42 backend
43 .create_record("valence_deletion_run", row)
44 .await
45 .map_err(|e| Error::Database(e.to_string()))?;
46 Ok(run_id)
47 }
48
49 pub async fn get_run_json(run_id: &str, v: &Valence) -> Result<Option<Value>> {
50 let sys = system_valence(v);
51 QueryCore::get_record_json("valence_deletion_run", run_id, &sys)
52 .await
53 .map_err(|e| Error::Database(e.to_string()))
54 }
55}