mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-22 06:21:53 +01:00
feat(core): delete expired payments
This commit is contained in:
parent
d03e472cc2
commit
d4c9043278
4 changed files with 41 additions and 13 deletions
|
@ -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
|
||||
# --------
|
||||
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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…
Add table
Reference in a new issue