diff --git a/src/flow.rs b/src/flow.rs index e3ca96d..1adeace 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -67,6 +67,14 @@ impl DoneInfo { produced: None, } } + + pub fn api(&self) -> serde_json::Value { + json!({ + "ended": self.ended, + "next": self.next.as_ref().map(|x| x.id()), + "produced": self.produced.as_ref().map(|x| x.iter().map(|t| t.id()).collect::>()), + }) + } } /// A production flow @@ -109,6 +117,18 @@ impl Validate for Flow { } } +impl ToAPI for Flow { + async fn api(&self) -> serde_json::Value { + json!({ + "id": self._id, + "started": self.started, + "kind": self.kind.id(), + "input": self.input.as_ref().map(|x| x.iter().map(|t| t.id()).collect::>()), + "done": self.done.as_ref().map(|x| x.api()) + }) + } +} + impl Flow { pub async fn create(kind: &str, input: Option>) -> Self { let f = Self { diff --git a/src/routes/flow.rs b/src/routes/flow.rs index 8ceea1a..d2a3dea 100644 --- a/src/routes/flow.rs +++ b/src/routes/flow.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; -use mongod::{Model, Referencable, ToAPI}; +use mongod::{vec_to_api, Model, Referencable, ToAPI}; +use mongodb::bson::doc; use rocket::{get, post, serde::json::Json, State}; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -30,6 +31,31 @@ pub async fn flow_info( Ok(flowinfo.api().await) } +#[get("/flow//active")] +pub async fn active_flows_route( + id: &str, + flows: &State>, + t: Token, + c: &State, +) -> FallibleApiResponse { + check_auth!(t, c); + + let flowinfo = flows.get(id).ok_or_else(|| api_error("Flow not found"))?; + + let flow = Flow::find( + doc! { + "kind": flowinfo.reference(), + "done": { "$not": { "$type": "object" } } + }, + None, + None, + ) + .await + .unwrap(); + + Ok(json!(vec_to_api(&flow).await)) +} + #[get("/flows")] pub async fn flows_list( flows: &State>,