diff --git a/src/location.rs b/src/location.rs index ef5ab46..d2ebdf9 100644 --- a/src/location.rs +++ b/src/location.rs @@ -55,6 +55,31 @@ impl Location { } .boxed() } + + // Get direct children + pub async fn children_direct(&self) -> Vec { + Location::find(doc! { "parent": self._id.clone()}, None, None) + .await + .unwrap() + } + + // Get all children locations + pub fn children_recursive<'a>(&'a self) -> futures::future::BoxFuture<'a, Vec> { + async move { + let mut all = Vec::new(); + + let direct = self.children_direct().await; + all.extend_from_slice(&direct); + + for loc in direct { + let sub = loc.children_recursive().await; + all.extend_from_slice(&sub); + } + + all + } + .boxed() + } } impl ToAPI for Location { diff --git a/src/transaction.rs b/src/transaction.rs index 6fd74b3..748d622 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -115,20 +115,33 @@ impl Transaction { pub async fn in_location(l: &str) -> Option> { let l = reference_of!(Location, l)?; - Some(Self::find(doc! { "location": l}, None, None).await.unwrap()) + Some( + Self::find( + doc! { "location": l, "consumed": { "$not": { "$type": "object" } }}, + None, + None, + ) + .await + .unwrap(), + ) } pub async fn in_location_recursive(l: &str) -> Option> { - let locations = Location::find(doc! { "parent": l}, None, None) - .await - .unwrap(); + // get the children of this location + let locations = Location::get(l).await?.children_recursive().await; let l = reference_of!(Location, l)?; - let mut transactions = Self::find(doc! { "location": l}, None, None).await.unwrap(); + let mut transactions = Self::find( + doc! { "location": l, "consumed": { "$not": { "$type": "object" } },}, + None, + None, + ) + .await + .unwrap(); for loc in locations { transactions.extend( - Self::find(doc! { "location": loc.reference() }, None, None) + Self::find(doc! { "location": loc.reference(), "consumed": { "$not": { "$type": "object" } }}, None, None) .await .unwrap(), );