From 37475460e2e5bd8cb6d14f3feb4853c22830b955 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 12 Sep 2024 10:58:49 +0200 Subject: [PATCH] update --- src/db.rs | 17 +++++++++++++++++ src/main.rs | 4 +++- src/routes/item/mod.rs | 33 +++++++++++++++++++++++++++++++++ src/transaction.rs | 29 ++++++++++++++++++++++++++++- src/variant.rs | 12 ++++++++++++ 5 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index e54638c..cd04b02 100644 --- a/src/db.rs +++ b/src/db.rs @@ -43,3 +43,20 @@ impl ItemDB { ret } } + +/// Get all item variants which inventory is under the minimum. Returns a Vec with the item variants id and the missing quanity to reach minimum. +pub async fn get_items_without_min_satisfied(db: &ItemDB) -> Vec<(String, i64)> { + let mut ret = Vec::new(); + + for item in db.items() { + let item = db.get_item(&item).unwrap(); + for var in &item.variants { + let res = var.1.is_below_min().await; + if res.0 { + ret.push((format!("{}::{}", var.1.item, var.1.variant), res.1)); + } + } + } + + ret +} diff --git a/src/main.rs b/src/main.rs index bc74614..aae6333 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,7 +85,9 @@ async fn rocket() -> _ { routes::item::locations_list, routes::item::location_inventory, routes::flow::flow_info, - routes::flow::flows_list + routes::flow::flows_list, + routes::item::expired_items_route, + routes::item::min_items_route ], ) .manage(itemdb) diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 1790c7a..004006a 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -16,6 +16,7 @@ use serde_json::json; use crate::check_auth; use crate::config::Config; +use crate::db::get_items_without_min_satisfied; use crate::db::ItemDB; use crate::routes::Token; use crate::transaction::Transaction; @@ -108,3 +109,35 @@ pub async fn unique_field_route( _ => Err(api_error("Unknown field")), } } + +#[get("/items/expired")] +pub async fn expired_items_route(t: Token, c: &State) -> FallibleApiResponse { + check_auth!(t, c); + + let t = Transaction::active_expired().await; + + Ok(json!(t)) +} + +#[get("/items/min")] +pub async fn min_items_route( + itemdb: &State, + t: Token, + c: &State, +) -> FallibleApiResponse { + check_auth!(t, c); + + let t: Vec<_> = get_items_without_min_satisfied(&itemdb) + .await + .into_iter() + .map(|x| { + json!({ + "item_variant": x.0, + "need": x.1 + } + ) + }) + .collect(); + + Ok(json!(t)) +} diff --git a/src/transaction.rs b/src/transaction.rs index a43e273..53c5f72 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,7 +1,8 @@ +use futures::StreamExt; use mongod::{ assert_reference_of, derive::{Model, Referencable}, - reference_of, Model, Referencable, Reference, Validate, + reference_of, Model, Referencable, Reference, Sort, Validate, }; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; @@ -132,6 +133,32 @@ impl Transaction { transactions } + + /// Get all Transactions which are not consumed and are expired + pub async fn active_expired() -> Vec { + let items = Self::find( + doc! { + "consumed": { "$not": { "$type": "object" } } + }, + None, + Some(doc! { "timestamp": Sort::Descending }), + ) + .await + .unwrap(); + + let expired_items: Vec<_> = futures::stream::iter(items) + .filter_map(|item| async move { + if item.is_expired().await { + Some(item) + } else { + None + } + }) + .collect() + .await; + + expired_items + } } impl mongod::ToAPI for Transaction { diff --git a/src/variant.rs b/src/variant.rs index e27265c..7271b2c 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -239,6 +239,18 @@ impl Variant { .clone() } + /// Check if item variant is below minimum. Returns if this is the case and the number needed to fulfill minimum + pub async fn is_below_min(&self) -> (bool, i64) { + if let Some(min) = self.min { + let amount = self.inventory().await.len() as i64; + if amount < min { + return (true, min - amount); + } + } + + (false, 0) + } + pub async fn stat(&self) -> serde_json::Value { let active_transactions = self.inventory().await;