Browse Source

feat(core): delete expired payments

fee_issues
Eneko Illarramendi 5 years ago
parent
commit
d4c9043278
  1. 11
      lnbits/core/crud.py
  2. 30
      lnbits/core/migrations.py
  3. 5
      lnbits/core/models.py
  4. 8
      lnbits/core/views/api.py

11
lnbits/core/crud.py

@ -159,6 +159,17 @@ def get_wallet_payments(wallet_id: str, *, include_all_pending: bool = False) ->
return [Payment(**row) for row in rows]
def delete_wallet_payments_expired(wallet_id: str, *, seconds: int = 86400) -> None:
with open_db() as db:
db.execute(
"""
DELETE
FROM apipayments WHERE wallet = ? AND pending = 1 AND time < strftime('%s', 'now') - ?
""",
(wallet_id, seconds),
)
# payments
# --------

30
lnbits/core/migrations.py

@ -5,14 +5,17 @@ def m001_initial(db):
"""
Initial LNbits tables.
"""
db.execute("""
db.execute(
"""
CREATE TABLE IF NOT EXISTS accounts (
id TEXT PRIMARY KEY,
email TEXT,
pass TEXT
);
""")
db.execute("""
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS extensions (
user TEXT NOT NULL,
extension TEXT NOT NULL,
@ -20,8 +23,10 @@ def m001_initial(db):
UNIQUE (user, extension)
);
""")
db.execute("""
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS wallets (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
@ -29,8 +34,10 @@ def m001_initial(db):
adminkey TEXT NOT NULL,
inkey TEXT
);
""")
db.execute("""
"""
)
db.execute(
"""
CREATE TABLE IF NOT EXISTS apipayments (
payhash TEXT NOT NULL,
amount INTEGER NOT NULL,
@ -42,8 +49,10 @@ def m001_initial(db):
UNIQUE (wallet, payhash)
);
""")
db.execute("""
"""
)
db.execute(
"""
CREATE VIEW IF NOT EXISTS balances AS
SELECT wallet, COALESCE(SUM(s), 0) AS balance FROM (
SELECT wallet, SUM(amount) AS s -- incoming
@ -57,7 +66,8 @@ def m001_initial(db):
GROUP BY wallet
)
GROUP BY wallet;
""")
"""
)
def migrate():

5
lnbits/core/models.py

@ -39,6 +39,11 @@ class Wallet(NamedTuple):
return get_wallet_payments(self.id, include_all_pending=include_all_pending)
def delete_expired_payments(self, seconds: int = 86400) -> None:
from .crud import delete_wallet_payments_expired
delete_wallet_payments_expired(self.id, seconds=seconds)
class Payment(NamedTuple):
checking_id: str

8
lnbits/core/views/api.py

@ -12,6 +12,8 @@ from ..services import create_invoice, pay_invoice
@api_check_wallet_key("invoice")
def api_payments():
if "check_pending" in request.args:
g.wallet.delete_expired_payments()
for payment in g.wallet.get_payments(include_all_pending=True):
if payment.is_out:
payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
@ -74,13 +76,13 @@ def api_payment(checking_id):
try:
if payment.is_out:
is_paid = WALLET.get_payment_status(checking_id).paid
is_paid = not WALLET.get_payment_status(checking_id).pending
elif payment.is_in:
is_paid = WALLET.get_invoice_status(checking_id).paid
is_paid = not WALLET.get_invoice_status(checking_id).pending
except Exception:
return jsonify({"paid": False}), Status.OK
if is_paid is True:
if is_paid:
payment.set_pending(False)
return jsonify({"paid": True}), Status.OK

Loading…
Cancel
Save