refactor: extract handle_order_paid

This commit is contained in:
Vlad Stan 2023-03-08 14:45:32 +02:00
parent c0c737378b
commit 6c6cd861ce
2 changed files with 28 additions and 20 deletions

View file

@ -1,18 +1,23 @@
import json
from typing import Optional
from loguru import logger
from lnbits.core import create_invoice
from .crud import (
get_merchant_by_pubkey,
get_merchant_for_user,
get_order,
get_order_by_event_id,
get_products_by_ids,
get_wallet_for_product,
update_order_paid_status,
)
from .models import (
Nostrable,
Order,
OrderExtra,
OrderStatusUpdate,
PartialOrder,
PaymentOption,
PaymentRequest,
@ -78,3 +83,23 @@ async def sign_and_send_to_nostr(
await publish_nostr_event(event)
return event
async def handle_order_paid(order_id: str, merchant_pubkey: str):
try:
order = await update_order_paid_status(order_id, True)
assert order, f"Paid order cannot be found. Order id: {order_id}"
order_status = OrderStatusUpdate(
id=order_id, message="Payment received.", paid=True, shipped=order.shipped
)
merchant = await get_merchant_by_pubkey(merchant_pubkey)
assert merchant, f"Merchant cannot be found for order {order_id}"
dm_content = json.dumps(
order_status.dict(), separators=(",", ":"), ensure_ascii=False
)
dm_event = merchant.build_dm_event(dm_content, order.pubkey)
await publish_nostr_event(dm_event)
except Exception as ex:
logger.warning(ex)

View file

@ -3,6 +3,7 @@ import json
from asyncio import Queue
import httpx
import websocket
from loguru import logger
from websocket import WebSocketApp
@ -17,13 +18,12 @@ from .crud import (
get_merchant_by_pubkey,
get_public_keys_for_merchants,
get_wallet_for_product,
update_order_paid_status,
)
from .helpers import order_from_json
from .models import OrderStatusUpdate, PartialDirectMessage, PartialOrder
from .models import PartialDirectMessage, PartialOrder
from .nostr.event import NostrEvent
from .nostr.nostr_client import connect_to_nostrclient_ws, publish_nostr_event
from .services import handle_order_paid
async def wait_for_paid_invoices():
invoice_queue = Queue()
@ -46,24 +46,7 @@ async def on_invoice_paid(payment: Payment) -> None:
await handle_order_paid(order_id, merchant_pubkey)
async def handle_order_paid(order_id: str, merchant_pubkey: str):
try:
order = await update_order_paid_status(order_id, True)
assert order, f"Paid order cannot be found. Order id: {order_id}"
order_status = OrderStatusUpdate(
id=order_id, message="Payment received.", paid=True, shipped=order.shipped
)
merchant = await get_merchant_by_pubkey(merchant_pubkey)
assert merchant, f"Merchant cannot be found for order {order_id}"
dm_content = json.dumps(
order_status.dict(), separators=(",", ":"), ensure_ascii=False
)
dm_event = merchant.build_dm_event(dm_content, order.pubkey)
await publish_nostr_event(dm_event)
except Exception as ex:
logger.warning(ex)
async def subscribe_to_nostr_client(recieve_event_queue: Queue, send_req_queue: Queue):