0 is out of stock and nothing is available from now on
This commit is contained in:
@@ -65,11 +65,15 @@ pub async fn place(
|
||||
.one(&txn)
|
||||
.await?
|
||||
.ok_or_else(|| Error::BadRequest("a product is no longer available".to_string()))?;
|
||||
if variant.stock < *qty {
|
||||
return Err(Error::BadRequest(format!(
|
||||
"not enough stock for {}",
|
||||
product.name
|
||||
)));
|
||||
// Tracked variants can't oversell; untracked ones (stock = None) are
|
||||
// always available and never decremented.
|
||||
if let Some(on_hand) = variant.stock {
|
||||
if on_hand < *qty {
|
||||
return Err(Error::BadRequest(format!(
|
||||
"not enough stock for {}",
|
||||
product.name
|
||||
)));
|
||||
}
|
||||
}
|
||||
currency = product.currency.clone();
|
||||
// Snapshot the price the buyer actually pays — public sale or, for a
|
||||
@@ -78,9 +82,11 @@ pub async fn place(
|
||||
let unit_price_cents = pricing::price_variant(ctx, &variant, user).await?.price_cents;
|
||||
subtotal += unit_price_cents * i64::from(*qty);
|
||||
|
||||
let mut active = variant.clone().into_active_model();
|
||||
active.stock = Set(variant.stock - *qty);
|
||||
active.update(&txn).await?;
|
||||
if let Some(on_hand) = variant.stock {
|
||||
let mut active = variant.clone().into_active_model();
|
||||
active.stock = Set(Some(on_hand - *qty));
|
||||
active.update(&txn).await?;
|
||||
}
|
||||
|
||||
snapshots.push((product.id, variant.id, product.name, variant.label, unit_price_cents, *qty));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user