From f3ddc0a9ea309daeff97e1c7146a0c59087115cf Mon Sep 17 00:00:00 2001 From: Ben White Date: Sun, 24 Dec 2023 16:59:49 +1300 Subject: [PATCH] feat: use structure for product status update --- src/methods/product/variant.rs | 4 ++++ src/methods/transaction/handlers.rs | 23 ++++++----------------- src/methods/transaction/structs.rs | 22 ++++++++++++++-------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/methods/product/variant.rs b/src/methods/product/variant.rs index a779929..635a7f6 100644 --- a/src/methods/product/variant.rs +++ b/src/methods/product/variant.rs @@ -119,6 +119,8 @@ pub struct PromotionInput { #[cfg(feature = "types")] #[derive(Deserialize, Serialize, Clone, JsonSchema)] +#[serde(tag = "type", content = "value")] +#[serde(rename_all = "lowercase")] pub enum PromotionBuy { // This(quantity), Specific((id, quantity)), Any(quantity) Specific((String, f32)), @@ -128,6 +130,8 @@ pub enum PromotionBuy { #[cfg(feature = "types")] #[derive(Deserialize, Serialize, Clone, JsonSchema)] +#[serde(tag = "type", content = "value")] +#[serde(rename_all = "lowercase")] pub enum PromotionGet { /// `SoloThis(discount)`
/// *Represents the individual product.*

diff --git a/src/methods/transaction/handlers.rs b/src/methods/transaction/handlers.rs index f7d60ae..6084d67 100644 --- a/src/methods/transaction/handlers.rs +++ b/src/methods/transaction/handlers.rs @@ -12,7 +12,7 @@ use super::{Transaction, TransactionInit, TransactionInput}; use crate::methods::employee::Action; use crate::methods::{cookie_status_wrapper, Error, ErrorResponse, QuantityAlterationIntent}; use crate::pool::Db; -use crate::{apply_discount, check_permissions, Order, OrderStatus, PickStatus, TransactionType}; +use crate::{apply_discount, check_permissions, Order, OrderStatus, ProductStatusUpdate, TransactionType}; pub fn documented_routes(settings: &OpenApiSettings) -> (Vec, OpenApi) { openapi_get_routes_spec![ @@ -183,35 +183,24 @@ async fn update_order_status( } #[openapi(tag = "Transaction")] -#[post("/status/product///", data = "")] +#[post("/status/product", data = "")] async fn update_product_status( conn: Connection, - refer: &str, - pid: &str, - iid: &str, - status: &str, + data: Validated>, cookies: &CookieJar<'_>, ) -> Result, Error> { let db = conn.into_inner(); + let data = data.clone().0.into_inner(); let session = cookie_status_wrapper(&db, cookies).await?; check_permissions!(session.clone(), Action::ModifyTransaction); - let tsn = Transaction::fetch_by_ref(refer, session.clone(), &db) + let tsn = Transaction::fetch_by_ref(&data.transaction_id.clone(), session.clone(), &db) .await .unwrap(); let id = tsn.get(0).unwrap().id.as_str(); - let product_status: PickStatus = match status { - "picked" => PickStatus::Picked, - "pending" => PickStatus::Pending, - "failed" => PickStatus::Failed, - "uncertain" => PickStatus::Uncertain, - "processing" => PickStatus::Processing, - _ => return Err(ErrorResponse::input_error()), - }; - - match Transaction::update_product_status(id, refer, pid, iid, product_status, session, &db).await + match Transaction::update_product_status(id, data, session, &db).await { Ok(res) => Ok(Json(res)), Err(_) => Err(ErrorResponse::input_error()), diff --git a/src/methods/transaction/structs.rs b/src/methods/transaction/structs.rs index feff053..bcf999a 100644 --- a/src/methods/transaction/structs.rs +++ b/src/methods/transaction/structs.rs @@ -159,6 +159,15 @@ pub struct TransactionInit { pub kiosk: Id, } +#[cfg(feature = "types")] +#[derive(Deserialize, Clone, JsonSchema, Validate)] +pub struct ProductStatusUpdate { + pub transaction_id: String, + pub product_purchase_id: String, + pub product_instance_id: String, + pub new_status: PickStatus +} + #[cfg(feature = "methods")] impl Transaction { pub async fn insert( @@ -405,10 +414,7 @@ impl Transaction { pub async fn update_product_status( id: &str, - refer: &str, - pid: &str, - iid: &str, - status: PickStatus, + update: ProductStatusUpdate, session: Session, db: &DbConn, ) -> Result { @@ -419,24 +425,24 @@ impl Transaction { .products .into_iter() .map(|mut v| { - if v.reference == refer { + if v.reference == update.transaction_id { let new_products = v .products .into_iter() .map(|mut p| { - if p.id == pid { + if p.id == update.product_purchase_id { p.instances = p .instances .into_iter() .map(|mut i| { - if i.id == iid { + if i.id == update.product_instance_id { i.fulfillment_status.pick_history.push(History { item: i.fulfillment_status.pick_status, reason: "Standard Update Bump".to_string(), timestamp: i.fulfillment_status.last_updated, }); i.fulfillment_status.last_updated = Utc::now(); - i.fulfillment_status.pick_status = status.clone(); + i.fulfillment_status.pick_status = update.new_status.clone(); } i