From fa71219ce7d3a5320605eab8094ab16053629147 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Mon, 9 Dec 2024 13:16:34 +0200 Subject: [PATCH] [fix] wallet search on postgres (#2780) * fix: wallet search for postgres * reformat: make it easier to read --- lnbits/core/crud/users.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lnbits/core/crud/users.py b/lnbits/core/crud/users.py index 717e297ae..75e447b8c 100644 --- a/lnbits/core/crud/users.py +++ b/lnbits/core/crud/users.py @@ -1,6 +1,6 @@ from datetime import datetime, timezone from time import time -from typing import Optional +from typing import Any, Optional from uuid import uuid4 from lnbits.core.crud.extensions import get_user_active_extensions_ids @@ -46,6 +46,20 @@ async def get_accounts( filters: Optional[Filters[AccountFilters]] = None, conn: Optional[Connection] = None, ) -> 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( """ SELECT @@ -53,7 +67,6 @@ async def get_accounts( accounts.username, accounts.email, accounts.pubkey, - wallets.id as wallet_id, SUM(COALESCE(( SELECT balance FROM balances WHERE wallet_id = wallets.id ), 0)) as balance_msat, @@ -69,8 +82,8 @@ async def get_accounts( )) as last_payment FROM accounts LEFT JOIN wallets ON accounts.id = wallets.user """, - [], - {}, + where_clauses, + values, filters=filters, model=AccountOverview, group_by=["accounts.id"],