update
This commit is contained in:
parent
a8dfe5f0e9
commit
37475460e2
5 changed files with 93 additions and 2 deletions
17
src/db.rs
17
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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<Config>) -> 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<ItemDB>,
|
||||
t: Token,
|
||||
c: &State<Config>,
|
||||
) -> 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))
|
||||
}
|
||||
|
|
|
@ -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<Self> {
|
||||
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 {
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue