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, pub location: Option, pub note: Option, } #[post("/supply", data = "
")] /// Route for supply action. Creates a new Transaction for the specified Item Variant. pub async fn supply_route( form: Json, itemdb: &State, t: Token, c: &State, ) -> 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///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, t: Token, c: &State, ) -> 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//inventory?")] /// Returns current active Transactions for Item pub async fn inventory_route( item_id: &str, itemdb: &State, t: Token, c: &State, 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///inventory")] /// Returns current active Transactions for Item Variant pub async fn inventory_route_variant( item_id: &str, variant_id: &str, itemdb: &State, t: Token, c: &State, ) -> 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///stat?")] /// Returns statistics for the Item Variant pub async fn variant_stat_route( item_id: &str, variant_id: &str, full: Option<&str>, itemdb: &State, t: Token, c: &State, ) -> 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) }