feat: do not issue invoice if insufficient products
This commit is contained in:
parent
b7dfa22026
commit
18196bf226
1 changed files with 36 additions and 15 deletions
51
services.py
51
services.py
|
|
@ -23,6 +23,7 @@ from .models import (
|
||||||
Nostrable,
|
Nostrable,
|
||||||
Order,
|
Order,
|
||||||
OrderExtra,
|
OrderExtra,
|
||||||
|
OrderItem,
|
||||||
OrderStatusUpdate,
|
OrderStatusUpdate,
|
||||||
PartialDirectMessage,
|
PartialDirectMessage,
|
||||||
PartialOrder,
|
PartialOrder,
|
||||||
|
|
@ -55,6 +56,13 @@ async def create_new_order(
|
||||||
wallet_id = await get_wallet_for_product(data.items[0].product_id)
|
wallet_id = await get_wallet_for_product(data.items[0].product_id)
|
||||||
assert wallet_id, "Missing wallet for order `{data.id}`"
|
assert wallet_id, "Missing wallet for order `{data.id}`"
|
||||||
|
|
||||||
|
product_ids = [i.product_id for i in data.items]
|
||||||
|
success, _, message = await compute_products_new_quantity(
|
||||||
|
merchant.id, product_ids, data.items
|
||||||
|
)
|
||||||
|
if not success:
|
||||||
|
return PaymentRequest(id=data.id, message=message, payment_options=[])
|
||||||
|
|
||||||
payment_hash, invoice = await create_invoice(
|
payment_hash, invoice = await create_invoice(
|
||||||
wallet_id=wallet_id,
|
wallet_id=wallet_id,
|
||||||
amount=round(total_amount),
|
amount=round(total_amount),
|
||||||
|
|
@ -134,21 +142,11 @@ async def update_products_for_order(
|
||||||
merchant: Merchant, order: Order
|
merchant: Merchant, order: Order
|
||||||
) -> Tuple[bool, str]:
|
) -> Tuple[bool, str]:
|
||||||
product_ids = [i.product_id for i in order.items]
|
product_ids = [i.product_id for i in order.items]
|
||||||
products: List[Product] = await get_products_by_ids(merchant.id, product_ids)
|
success, products, message = await compute_products_new_quantity(
|
||||||
|
merchant.id, product_ids, order.items
|
||||||
for p in products:
|
)
|
||||||
required_quantity = next(
|
if not success:
|
||||||
(i.quantity for i in order.items if i.product_id == p.id), None
|
return success, message
|
||||||
)
|
|
||||||
if not required_quantity:
|
|
||||||
return False, f"Product not found for order: {p.id}"
|
|
||||||
if p.quantity < required_quantity:
|
|
||||||
return (
|
|
||||||
False,
|
|
||||||
f"Quantity not sufficient for product: {p.id}. Required {required_quantity} but only have {p.quantity}",
|
|
||||||
)
|
|
||||||
|
|
||||||
p.quantity -= required_quantity
|
|
||||||
|
|
||||||
for p in products:
|
for p in products:
|
||||||
product = await update_product_quantity(p.id, p.quantity)
|
product = await update_product_quantity(p.id, p.quantity)
|
||||||
|
|
@ -159,6 +157,29 @@ async def update_products_for_order(
|
||||||
return True, "ok"
|
return True, "ok"
|
||||||
|
|
||||||
|
|
||||||
|
async def compute_products_new_quantity(
|
||||||
|
merchant_id: str, product_ids: List[str], items: List[OrderItem]
|
||||||
|
) -> Tuple[bool, List[Product], str]:
|
||||||
|
products: List[Product] = await get_products_by_ids(merchant_id, product_ids)
|
||||||
|
|
||||||
|
for p in products:
|
||||||
|
required_quantity = next(
|
||||||
|
(i.quantity for i in items if i.product_id == p.id), None
|
||||||
|
)
|
||||||
|
if not required_quantity:
|
||||||
|
return False, [], f"Product not found for order: {p.id}"
|
||||||
|
if p.quantity < required_quantity:
|
||||||
|
return (
|
||||||
|
False,
|
||||||
|
[],
|
||||||
|
f"Quantity not sufficient for product: {p.id}. Required {required_quantity} but only have {p.quantity}",
|
||||||
|
)
|
||||||
|
|
||||||
|
p.quantity -= required_quantity
|
||||||
|
|
||||||
|
return True, products, "ok"
|
||||||
|
|
||||||
|
|
||||||
async def process_nostr_message(msg: str):
|
async def process_nostr_message(msg: str):
|
||||||
try:
|
try:
|
||||||
type, *rest = json.loads(msg)
|
type, *rest = json.loads(msg)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue