get_wallet_payments with more fine-grained, explicit filters.
This commit is contained in:
parent
d09e624eb6
commit
fadddc995a
4 changed files with 34 additions and 14 deletions
|
|
@ -150,18 +150,38 @@ def get_wallet_payment(wallet_id: str, checking_id: str) -> Optional[Payment]:
|
||||||
return Payment(**row) if row else None
|
return Payment(**row) if row else None
|
||||||
|
|
||||||
|
|
||||||
def get_wallet_payments(wallet_id: str, *, include_all_pending: bool = False) -> List[Payment]:
|
def get_wallet_payments(
|
||||||
with open_db() as db:
|
wallet_id: str, *, complete: bool = False, pending: bool = False, outgoing: bool = False, incoming: bool = False
|
||||||
if include_all_pending:
|
) -> List[Payment]:
|
||||||
clause = "pending = 1"
|
"""
|
||||||
else:
|
Filters payments to be returned by complete | pending | outgoing | incoming.
|
||||||
clause = "((amount > 0 AND pending = 0) OR amount < 0)"
|
"""
|
||||||
|
|
||||||
|
clause = ""
|
||||||
|
if complete and pending:
|
||||||
|
clause += ""
|
||||||
|
elif complete:
|
||||||
|
clause += "AND ((amount > 0 AND pending = 0) OR amount < 0)"
|
||||||
|
elif pending:
|
||||||
|
clause += "AND pending = 1"
|
||||||
|
else:
|
||||||
|
raise TypeError("at least one of [complete, pending] must be True.")
|
||||||
|
|
||||||
|
if outgoing and incoming:
|
||||||
|
clause += ""
|
||||||
|
elif outgoing:
|
||||||
|
clause += "AND amount < 0"
|
||||||
|
elif incoming:
|
||||||
|
clause += "AND amount > 0"
|
||||||
|
else:
|
||||||
|
raise TypeError("at least one of [outgoing, incoming] must be True.")
|
||||||
|
|
||||||
|
with open_db() as db:
|
||||||
rows = db.fetchall(
|
rows = db.fetchall(
|
||||||
f"""
|
f"""
|
||||||
SELECT payhash as checking_id, amount, fee, pending, memo, time
|
SELECT payhash as checking_id, amount, fee, pending, memo, time
|
||||||
FROM apipayments
|
FROM apipayments
|
||||||
WHERE wallet = ? AND {clause}
|
WHERE wallet = ? {clause}
|
||||||
ORDER BY time DESC
|
ORDER BY time DESC
|
||||||
""",
|
""",
|
||||||
(wallet_id,),
|
(wallet_id,),
|
||||||
|
|
|
||||||
|
|
@ -34,10 +34,12 @@ class Wallet(NamedTuple):
|
||||||
|
|
||||||
return get_wallet_payment(self.id, checking_id)
|
return get_wallet_payment(self.id, checking_id)
|
||||||
|
|
||||||
def get_payments(self, *, include_all_pending: bool = False) -> List["Payment"]:
|
def get_payments(
|
||||||
|
self, *, complete: bool = True, pending: bool = False, outgoing: bool = True, incoming: bool = True
|
||||||
|
) -> List["Payment"]:
|
||||||
from .crud import get_wallet_payments
|
from .crud import get_wallet_payments
|
||||||
|
|
||||||
return get_wallet_payments(self.id, include_all_pending=include_all_pending)
|
return get_wallet_payments(self.id, complete=complete, pending=pending, outgoing=outgoing, incoming=incoming)
|
||||||
|
|
||||||
def delete_expired_payments(self, seconds: int = 86400) -> None:
|
def delete_expired_payments(self, seconds: int = 86400) -> None:
|
||||||
from .crud import delete_wallet_payments_expired
|
from .crud import delete_wallet_payments_expired
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ def api_payments():
|
||||||
if "check_pending" in request.args:
|
if "check_pending" in request.args:
|
||||||
g.wallet.delete_expired_payments()
|
g.wallet.delete_expired_payments()
|
||||||
|
|
||||||
for payment in g.wallet.get_payments(include_all_pending=True):
|
for payment in g.wallet.get_payments(pending=True):
|
||||||
if payment.is_out:
|
if payment.is_out:
|
||||||
payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
|
payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
from lnbits.db import open_ext_db
|
from lnbits.db import open_ext_db
|
||||||
from lnbits.settings import WALLET
|
|
||||||
from .models import Users, Wallets
|
from .models import Users, Wallets
|
||||||
from typing import List, Optional, Union
|
from typing import Optional
|
||||||
|
|
||||||
from ...core.crud import (
|
from ...core.crud import (
|
||||||
create_account,
|
create_account,
|
||||||
get_user,
|
get_user,
|
||||||
update_user_extension,
|
|
||||||
get_wallet_payments,
|
get_wallet_payments,
|
||||||
create_wallet,
|
create_wallet,
|
||||||
delete_wallet,
|
delete_wallet,
|
||||||
|
|
@ -103,7 +101,7 @@ def get_usermanager_wallets(user_id: str) -> Wallets:
|
||||||
|
|
||||||
|
|
||||||
def get_usermanager_wallet_transactions(wallet_id: str) -> Users:
|
def get_usermanager_wallet_transactions(wallet_id: str) -> Users:
|
||||||
return get_wallet_payments(wallet_id=wallet_id, include_all_pending=False)
|
return get_wallet_payments(wallet_id=wallet_id, complete=True, pending=False, outgoing=True, incoming=True)
|
||||||
|
|
||||||
|
|
||||||
def get_usermanager_wallet_balances(user_id: str) -> Users:
|
def get_usermanager_wallet_balances(user_id: str) -> Users:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue