0 is out of stock and nothing is available from now on
Some checks failed
CI / Check Style (push) Has been cancelled
CI / Run Clippy (push) Has been cancelled
CI / Run Tests (push) Has been cancelled

This commit is contained in:
Priec
2026-06-22 16:48:28 +02:00
parent 6828854f24
commit 681c88f85d
15 changed files with 140 additions and 39 deletions

View File

@@ -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));
}