From e1618b40efbd06e61772dc69c008b07830786ad0 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 30 Aug 2024 14:13:56 +0200 Subject: [PATCH] add min + expiry --- Cargo.lock | 12 ++++++------ schema/item.json | 10 ++++++++++ src/item.rs | 3 ++- src/routes/item/mod.rs | 3 ++- src/routes/item/supply.rs | 5 +---- src/transaction.rs | 33 +++++++++++++++++++++++++++++++-- src/variant.rs | 39 ++++++++++++++++++++++++++++++++++++--- 7 files changed, 88 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4bfbdf9..cfb6e91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1242,7 +1242,7 @@ dependencies = [ [[package]] name = "mongod" version = "0.2.1" -source = "git+https://git.hydrar.de/jmarya/mongod#9b56a434e7fe32ba2157d0b9f725a43c74a3971a" +source = "git+https://git.hydrar.de/jmarya/mongod#9784c3cac65e5a1305e4f0e4ec769316eacced3c" dependencies = [ "chrono", "futures", @@ -1258,7 +1258,7 @@ dependencies = [ [[package]] name = "mongod_derive" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/mongod#9b56a434e7fe32ba2157d0b9f725a43c74a3971a" +source = "git+https://git.hydrar.de/jmarya/mongod#9784c3cac65e5a1305e4f0e4ec769316eacced3c" dependencies = [ "case", "proc-macro2", @@ -1369,9 +1369,9 @@ dependencies = [ [[package]] name = "object" -version = "0.36.3" +version = "0.36.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9" +checksum = "084f1a5821ac4c651660a94a7153d27ac9d8a53736203f58b31945ded098070a" dependencies = [ "memchr", ] @@ -2252,9 +2252,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.39.3" +version = "1.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" dependencies = [ "backtrace", "bytes", diff --git a/schema/item.json b/schema/item.json index 434382d..9f27fc2 100644 --- a/schema/item.json +++ b/schema/item.json @@ -26,6 +26,16 @@ "type": "string", "title": "Variant Name", "description": "The name of the Variant" + }, + "min": { + "type": "number", + "title": "Minimum inventory", + "description": "The minimum amount of inventory for an Item. Thre actual inventory amount should always be higher than that." + }, + "expiry": { + "type": "number", + "title": "Expiry days", + "description": "Number of days until this item variant is considered expired." } } } diff --git a/src/item.rs b/src/item.rs index 21fe673..aee129f 100644 --- a/src/item.rs +++ b/src/item.rs @@ -121,7 +121,8 @@ impl Item { .collect(); json!({ - "item": self.name, + "uuid": self._id, + "name": self.name, "category": self.category, "variants": variants }) diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 3126612..856653e 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -5,6 +5,7 @@ mod supply; pub use demand::*; pub use error::*; use mongod::Model; +use mongod::ToAPI; pub use supply::*; use rocket::get; @@ -53,7 +54,7 @@ pub async fn transaction_route(transaction: &str) -> FallibleApiResponse { let t = Transaction::get(transaction) .await .ok_or_else(|| api_error("No transaction with this UUID"))?; - Ok(t.api_json()) + Ok(t.api().await) } /// Returns unique values for a field diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index decfcd0..312471c 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -74,10 +74,7 @@ pub async fn inventory_route( let transactions = variant.inventory().await; - Ok(json!(transactions - .into_iter() - .map(|x| x.api_json()) - .collect::>())) + Ok(json!(mongod::vec_to_api(&transactions).await)) } /// Returns statistics for the Item Variant diff --git a/src/transaction.rs b/src/transaction.rs index 5300f7e..6ec5f1d 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -6,6 +6,8 @@ use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; +use crate::item::Item; + /// A Transaction of an Item Variant #[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] pub struct Transaction { @@ -64,7 +66,33 @@ impl Transaction { .ok().unwrap(); } - pub fn api_json(&self) -> serde_json::Value { + pub async fn is_expired(&self) -> bool { + let current_time = chrono::Utc::now().timestamp(); + + if let Some(expiry) = Item::get(&self.item) + .await + .unwrap() + .variant(&self.variant) + .unwrap() + .expiry + { + let date_added = self.timestamp; + + let expiration_ts = expiry * 24 * 60 * 60; + + if (date_added + expiration_ts) < current_time { + return true; + } else { + return false; + } + } + + false + } +} + +impl mongod::ToAPI for Transaction { + async fn api(&self) -> serde_json::Value { json!({ "uuid": self._id, "item": self.item, @@ -72,7 +100,8 @@ impl Transaction { "price": self.price, "origin": self.origin, "timestamp": self.timestamp, - "consumed": self.consumed + "consumed": self.consumed, + "expired": self.is_expired().await }) } } diff --git a/src/variant.rs b/src/variant.rs index f4028aa..a3cca01 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -43,6 +43,10 @@ pub struct Variant { pub variant: String, /// Variant Name pub name: String, + /// Minimum amount + pub min: Option, + /// Days until expiry + pub expiry: Option, } impl Variant { @@ -59,6 +63,16 @@ impl Variant { .as_str() .unwrap() .to_string(), + min: json + .as_mapping() + .unwrap() + .get("min") + .map(|x| x.as_i64().unwrap()), + expiry: json + .as_mapping() + .unwrap() + .get("expiry") + .map(|x| x.as_i64().unwrap()), } } @@ -85,7 +99,7 @@ impl Variant { let filter = doc! { "item": &self.item, "variant": &self.variant, - "consumed": { "$exists": false } + "consumed": { "$not": { "$type": "object" } } }; Transaction::find(filter, None, None).await.unwrap() @@ -95,7 +109,7 @@ impl Variant { let filter = doc! { "item": &self.item, "variant": &self.variant, - "consumed": { "$exists": true } + "consumed": { "$type": "object" } }; let result = Transaction::find_partial(filter, json!({}), None, None) @@ -184,6 +198,23 @@ impl Variant { .await } + pub async fn price_history_by_origin(&self, origin: &str) -> Vec { + Transaction::find( + doc! { + "item": &self.item, + "variant": &self.variant, + "origin": origin + }, + None, + Some(sort_by_timestamp()), + ) + .await + .unwrap() + .into_iter() + .map(|x| x.price) + .collect() + } + pub async fn get_latest_price(&self, origin: Option) -> Price { let mut filter = doc! { "item": &self.item, @@ -219,7 +250,9 @@ impl Variant { json!({ "item": self.item, "variant": self.variant, - "name": self.name + "name": self.name, + "min": self.min, + "expiry": self.expiry }) } }