From 2149217dc25b6d9b3c7a3e6feaecdeb4cfe5f8b4 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 18 Sep 2024 08:29:27 +0200 Subject: [PATCH] condition inherit location --- src/location.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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 }) } }