Skip to content

Commit

Permalink
Fix ticket create (#13)
Browse files Browse the repository at this point in the history
* don't set ticket as paid on create
* use crud fn in tasks.py
* create ticket is unpaid by default
  • Loading branch information
talvasconcelos authored Aug 18, 2023
1 parent c8b31d8 commit 00f552c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
44 changes: 31 additions & 13 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,))
Expand Down
9 changes: 3 additions & 6 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
1 change: 0 additions & 1 deletion templates/events/display.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ <h5 class="q-my-none">{{ event_info }}</h5>

{% endblock %} {% block scripts %}
<script>
console.log('{{ form_costpword }}')
Vue.component(VueQrcode.name, VueQrcode)

new Vue({
Expand Down
3 changes: 3 additions & 0 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_ticket,
get_tickets,
reg_ticket,
set_ticket_paid,
update_event,
)
from .models import CreateEvent, CreateTicket
Expand Down Expand Up @@ -138,7 +139,9 @@ async def api_ticket_send_ticket(event_id, payment_hash):
)

payment = await get_standalone_payment(payment_hash)
assert payment
if not payment.pending and event.price_per_ticket * 1000 == payment.amount:
await set_ticket_paid(payment_hash)
return {"paid": True, "ticket_id": ticket.id}

return {"paid": False}
Expand Down

0 comments on commit 00f552c

Please sign in to comment.