Refactors Castle user retrieval

Simplifies the API endpoint for retrieving Castle users.

Instead of gathering users from various sources (accounts,
permissions, equity), it now focuses on users who have configured
their wallet settings, streamlining the process and aligning with
the intended use case.
This commit is contained in:
padreug 2025-11-07 23:06:37 +01:00
parent d6a1c6e5b3
commit 7752b41e06

View file

@ -1344,58 +1344,28 @@ async def api_get_castle_users(
wallet: WalletTypeInfo = Depends(require_admin_key), wallet: WalletTypeInfo = Depends(require_admin_key),
) -> list[dict]: ) -> list[dict]:
""" """
Get all users who have Castle accounts (receivables, payables, equity, or permissions). Get all users who have configured their wallet in Castle.
This returns only users who are actively using Castle, not all LNbits users. These are users who can interact with Castle (submit expenses, receive permissions, etc.).
Admin only. Admin only.
""" """
from lnbits.core.crud.users import get_user from lnbits.core.crud.users import get_user
from .crud import get_all_equity_eligible_users
# Get all user-specific accounts (Receivable/Payable/Equity) # Get all users who have configured their wallet
all_accounts = await get_all_accounts() user_settings = await get_all_user_wallet_settings()
user_accounts = [acc for acc in all_accounts if acc.user_id is not None]
# Get all users who have permissions
all_permissions = []
for account in all_accounts:
account_perms = await get_account_permissions(account.id)
all_permissions.extend(account_perms)
# Get all equity-eligible users
equity_users = await get_all_equity_eligible_users()
# Collect unique user IDs
user_ids = set()
# Add users with accounts
for acc in user_accounts:
user_ids.add(acc.user_id)
# Add users with permissions
for perm in all_permissions:
user_ids.add(perm.user_id)
# Add equity-eligible users
for equity in equity_users:
user_ids.add(equity.user_id)
# Build user list with enriched data
users = [] users = []
for user_id in user_ids: for setting in user_settings:
# Get user details from core # Get user details from core
user = await get_user(user_id) user = await get_user(setting.id)
# Use username if available, otherwise use user_id # Use username if available, otherwise use user_id
username = user.username if user and user.username else None username = user.username if user and user.username else None
# Get user's wallet setting if exists
user_wallet = await get_user_wallet(user_id)
users.append({ users.append({
"id": user_id, "id": setting.id,
"user_id": user_id, # Compatibility with existing code "user_id": setting.id, # Compatibility with existing code
"username": username, "username": username,
"user_wallet_id": user_wallet.user_wallet_id if user_wallet else None, "user_wallet_id": setting.user_wallet_id,
}) })
# Sort by username (None values last) # Sort by username (None values last)