update expiry
This commit is contained in:
parent
012e5273b1
commit
58f5b1d93c
3 changed files with 71 additions and 51 deletions
|
@ -45,36 +45,6 @@ pub fn get_items_route(itemdb: &State<ItemDB>, t: Token, c: &State<Config>) -> F
|
||||||
Ok(json!({"items": items}))
|
Ok(json!({"items": items}))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/items/stat")]
|
|
||||||
pub async fn item_stat_route(
|
|
||||||
itemdb: &State<ItemDB>,
|
|
||||||
t: Token,
|
|
||||||
c: &State<Config>,
|
|
||||||
) -> FallibleApiResponse {
|
|
||||||
check_auth!(t, c);
|
|
||||||
|
|
||||||
let items = itemdb.items();
|
|
||||||
let item_count = items.len();
|
|
||||||
let mut transaction_count = 0;
|
|
||||||
let mut total_price = 0.0;
|
|
||||||
|
|
||||||
for item in items {
|
|
||||||
for var in itemdb.get_item(&item).unwrap().variants.keys() {
|
|
||||||
let item_var = itemdb.get_item(&item).unwrap().variant(var).unwrap();
|
|
||||||
for t in item_var.inventory().await {
|
|
||||||
transaction_count += 1;
|
|
||||||
total_price += t.price.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(json!({
|
|
||||||
"item_count": item_count,
|
|
||||||
"total_transactions": transaction_count,
|
|
||||||
"total_price": total_price
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Return an API Response for an `Item`
|
/// Return an API Response for an `Item`
|
||||||
#[get("/item/<item_id>")]
|
#[get("/item/<item_id>")]
|
||||||
pub fn item_route(
|
pub fn item_route(
|
||||||
|
@ -162,11 +132,17 @@ pub async fn unique_field_route(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/items/expired")]
|
#[get("/items/expired?<days>")]
|
||||||
pub async fn expired_items_route(t: Token, c: &State<Config>) -> FallibleApiResponse {
|
pub async fn expired_items_route(days: Option<&str>, t: Token, c: &State<Config>) -> FallibleApiResponse {
|
||||||
check_auth!(t, c);
|
check_auth!(t, c);
|
||||||
|
|
||||||
let t = Transaction::active_expired().await;
|
let days = if let Some(days) = days {
|
||||||
|
days.parse().map_err(|_| api_error("Invalid days value")).ok()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let t = Transaction::active_expired(days).await;
|
||||||
|
|
||||||
Ok(json!(t))
|
Ok(json!(t))
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,3 +54,33 @@ pub async fn variant_price_latest_by_origin(
|
||||||
.first()
|
.first()
|
||||||
.unwrap()))
|
.unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/items/stat")]
|
||||||
|
pub async fn item_stat_route(
|
||||||
|
itemdb: &State<ItemDB>,
|
||||||
|
t: Token,
|
||||||
|
c: &State<Config>,
|
||||||
|
) -> FallibleApiResponse {
|
||||||
|
check_auth!(t, c);
|
||||||
|
|
||||||
|
let items = itemdb.items();
|
||||||
|
let item_count = items.len();
|
||||||
|
let mut transaction_count = 0;
|
||||||
|
let mut total_price = 0.0;
|
||||||
|
|
||||||
|
for item in items {
|
||||||
|
for var in itemdb.get_item(&item).unwrap().variants.keys() {
|
||||||
|
let item_var = itemdb.get_item(&item).unwrap().variant(var).unwrap();
|
||||||
|
for t in item_var.inventory().await {
|
||||||
|
transaction_count += 1;
|
||||||
|
total_price += t.price.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"item_count": item_count,
|
||||||
|
"total_transactions": transaction_count,
|
||||||
|
"total_price": total_price
|
||||||
|
}))
|
||||||
|
}
|
|
@ -95,24 +95,32 @@ impl Transaction {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn is_expired_at(&self, time: i64) -> bool {
|
||||||
|
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;
|
||||||
|
|
||||||
|
return (date_added + expiration_ts) < time;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn is_expired_in_days(&self, days: i64) -> bool {
|
||||||
|
let current_time = chrono::Utc::now().timestamp();
|
||||||
|
self.is_expired_at(current_time + (days * 24 * 60 * 60)).await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn is_expired(&self) -> bool {
|
pub async fn is_expired(&self) -> bool {
|
||||||
let current_time = chrono::Utc::now().timestamp();
|
let current_time = chrono::Utc::now().timestamp();
|
||||||
|
self.is_expired_at(current_time).await
|
||||||
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;
|
|
||||||
|
|
||||||
return (date_added + expiration_ts) < current_time;
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn in_location(l: &str) -> Option<Vec<Self>> {
|
pub async fn in_location(l: &str) -> Option<Vec<Self>> {
|
||||||
|
@ -153,7 +161,7 @@ impl Transaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all Transactions which are not consumed and are expired
|
/// Get all Transactions which are not consumed and are expired
|
||||||
pub async fn active_expired() -> Vec<Self> {
|
pub async fn active_expired(days: Option<i64>) -> Vec<Self> {
|
||||||
let items = Self::find(
|
let items = Self::find(
|
||||||
doc! {
|
doc! {
|
||||||
"consumed": { "$not": { "$type": "object" } }
|
"consumed": { "$not": { "$type": "object" } }
|
||||||
|
@ -166,7 +174,13 @@ impl Transaction {
|
||||||
|
|
||||||
let expired_items: Vec<_> = futures::stream::iter(items)
|
let expired_items: Vec<_> = futures::stream::iter(items)
|
||||||
.filter_map(|item| async move {
|
.filter_map(|item| async move {
|
||||||
if item.is_expired().await {
|
let expired = if let Some(days) = days {
|
||||||
|
item.is_expired_in_days(days).await
|
||||||
|
} else {
|
||||||
|
item.is_expired().await
|
||||||
|
};
|
||||||
|
|
||||||
|
if expired {
|
||||||
Some(item)
|
Some(item)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
Loading…
Reference in a new issue