update product stock tests

This commit is contained in:
Tiago vasconcelos 2022-08-19 13:24:18 +01:00
parent a5c6b1135c
commit 7c8e3de261
3 changed files with 26 additions and 18 deletions

View file

@ -1,10 +1,7 @@
import re
from base64 import urlsafe_b64encode
from typing import List, Optional, Union
from uuid import uuid4
import httpx
# from lnbits.db import open_ext_db
from lnbits.db import SQLITE
from lnbits.helpers import urlsafe_short_hash
@ -291,6 +288,22 @@ async def set_diagonalley_order_paid(payment_hash: str) -> Orders:
(payment_hash,),
)
async def update_diagonalley_product_stock(products):
q = "\n".join([f"""WHEN id='{p["product_id"]}' THEN {p["quantity"]}""" for p in products])
v = ",".join(["?"] * len(products))
await db.execute(
f"""
UPDATE diagonalley.products
SET quantity=(CASE
{q}
END)
WHERE id IN ({v});
""",
(*[p["product_id"] for p in products],)
)
async def get_diagonalley_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

View file

@ -30,19 +30,6 @@ async def on_invoice_paid(payment: Payment) -> None:
await set_diagonalley_order_paid(payment.payment_hash)
# deduct items sold from stock
# TODO
"""
if "lnticket" != payment.extra.get("tag"):
# not a lnticket invoice
return
ticket = await get_ticket(payment.checking_id)
if not ticket:
print("this should never happen", payment)
return
await payment.set_pending(False)
await set_ticket_paid(payment.payment_hash)
"""

View file

@ -17,6 +17,7 @@ from .crud import (
get_diagonalley_stall,
get_diagonalley_zone,
get_diagonalley_zones,
update_diagonalley_product_stock,
)
templates = Jinja2Templates(directory="templates")
@ -31,6 +32,13 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
@diagonalley_ext.get("/{stall_id}", response_class=HTMLResponse)
async def display(request: Request, stall_id):
# test_qty = 99
# test = [
# {"product_id": "55vpVjWAuQExHsJxT28MYe", "quantity": test_qty},
# {"product_id": "f2eGNsEWgbLJbfAApd3Jw5", "quantity": test_qty},
# {"product_id": "FVqZLZdemWCsiqe9gafvsC", "quantity": test_qty},
# ]
# await update_diagonalley_product_stock(test)
stall = await get_diagonalley_stall(stall_id)
products = await get_diagonalley_products(stall_id)
zones = []