fix location recursion

This commit is contained in:
JMARyA 2024-09-22 02:38:03 +02:00
parent bfd4c1c0b9
commit 7f32f2429e
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 44 additions and 6 deletions

View file

@ -55,6 +55,31 @@ impl Location {
}
.boxed()
}
// Get direct children
pub async fn children_direct(&self) -> Vec<Location> {
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<Location>> {
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 {

View file

@ -115,20 +115,33 @@ impl Transaction {
pub async fn in_location(l: &str) -> Option<Vec<Self>> {
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<Vec<Self>> {
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(),
);