[fix] wallet search on postgres (#2780)

* fix: wallet search for postgres
* reformat: make it easier to read
This commit is contained in:
Vlad Stan 2024-12-09 13:16:34 +02:00 committed by GitHub
parent f5f43e7668
commit fa71219ce7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from time import time from time import time
from typing import Optional from typing import Any, Optional
from uuid import uuid4 from uuid import uuid4
from lnbits.core.crud.extensions import get_user_active_extensions_ids from lnbits.core.crud.extensions import get_user_active_extensions_ids
@ -46,6 +46,20 @@ async def get_accounts(
filters: Optional[Filters[AccountFilters]] = None, filters: Optional[Filters[AccountFilters]] = None,
conn: Optional[Connection] = None, conn: Optional[Connection] = None,
) -> Page[AccountOverview]: ) -> Page[AccountOverview]:
where_clauses = []
values: dict[str, Any] = {}
# Make wallet filter explicit
wallet_filter = (
next((f for f in filters.filters if f.field == "wallet_id"), None)
if filters
else None
)
if filters and wallet_filter and wallet_filter.values:
where_clauses.append("wallets.id = :wallet_id")
values = {**values, "wallet_id": next(iter(wallet_filter.values.values()))}
filters.filters = [f for f in filters.filters if f.field != "wallet_id"]
return await (conn or db).fetch_page( return await (conn or db).fetch_page(
""" """
SELECT SELECT
@ -53,7 +67,6 @@ async def get_accounts(
accounts.username, accounts.username,
accounts.email, accounts.email,
accounts.pubkey, accounts.pubkey,
wallets.id as wallet_id,
SUM(COALESCE(( SUM(COALESCE((
SELECT balance FROM balances WHERE wallet_id = wallets.id SELECT balance FROM balances WHERE wallet_id = wallets.id
), 0)) as balance_msat, ), 0)) as balance_msat,
@ -69,8 +82,8 @@ async def get_accounts(
)) as last_payment )) as last_payment
FROM accounts LEFT JOIN wallets ON accounts.id = wallets.user FROM accounts LEFT JOIN wallets ON accounts.id = wallets.user
""", """,
[], where_clauses,
{}, values,
filters=filters, filters=filters,
model=AccountOverview, model=AccountOverview,
group_by=["accounts.id"], group_by=["accounts.id"],