From 18196bf22672ebb3c51ed8c898cd8b71549a08fb Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 16 Mar 2023 15:15:48 +0200 Subject: [PATCH] feat: do not issue invoice if insufficient products --- services.py | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/services.py b/services.py index 88926e0..4c762e0 100644 --- a/services.py +++ b/services.py @@ -23,6 +23,7 @@ from .models import ( Nostrable, Order, OrderExtra, + OrderItem, OrderStatusUpdate, PartialDirectMessage, PartialOrder, @@ -55,6 +56,13 @@ async def create_new_order( wallet_id = await get_wallet_for_product(data.items[0].product_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( wallet_id=wallet_id, amount=round(total_amount), @@ -134,21 +142,11 @@ async def update_products_for_order( merchant: Merchant, order: Order ) -> Tuple[bool, str]: product_ids = [i.product_id for i in order.items] - products: List[Product] = await get_products_by_ids(merchant.id, product_ids) - - for p in products: - required_quantity = next( - (i.quantity for i in order.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 + success, products, message = await compute_products_new_quantity( + merchant.id, product_ids, order.items + ) + if not success: + return success, message for p in products: product = await update_product_quantity(p.id, p.quantity) @@ -159,6 +157,29 @@ async def update_products_for_order( 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): try: type, *rest = json.loads(msg)