149 lines
3.8 KiB
Rust
149 lines
3.8 KiB
Rust
use crate::check_auth;
|
|
use crate::config::{Config, Webhook};
|
|
use crate::routes::Token;
|
|
use crate::{
|
|
db::ItemDB,
|
|
routes::{FallibleApiResponse, api_error},
|
|
};
|
|
use based::request::api::{ToAPI, vec_to_api};
|
|
use rocket::serde::json::Json;
|
|
use rocket::{State, get, post};
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::json;
|
|
|
|
use super::{item_does_not_exist_error, variant_does_not_exist_error};
|
|
|
|
#[derive(Deserialize, Debug, Clone, Serialize)]
|
|
pub struct SupplyForm {
|
|
pub item: String,
|
|
pub variant: String,
|
|
pub price: f64,
|
|
pub origin: Option<String>,
|
|
pub location: Option<String>,
|
|
pub note: Option<String>,
|
|
}
|
|
|
|
#[post("/supply", data = "<form>")]
|
|
/// Route for supply action. Creates a new Transaction for the specified Item Variant.
|
|
pub async fn supply_route(
|
|
form: Json<SupplyForm>,
|
|
itemdb: &State<ItemDB>,
|
|
t: Token,
|
|
c: &State<Config>,
|
|
) -> FallibleApiResponse {
|
|
check_auth!(t, c);
|
|
|
|
let variant = itemdb
|
|
.get_item(&form.item)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(&form.variant)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
let transaction = variant
|
|
.supply(
|
|
form.price,
|
|
form.origin.as_deref(),
|
|
form.location.as_deref(),
|
|
form.note.as_deref(),
|
|
)
|
|
.await;
|
|
|
|
if let Some(hook) = &c.webhook {
|
|
if let Some(url) = &hook.transaction_added {
|
|
Webhook::send(url, &transaction.api().await).await;
|
|
}
|
|
}
|
|
|
|
Ok(json!({"uuid": transaction.id}))
|
|
}
|
|
|
|
#[get("/item/<item_id>/<variant_id>/supply")]
|
|
/// Returns a list of Transaction UUIDs for the Item Variant
|
|
pub async fn supply_log_route(
|
|
item_id: &str,
|
|
variant_id: &str,
|
|
itemdb: &State<ItemDB>,
|
|
t: Token,
|
|
c: &State<Config>,
|
|
) -> FallibleApiResponse {
|
|
check_auth!(t, c);
|
|
|
|
let variant = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(variant_id)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
let transactions = variant.supply_log().await;
|
|
|
|
Ok(json!(transactions))
|
|
}
|
|
|
|
#[get("/item/<item_id>/inventory?<origin>")]
|
|
/// Returns current active Transactions for Item
|
|
pub async fn inventory_route(
|
|
item_id: &str,
|
|
itemdb: &State<ItemDB>,
|
|
t: Token,
|
|
c: &State<Config>,
|
|
origin: Option<&str>,
|
|
) -> FallibleApiResponse {
|
|
check_auth!(t, c);
|
|
|
|
let item = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?;
|
|
|
|
let transactions = if let Some(origin) = origin {
|
|
item.inventory_by_origin(origin).await
|
|
} else {
|
|
item.inventory().await
|
|
};
|
|
|
|
Ok(json!(vec_to_api(&transactions).await))
|
|
}
|
|
|
|
#[get("/item/<item_id>/<variant_id>/inventory")]
|
|
/// Returns current active Transactions for Item Variant
|
|
pub async fn inventory_route_variant(
|
|
item_id: &str,
|
|
variant_id: &str,
|
|
itemdb: &State<ItemDB>,
|
|
t: Token,
|
|
c: &State<Config>,
|
|
) -> FallibleApiResponse {
|
|
check_auth!(t, c);
|
|
|
|
let variant = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(variant_id)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
let transactions = variant.inventory().await;
|
|
|
|
Ok(json!(vec_to_api(&transactions).await))
|
|
}
|
|
|
|
#[get("/item/<item_id>/<variant_id>/stat?<full>")]
|
|
/// Returns statistics for the Item Variant
|
|
pub async fn variant_stat_route(
|
|
item_id: &str,
|
|
variant_id: &str,
|
|
full: Option<&str>,
|
|
itemdb: &State<ItemDB>,
|
|
t: Token,
|
|
c: &State<Config>,
|
|
) -> FallibleApiResponse {
|
|
check_auth!(t, c);
|
|
|
|
let variant = itemdb
|
|
.get_item(item_id)
|
|
.ok_or_else(item_does_not_exist_error)?
|
|
.variant(variant_id)
|
|
.ok_or_else(variant_does_not_exist_error)?;
|
|
|
|
Ok(variant
|
|
.stat(full.map(|x| x == "1" || x == "true").unwrap_or(false))
|
|
.await)
|
|
}
|