update CLAUDE.md

This commit is contained in:
padreug 2025-11-10 16:35:03 +01:00
parent 87a3505376
commit 1b1d066d07
3 changed files with 292 additions and 87 deletions

View file

@ -464,8 +464,8 @@ async def api_get_user_entries(
reference = link_clean
break
# Get username from user ID (first 8 chars for display)
username = f"User-{user_id_match[:8]}" if user_id_match else None
# Look up actual username using helper function
username = await _get_username_from_user_id(user_id_match) if user_id_match else None
entry_data = {
"id": entry_id or e.get("entry_hash", "unknown"),
@ -506,6 +506,107 @@ async def api_get_user_entries(
}
async def _get_username_from_user_id(user_id: str) -> str:
"""
Helper function to get username from user_id, handling various formats.
Supports:
- Full UUID with dashes (36 chars): "375ec158-686c-4a21-b44d-a51cc90ef07d"
- Dashless UUID (32 chars): "375ec158686c4a21b44da51cc90ef07d"
- Partial ID (8 chars from account names): "375ec158"
Returns username or formatted fallback.
"""
from lnbits.core.crud.users import get_user
logger.info(f"[USERNAME] Called with: '{user_id}' (len={len(user_id) if user_id else 0})")
if not user_id:
return None
# Case 1: Already in standard UUID format (36 chars with dashes)
if len(user_id) == 36 and user_id.count('-') == 4:
logger.info(f"[USERNAME] Case 1: Full UUID format")
user = await get_user(user_id)
result = user.username if user and user.username else f"User-{user_id[:8]}"
logger.info(f"[USERNAME] Case 1 result: '{result}'")
return result
# Case 2: Dashless 32-char UUID - lookup via Castle user settings
elif len(user_id) == 32 and '-' not in user_id:
logger.info(f"[USERNAME] Case 2: Dashless UUID format - looking up in Castle user settings")
try:
# Get all Castle users (which have full user_ids)
user_settings = await get_all_user_wallet_settings()
# Convert dashless to dashed format for comparison
user_id_with_dashes = f"{user_id[0:8]}-{user_id[8:12]}-{user_id[12:16]}-{user_id[16:20]}-{user_id[20:32]}"
logger.info(f"[USERNAME] Converted to dashed format: {user_id_with_dashes}")
# Find matching user
for setting in user_settings:
if setting.id == user_id_with_dashes:
logger.info(f"[USERNAME] Found matching user in Castle settings")
# Get username from LNbits
user = await get_user(setting.id)
result = user.username if user and user.username else f"User-{user_id[:8]}"
logger.info(f"[USERNAME] Case 2 result (found): '{result}'")
return result
# No matching user found
logger.info(f"[USERNAME] No matching user found in Castle settings")
result = f"User-{user_id[:8]}"
logger.info(f"[USERNAME] Case 2 result (not found): '{result}'")
return result
except Exception as e:
logger.error(f"Error looking up user by dashless UUID {user_id}: {e}")
result = f"User-{user_id[:8]}"
return result
# Case 3: Partial ID (8 chars from account name) - lookup via Castle user settings
elif len(user_id) == 8:
logger.info(f"[USERNAME] Case 3: Partial ID format - looking up in Castle user settings")
try:
# Get all Castle users (which have full user_ids)
user_settings = await get_all_user_wallet_settings()
# Find matching user by first 8 chars
for setting in user_settings:
if setting.id.startswith(user_id):
logger.info(f"[USERNAME] Found full user_id: {setting.id}")
# Now get username from LNbits with full ID
user = await get_user(setting.id)
result = user.username if user and user.username else f"User-{user_id}"
logger.info(f"[USERNAME] Case 3 result (found): '{result}'")
return result
# No matching user found in Castle settings
logger.info(f"[USERNAME] No matching user found in Castle settings")
result = f"User-{user_id}"
logger.info(f"[USERNAME] Case 3 result (not found): '{result}'")
return result
except Exception as e:
logger.error(f"Error looking up user by partial ID {user_id}: {e}")
result = f"User-{user_id}"
return result
# Case 4: Unknown format - try as-is and fall back
else:
logger.info(f"[USERNAME] Case 4: Unknown format - trying as-is")
try:
user = await get_user(user_id)
result = user.username if user and user.username else f"User-{user_id[:8]}"
logger.info(f"[USERNAME] Case 4 result: '{result}'")
return result
except Exception as e:
logger.info(f"[USERNAME] Case 4 exception: {e}")
result = f"User-{user_id[:8]}"
logger.info(f"[USERNAME] Case 4 fallback result: '{result}'")
return result
@castle_api_router.get("/api/v1/entries/pending")
async def api_get_pending_entries(
wallet: WalletTypeInfo = Depends(require_admin_key),
@ -516,7 +617,6 @@ 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:
@ -552,9 +652,13 @@ async def api_get_pending_entries(
# Extract user ID from metadata or account names
user_id = None
entry_meta = e.get("meta", {})
logger.info(f"[EXTRACT] Entry metadata keys: {list(entry_meta.keys())}")
logger.info(f"[EXTRACT] Entry metadata: {entry_meta}")
if "user-id" in entry_meta:
user_id = entry_meta["user-id"]
logger.info(f"[EXTRACT] Found user-id in metadata: {user_id}")
else:
logger.info(f"[EXTRACT] No user-id in metadata, checking account names")
# Try to extract from account names in postings
for posting in e.get("postings", []):
account = posting.get("account", "")
@ -563,13 +667,11 @@ async def api_get_pending_entries(
parts = account.split("User-")
if len(parts) > 1:
user_id = parts[1] # Short ID after User-
logger.info(f"[EXTRACT] Extracted user_id from account name: {user_id}")
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]}"
# Look up username using helper function
username = await _get_username_from_user_id(user_id) if user_id else None
# Extract amount from postings (sum of absolute values / 2)
amount_sats = 0
@ -1243,17 +1345,15 @@ async def api_get_all_balances(
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> list[dict]:
"""Get all user balances (admin/super user only) from Fava/Beancount"""
from lnbits.core.crud.users import get_user
from .fava_client import get_fava_client
fava = get_fava_client()
balances = await fava.get_all_user_balances()
# Enrich with username information
# Enrich with username information using helper function
result = []
for balance in balances:
user = await get_user(balance["user_id"])
username = user.username if user and user.username else balance["user_id"][:16] + "..."
username = await _get_username_from_user_id(balance["user_id"])
result.append({
"user_id": balance["user_id"],