add min + expiry

This commit is contained in:
JMARyA 2024-08-30 14:13:56 +02:00
parent 0e174dc06e
commit e1618b40ef
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 88 additions and 17 deletions

12
Cargo.lock generated
View file

@ -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",

View file

@ -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."
}
}
}

View file

@ -121,7 +121,8 @@ impl Item {
.collect();
json!({
"item": self.name,
"uuid": self._id,
"name": self.name,
"category": self.category,
"variants": variants
})

View file

@ -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

View file

@ -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::<Vec<_>>()))
Ok(json!(mongod::vec_to_api(&transactions).await))
}
/// Returns statistics for the Item Variant

View file

@ -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
})
}
}

View file

@ -43,6 +43,10 @@ pub struct Variant {
pub variant: String,
/// Variant Name
pub name: String,
/// Minimum amount
pub min: Option<i64>,
/// Days until expiry
pub expiry: Option<i64>,
}
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<Price> {
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<String>) -> 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
})
}
}