add active flow route

This commit is contained in:
JMARyA 2024-09-19 08:38:37 +02:00
parent 2149217dc2
commit 1271387131
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 47 additions and 1 deletions

View file

@ -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::<Vec<_>>()),
})
}
}
/// 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::<Vec<_>>()),
"done": self.done.as_ref().map(|x| x.api())
})
}
}
impl Flow {
pub async fn create(kind: &str, input: Option<Vec<Reference>>) -> Self {
let f = Self {

View file

@ -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/<id>/active")]
pub async fn active_flows_route(
id: &str,
flows: &State<JSONStore<FlowInfo>>,
t: Token,
c: &State<Config>,
) -> 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<JSONStore<FlowInfo>>,