add min + expiry
This commit is contained in:
parent
0e174dc06e
commit
e1618b40ef
7 changed files with 88 additions and 17 deletions
|
@ -121,7 +121,8 @@ impl Item {
|
|||
.collect();
|
||||
|
||||
json!({
|
||||
"item": self.name,
|
||||
"uuid": self._id,
|
||||
"name": self.name,
|
||||
"category": self.category,
|
||||
"variants": variants
|
||||
})
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue