diff --git a/src/location.rs b/src/location.rs index aca348f..3c0c2bd 100644 --- a/src/location.rs +++ b/src/location.rs @@ -1,3 +1,6 @@ +use std::{future::Future, pin::Pin}; + +use futures::FutureExt; use mongod::{ derive::{Model, Referencable}, Model, ToAPI, Validate, @@ -32,13 +35,37 @@ impl Validate for Location { } } +impl Location { + /// Recursively get the conditions of a location. This inherits from parent locations. + pub fn conditions_rec<'a>( + &'a self, + ) -> futures::future::BoxFuture<'a, Option> { + async move { + if let Some(cond) = &self.conditions { + return Some(cond.clone()); + } + + if let Some(parent) = &self.parent { + if let Some(parent_loc) = Location::get(parent).await { + if let Some(cond) = parent_loc.conditions_rec().await { + return Some(cond); + } + } + } + + None + } + .boxed() + } +} + impl ToAPI for Location { async fn api(&self) -> serde_json::Value { json!({ "id": self._id, "name": self.name, "parent": self.parent, - "conditions": self.conditions + "conditions": self.conditions_rec().await }) } }