Adds super user balance overview
Implements functionality for super users to view a breakdown of outstanding balances for all users. This includes: - Adding an API endpoint to fetch all user balances. - Updating the frontend to display these balances in a table, accessible only to super users. - Modifying the balance calculation for the current user to reflect the total owed by or to the castle for super users. This provides super users with a comprehensive view of the castle's financial position.
This commit is contained in:
parent
cb7e4ee555
commit
b7e4e05469
4 changed files with 117 additions and 4 deletions
23
views_api.py
23
views_api.py
|
|
@ -19,6 +19,7 @@ from .crud import (
|
|||
get_account_transactions,
|
||||
get_all_accounts,
|
||||
get_all_journal_entries,
|
||||
get_all_user_balances,
|
||||
get_journal_entries_by_user,
|
||||
get_journal_entry,
|
||||
get_or_create_user_account,
|
||||
|
|
@ -382,7 +383,19 @@ async def api_get_my_balance(
|
|||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> UserBalance:
|
||||
"""Get current user's balance with the Castle"""
|
||||
return await get_user_balance(wallet.wallet.id)
|
||||
from lnbits.settings import settings as lnbits_settings
|
||||
|
||||
# If super user, show total castle liabilities (what castle owes to all users)
|
||||
if wallet.wallet.user == lnbits_settings.super_user:
|
||||
all_balances = await get_all_user_balances()
|
||||
total_owed = sum(b.balance for b in all_balances if b.balance > 0)
|
||||
# Return as castle's "balance" - positive means castle owes money
|
||||
return UserBalance(
|
||||
user_id=wallet.wallet.user, balance=total_owed, accounts=[]
|
||||
)
|
||||
|
||||
# For regular users, show their individual balance
|
||||
return await get_user_balance(wallet.wallet.user)
|
||||
|
||||
|
||||
@castle_api_router.get("/api/v1/balance/{user_id}")
|
||||
|
|
@ -391,6 +404,14 @@ async def api_get_user_balance(user_id: str) -> UserBalance:
|
|||
return await get_user_balance(user_id)
|
||||
|
||||
|
||||
@castle_api_router.get("/api/v1/balances/all")
|
||||
async def api_get_all_balances(
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> list[UserBalance]:
|
||||
"""Get all user balances (admin/super user only)"""
|
||||
return await get_all_user_balances()
|
||||
|
||||
|
||||
# ===== PAYMENT ENDPOINTS =====
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue