feat: multi-ticket purchases as N rows sharing one payment_hash
Replaces the previous "one row, N seats via extra.quantity" model
with proper one-row-per-attendee semantics. Each attendee gets a
unique scannable id; the door PUT /register/{ticket_id} marks
them registered independently — so a buyer can purchase 3 tickets,
hand 2 QRs to friends arriving separately, and each attendee can
enter on their own schedule.
Schema (migrations_fork.py m002):
- ticket.payment_hash: new TEXT column shared across all rows of
a multi-ticket purchase. Backfilled `payment_hash = id` for
pre-migration rows (id WAS the payment_hash by invariant).
Wire:
- TicketPaymentRequest grows `ticket_ids: list[str]` so the
webapp gets every scannable id back in the create response.
- POST /tickets/{event_id}/{payment_hash} polling endpoint now
reports `ticket_ids` (every row) + keeps `ticket_id` for
back-compat.
- api_ticket_create loops quantity times; the first row reuses
payment_hash as id (preserves legacy `id == payment_hash`
invariant for single-ticket purchases), the rest get
urlsafe_short_hash() uuids.
Payment flow:
- on_invoice_paid fetches all rows by payment_hash and marks each
paid via set_ticket_paid, which now increments event.sold by 1
per row (was N per row via extra.quantity — simpler now). The
per-event asyncio lock still serializes counter + republish so
concurrent multi-ticket purchases for the same event don't
reorder the published Nostr state.
- Each paid row triggers its own send_ticket_notification_in_
background call — no-op for buyers without nostr_identifier /
email, useful when the buyer set those on the row.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
36568d3eee
commit
59068fe09d
5 changed files with 157 additions and 45 deletions
33
tasks.py
33
tasks.py
|
|
@ -4,7 +4,7 @@ from lnbits.core.models import Payment
|
|||
from lnbits.tasks import register_invoice_listener
|
||||
from loguru import logger
|
||||
|
||||
from .crud import get_ticket
|
||||
from .crud import get_ticket, get_tickets_by_payment_hash
|
||||
from .models import Ticket
|
||||
from .services import send_ticket_notification_in_background, set_ticket_paid
|
||||
|
||||
|
|
@ -37,13 +37,32 @@ async def on_invoice_paid(payment: Payment) -> None:
|
|||
if not payment.extra or "events" != payment.extra.get("tag"):
|
||||
return
|
||||
|
||||
ticket = await get_ticket(payment.payment_hash)
|
||||
if not ticket:
|
||||
logger.warning(f"Ticket for payment {payment.payment_hash} not found.")
|
||||
# Multi-ticket purchases land as N rows sharing this payment_hash;
|
||||
# each one needs to be marked paid + counted against capacity, and
|
||||
# each gets its own buyer notification (mostly a no-op when all
|
||||
# rows are owned by the same buyer, but cheap and consistent).
|
||||
tickets = await get_tickets_by_payment_hash(payment.payment_hash)
|
||||
if not tickets:
|
||||
# Backstop for any legacy row created before the payment_hash
|
||||
# column was populated by the migration backfill.
|
||||
legacy = await get_ticket(payment.payment_hash)
|
||||
if legacy:
|
||||
tickets = [legacy]
|
||||
|
||||
if not tickets:
|
||||
logger.warning(f"No tickets for payment {payment.payment_hash}.")
|
||||
return
|
||||
|
||||
ticket = await set_ticket_paid(ticket)
|
||||
send_ticket_notification_in_background(ticket)
|
||||
paid_tickets: list[Ticket] = []
|
||||
for ticket in tickets:
|
||||
paid_tickets.append(await set_ticket_paid(ticket))
|
||||
|
||||
for paid_ticket in paid_tickets:
|
||||
send_ticket_notification_in_background(paid_ticket)
|
||||
|
||||
# Wake up the WebSocket / poll listeners. Forward the first paid
|
||||
# ticket so the existing single-ticket subscribers still work; the
|
||||
# webapp re-fetches all ids via the polling endpoint anyway.
|
||||
if payment_listeners.get(payment.payment_hash):
|
||||
for paid_ticket_queue in payment_listeners[payment.payment_hash]:
|
||||
paid_ticket_queue.put_nowait(ticket)
|
||||
paid_ticket_queue.put_nowait(paid_tickets[0])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue