diff --git a/lnbits/extensions/events/config.json.example b/lnbits/extensions/events/config.json similarity index 100% rename from lnbits/extensions/events/config.json.example rename to lnbits/extensions/events/config.json diff --git a/lnbits/extensions/events/crud.py b/lnbits/extensions/events/crud.py index b35a4bff..8ac0e40c 100644 --- a/lnbits/extensions/events/crud.py +++ b/lnbits/extensions/events/crud.py @@ -9,33 +9,46 @@ from .models import Tickets, Events #######TICKETS######## -def create_ticket(wallet: str, event: str, name: str, email: str) -> Tickets: +def create_ticket(checking_id: str, wallet: str, event: str, name: str, email: str) -> Tickets: with open_ext_db("events") as db: - eventdata = get_event(event) - sold = eventdata.sold + 1 - amount_tickets = eventdata.amount_tickets - 1 - ticket_id = urlsafe_short_hash() db.execute( """ - INSERT INTO tickets (id, wallet, event, name, email, registered) - VALUES (?, ?, ?, ?, ?, ?) + INSERT INTO tickets (id, paid, wallet, event, name, email, registered) + VALUES (?, ?, ?, ?, ?, ?, ?) """, - (ticket_id, wallet, event, name, email, False), + (checking_id, False, wallet, event, name, email, False), ) + + return get_ticket(checking_id) + +def update_ticket(paid: bool, checking_id: str) -> Tickets: + with open_ext_db("events") as db: + db.execute( + """ + UPDATE tickets + SET paid = ? + WHERE id = ? + """, + (paid, checking_id), + ) + row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (checking_id,)) + eventdata = get_event(row[3]) + sold = eventdata.sold + 1 + amount_tickets = eventdata.amount_tickets - 1 db.execute( """ UPDATE events SET sold = ?, amount_tickets = ? WHERE id = ? """, - (sold, amount_tickets, event), + (sold, amount_tickets, row[3]), ) - return get_ticket(ticket_id) + return get_ticket(checking_id) -def get_ticket(ticket_id: str) -> Optional[Tickets]: +def get_ticket(checking_id: str) -> Optional[Tickets]: with open_ext_db("events") as db: - row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (ticket_id,)) + row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (checking_id,)) return Tickets(**row) if row else None @@ -51,9 +64,9 @@ def get_tickets(wallet_ids: Union[str, List[str]]) -> List[Tickets]: return [Tickets(**row) for row in rows] -def delete_ticket(ticket_id: str) -> None: +def delete_ticket(checking_id: str) -> None: with open_ext_db("events") as db: - db.execute("DELETE FROM tickets WHERE id = ?", (ticket_id,)) + db.execute("DELETE FROM tickets WHERE id = ?", (checking_id,)) diff --git a/lnbits/extensions/events/migrations.py b/lnbits/extensions/events/migrations.py index 523071e5..4d8a1890 100644 --- a/lnbits/extensions/events/migrations.py +++ b/lnbits/extensions/events/migrations.py @@ -24,6 +24,7 @@ def m001_initial(db): """ CREATE TABLE IF NOT EXISTS tickets ( id TEXT PRIMARY KEY, + paid BOOLEAN NOT NULL, wallet TEXT NOT NULL, event TEXT NOT NULL, name TEXT NOT NULL, diff --git a/lnbits/extensions/events/models.py b/lnbits/extensions/events/models.py index d2d60663..b27a96f9 100644 --- a/lnbits/extensions/events/models.py +++ b/lnbits/extensions/events/models.py @@ -16,6 +16,7 @@ class Events(NamedTuple): class Tickets(NamedTuple): id: str + paid: bool wallet: str event: str name: str diff --git a/lnbits/extensions/events/templates/events/display.html b/lnbits/extensions/events/templates/events/display.html index 04658c4d..59a06ef2 100644 --- a/lnbits/extensions/events/templates/events/display.html +++ b/lnbits/extensions/events/templates/events/display.html @@ -133,8 +133,15 @@ var self = this axios - .get('/events/api/v1/tickets/' + '{{ event_id }}/{{ event_price }}') - + .post( + '/events/api/v1/tickets/' + '{{ event_id }}/{{ event_price }}', + { + event: '{{ event_id }}', + event_name: '{{ event_name }}', + name: self.formDialog.data.name, + email: self.formDialog.data.email + } + ) .then(function (response) { self.paymentReq = response.data.payment_request self.paymentCheck = response.data.checking_id @@ -152,12 +159,7 @@ paymentChecker = setInterval(function () { axios - .post('/events/api/v1/tickets/' + self.paymentCheck, { - event: '{{ event_id }}', - event_name: '{{ event_name }}', - name: self.formDialog.data.name, - email: self.formDialog.data.email - }) + .get('/events/api/v1/tickets/' + self.paymentCheck) .then(function (res) { if (res.data.paid) { clearInterval(paymentChecker) diff --git a/lnbits/extensions/events/templates/events/register.html b/lnbits/extensions/events/templates/events/register.html index c7caea8c..dd4cc549 100644 --- a/lnbits/extensions/events/templates/events/register.html +++ b/lnbits/extensions/events/templates/events/register.html @@ -156,9 +156,9 @@ type: 'positive', message: 'Registered!' }) - self.tickets = response.data.map(function (obj) { - return mapEvents(obj) - }) + setTimeout(function () { + window.location.reload() + }, 2000) }) .catch(function (error) { LNbits.utils.notifyApiError(error) diff --git a/lnbits/extensions/events/views_api.py b/lnbits/extensions/events/views_api.py index e04ee8be..78ae44f3 100644 --- a/lnbits/extensions/events/views_api.py +++ b/lnbits/extensions/events/views_api.py @@ -7,7 +7,7 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.settings import WALLET from lnbits.extensions.events import events_ext -from .crud import create_ticket, get_ticket, get_tickets, delete_ticket, create_event, update_event, get_event, get_events, delete_event, get_event_tickets, reg_ticket +from .crud import create_ticket, update_ticket, get_ticket, get_tickets, delete_ticket, create_event, update_event, get_event, get_events, delete_event, get_event_tickets, reg_ticket #########Events########## @@ -86,11 +86,18 @@ def api_tickets(): return jsonify([ticket._asdict() for ticket in get_tickets(wallet_ids)]), HTTPStatus.OK +@events_ext.route("/api/v1/tickets//", methods=["POST"]) +@api_validate_post_request( + schema={ + "name": {"type": "string", "empty": False, "required": True}, + "email": {"type": "string", "empty": False, "required": True} + }) +def api_ticket_make_ticket(event_id, sats): -@events_ext.route("/api/v1/tickets//", methods=["GET"]) -def api_ticket_create(event_id, sats): event = get_event(event_id) + if not event: + return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND try: checking_id, payment_request = create_invoice( wallet_id=event.wallet, amount=int(sats), memo=f"#lnticket {event_id}" @@ -98,32 +105,27 @@ def api_ticket_create(event_id, sats): except Exception as e: return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR + ticket = create_ticket(checking_id=checking_id, wallet=event.wallet, event=event_id, **g.data) + + if not ticket: + return jsonify({"message": "LNTicket could not be fetched."}), HTTPStatus.NOT_FOUND + return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK - -@events_ext.route("/api/v1/tickets/", methods=["POST"]) -@api_validate_post_request( - schema={ - "event": {"type": "string", "empty": False, "required": True}, - "name": {"type": "string", "empty": False, "required": True}, - "email": {"type": "string", "empty": False, "required": True} - }) +@events_ext.route("/api/v1/tickets/", methods=["GET"]) def api_ticket_send_ticket(checking_id): - - event = get_event(g.data['event']) - if not event: - return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND + theticket = get_ticket(checking_id) try: is_paid = not WALLET.get_invoice_status(checking_id).pending except Exception: return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND if is_paid: - wallet = get_wallet(event.wallet) + wallet = get_wallet(theticket.wallet) payment = wallet.get_payment(checking_id) payment.set_pending(False) - ticket = create_ticket(wallet=event.wallet, **g.data) + ticket = update_ticket(paid=True, checking_id=checking_id) return jsonify({"paid": True, "ticket_id": ticket.id}), HTTPStatus.OK