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.
This commit is contained in:
padreug 2025-11-10 16:21:21 +01:00
parent 700beb6f7f
commit 87a3505376

View file

@ -280,14 +280,51 @@ async def api_get_journal_entries(limit: int = 100) -> list[dict]:
""" """
Get all journal entries from Fava/Beancount. 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 from .fava_client import get_fava_client
fava = 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") @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. Returns transactions with flag='!' from Fava/Beancount.
""" """
from lnbits.settings import settings as lnbits_settings from lnbits.settings import settings as lnbits_settings
from lnbits.core.crud.users import get_user
from .fava_client import get_fava_client from .fava_client import get_fava_client
if wallet.wallet.user != lnbits_settings.super_user: if wallet.wallet.user != lnbits_settings.super_user:
@ -511,6 +549,28 @@ async def api_get_pending_entries(
entry_id = parts[-1] entry_id = parts[-1]
break 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) # Extract amount from postings (sum of absolute values / 2)
amount_sats = 0 amount_sats = 0
fiat_amount = None fiat_amount = None
@ -560,6 +620,8 @@ async def api_get_pending_entries(
"tags": e.get("tags", []), "tags": e.get("tags", []),
"links": links, "links": links,
"amount": amount_sats, "amount": amount_sats,
"user_id": user_id,
"username": username,
} }
# Add fiat info if available # Add fiat info if available