Skip to content

Commit

Permalink
feat: use structure for product status update
Browse files Browse the repository at this point in the history
  • Loading branch information
bennjii committed Dec 24, 2023
1 parent 630151a commit f3ddc0a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/methods/product/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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)` <br />
/// *Represents the individual product.* <br /> <br />
Expand Down
23 changes: 6 additions & 17 deletions src/methods/transaction/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<rocket::Route>, OpenApi) {
openapi_get_routes_spec![
Expand Down Expand Up @@ -183,35 +183,24 @@ async fn update_order_status(
}

#[openapi(tag = "Transaction")]
#[post("/status/product/<refer>/<pid>/<iid>", data = "<status>")]
#[post("/status/product", data = "<data>")]
async fn update_product_status(
conn: Connection<Db>,
refer: &str,
pid: &str,
iid: &str,
status: &str,
data: Validated<Json<ProductStatusUpdate>>,
cookies: &CookieJar<'_>,
) -> Result<Json<Transaction>, 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()),
Expand Down
22 changes: 14 additions & 8 deletions src/methods/transaction/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<Transaction, DbErr> {
Expand All @@ -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
Expand Down

0 comments on commit f3ddc0a

Please sign in to comment.