lnbits-legend/lnbits/extensions/market/tasks.py

40 lines
988 B
Python
Raw Normal View History

2022-01-27 12:24:38 +00:00
import asyncio
2022-08-19 11:19:45 +01:00
from loguru import logger
2022-01-27 12:24:38 +00:00
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
2022-08-20 08:18:28 +01:00
from .crud import (
2023-01-04 21:08:28 +00:00
get_market_order_details,
get_market_order_invoiceid,
set_market_order_paid,
update_market_product_stock,
2022-08-20 08:18:28 +01:00
)
2022-08-19 11:19:45 +01:00
2022-01-27 12:24:38 +00:00
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue)
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
2023-01-04 21:08:28 +00:00
if payment.extra.get("tag") != "market":
2022-08-19 11:19:45 +01:00
return
2023-01-04 21:08:28 +00:00
order = await get_market_order_invoiceid(payment.payment_hash)
2022-08-19 11:19:45 +01:00
if not order:
logger.error("this should never happen", payment)
return
# set order as paid
2023-01-04 21:08:28 +00:00
await set_market_order_paid(payment.payment_hash)
2022-09-12 17:01:41 +01:00
2022-08-19 11:19:45 +01:00
# deduct items sold from stock
2023-01-04 21:08:28 +00:00
details = await get_market_order_details(order.id)
await update_market_product_stock(details)