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),
) -> list[dict]:
"""
Get all users who have Castle accounts (receivables, payables, equity, or permissions).
This returns only users who are actively using Castle, not all LNbits users.
Get all users who have configured their wallet in Castle.
These are users who can interact with Castle (submit expenses, receive permissions, etc.).
Admin only.
"""
from lnbits.core.crud.users import get_user
from .crud import get_all_equity_eligible_users
# Get all user-specific accounts (Receivable/Payable/Equity)
all_accounts = await get_all_accounts()
user_accounts = [acc for acc in all_accounts if acc.user_id is not None]
# Get all users who have configured their wallet
user_settings = await get_all_user_wallet_settings()
# 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 = []
for user_id in user_ids:
for setting in user_settings:
# 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
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({
"id": user_id,
"user_id": user_id, # Compatibility with existing code
"id": setting.id,
"user_id": setting.id, # Compatibility with existing code
"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)