diff --git a/crud.py b/crud.py index 00c7b72..f347936 100644 --- a/crud.py +++ b/crud.py @@ -9,34 +9,52 @@ async def create_ticket( - payment_hash: str, wallet: str, event: str, name: str, email: str -) -> Ticket: + payment_hash: str, wallet: str, event: str, name: str, email: str) -> Ticket: await db.execute( """ INSERT INTO events.ticket (id, wallet, event, name, email, registered, paid) VALUES (?, ?, ?, ?, ?, ?, ?) """, - (payment_hash, wallet, event, name, email, False, True), + (payment_hash, wallet, event, name, email, False, False), ) - # UPDATE EVENT DATA ON SOLD TICKET - eventdata = await get_event(event) - assert eventdata, "Couldn't get event from ticket being paid" - sold = eventdata.sold + 1 - amount_tickets = eventdata.amount_tickets - 1 + ticket = await get_ticket(payment_hash) + assert ticket, "Newly created ticket couldn't be retrieved" + return ticket + +async def set_ticket_paid(payment_hash: str) -> Ticket: + ticket = await get_ticket(payment_hash) + assert ticket, "Ticket couldn't be retrieved" + await db.execute( """ - UPDATE events.events - SET sold = ?, amount_tickets = ? + UPDATE events.ticket + SET paid = ? WHERE id = ? """, - (sold, amount_tickets, event), + (True, ticket.id), ) + + await update_event_sold(ticket.event) - ticket = await get_ticket(payment_hash) - assert ticket, "Newly created ticket couldn't be retrieved" return ticket +async def update_event_sold(event_id: str): + event = await get_event(event_id) + assert event, "Couldn't get event from ticket being paid" + sold = event.sold + 1 + amount_tickets = event.amount_tickets - 1 + await db.execute( + """ + UPDATE events.events + SET sold = ?, amount_tickets = ? + WHERE id = ? + """, + (sold, amount_tickets, event_id), + ) + + return + async def get_ticket(payment_hash: str) -> Optional[Ticket]: row = await db.fetchone("SELECT * FROM events.ticket WHERE id = ?", (payment_hash,)) diff --git a/tasks.py b/tasks.py index 68a3908..d2ed732 100644 --- a/tasks.py +++ b/tasks.py @@ -4,8 +4,7 @@ from lnbits.helpers import get_current_extension_name from lnbits.tasks import register_invoice_listener -from .models import CreateTicket -from .views_api import api_ticket_send_ticket +from .crud import set_ticket_paid async def wait_for_paid_invoices(): @@ -25,8 +24,6 @@ async def on_invoice_paid(payment: Payment) -> None: and payment.extra.get("name") and payment.extra.get("email") ): - await api_ticket_send_ticket( - payment.memo, - payment.payment_hash, - ) + + await set_ticket_paid(payment.payment_hash) return diff --git a/templates/events/display.html b/templates/events/display.html index 45c2aca..74fd871 100644 --- a/templates/events/display.html +++ b/templates/events/display.html @@ -86,7 +86,6 @@
{{ event_info }}
{% endblock %} {% block scripts %}