From 87a350537640c739eae1a531b84c0c672e0f41ae Mon Sep 17 00:00:00 2001 From: padreug Date: Mon, 10 Nov 2025 16:21:21 +0100 Subject: [PATCH] Enriches journal entries with usernames Enhances the journal entries API to include username information. This is achieved by extracting the user ID from transaction metadata or account names and retrieving the corresponding username. A default username is provided if the user is not found. The pending entries API is updated with the same functionality. --- views_api.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/views_api.py b/views_api.py index 7fb5644..a020cfa 100644 --- a/views_api.py +++ b/views_api.py @@ -280,14 +280,51 @@ async def api_get_journal_entries(limit: int = 100) -> list[dict]: """ Get all journal entries from Fava/Beancount. - Returns all transactions in reverse chronological order. + Returns all transactions in reverse chronological order with username enrichment. """ + from lnbits.core.crud.users import get_user from .fava_client import get_fava_client fava = get_fava_client() - transactions = await fava.query_transactions(limit=limit) + all_entries = await fava.get_journal_entries() - return transactions + # Filter to transactions only and enrich with username + enriched_entries = [] + for e in all_entries: + if e.get("t") != "Transaction": + continue + + # Extract user ID from metadata or account names + user_id = None + entry_meta = e.get("meta", {}) + if "user-id" in entry_meta: + user_id = entry_meta["user-id"] + else: + # Try to extract from account names in postings + for posting in e.get("postings", []): + account = posting.get("account", "") + if "User-" in account: + parts = account.split("User-") + if len(parts) > 1: + user_id = parts[1] + break + + # Look up username + username = None + if user_id: + user = await get_user(user_id) + username = user.username if user and user.username else f"User-{user_id[:8]}" + + # Add username to entry + enriched_entry = dict(e) + enriched_entry["user_id"] = user_id + enriched_entry["username"] = username + enriched_entries.append(enriched_entry) + + if len(enriched_entries) >= limit: + break + + return enriched_entries @castle_api_router.get("/api/v1/entries/user") @@ -479,6 +516,7 @@ async def api_get_pending_entries( Returns transactions with flag='!' from Fava/Beancount. """ from lnbits.settings import settings as lnbits_settings + from lnbits.core.crud.users import get_user from .fava_client import get_fava_client if wallet.wallet.user != lnbits_settings.super_user: @@ -511,6 +549,28 @@ async def api_get_pending_entries( entry_id = parts[-1] break + # Extract user ID from metadata or account names + user_id = None + entry_meta = e.get("meta", {}) + if "user-id" in entry_meta: + user_id = entry_meta["user-id"] + else: + # Try to extract from account names in postings + for posting in e.get("postings", []): + account = posting.get("account", "") + if "User-" in account: + # Extract user ID from account name (e.g., "Liabilities:Payable:User-abc123") + parts = account.split("User-") + if len(parts) > 1: + user_id = parts[1] # Short ID after User- + break + + # Look up username + username = None + if user_id: + user = await get_user(user_id) + username = user.username if user and user.username else f"User-{user_id[:8]}" + # Extract amount from postings (sum of absolute values / 2) amount_sats = 0 fiat_amount = None @@ -560,6 +620,8 @@ async def api_get_pending_entries( "tags": e.get("tags", []), "links": links, "amount": amount_sats, + "user_id": user_id, + "username": username, } # Add fiat info if available