Improves super user balance and journal views

For super users, the balance view now displays the net position
(liabilities - receivables) of the Castle. The journal view
shows all entries across all users.

The settings are now loaded first to determine if the user is a
super user.
This commit is contained in:
padreug 2025-10-22 17:51:41 +02:00
parent d7b5259b74
commit 3a26d963dc
2 changed files with 22 additions and 9 deletions

View file

@ -551,12 +551,13 @@ window.app = Vue.createApp({
}
},
async created() {
// Load settings first to determine if user is super user
await this.loadSettings()
await this.loadUserWallet()
await this.loadBalance()
await this.loadTransactions()
await this.loadAccounts()
await this.loadCurrencies()
await this.loadSettings()
await this.loadUserWallet()
// Load users if super user (for receivable dialog)
if (this.isSuperUser) {
await this.loadUsers()

View file

@ -158,6 +158,12 @@ async def api_get_user_entries(
limit: int = 100,
) -> list[JournalEntry]:
"""Get journal entries that affect the current user's accounts"""
from lnbits.settings import settings as lnbits_settings
# If super user, show all journal entries
if wallet.wallet.user == lnbits_settings.super_user:
return await get_all_journal_entries(limit)
return await get_journal_entries_by_user(wallet.wallet.user, limit)
@ -416,10 +422,17 @@ async def api_get_my_balance(
"""Get current user's balance with the Castle"""
from lnbits.settings import settings as lnbits_settings
# If super user, show total castle liabilities (what castle owes to all users)
# If super user, show total castle position
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)
# Calculate total:
# Positive balances = Castle owes users (liabilities)
# Negative balances = Users owe Castle (receivables)
# Net: positive means castle owes, negative means castle is owed
total_liabilities = sum(b.balance for b in all_balances if b.balance > 0)
total_receivables = sum(abs(b.balance) for b in all_balances if b.balance < 0)
net_balance = total_liabilities - total_receivables
# Aggregate fiat balances from all users
total_fiat_balances = {}
@ -427,14 +440,13 @@ async def api_get_my_balance(
for currency, amount in user_balance.fiat_balances.items():
if currency not in total_fiat_balances:
total_fiat_balances[currency] = 0.0
# Only add positive balances (what castle owes)
if amount > 0:
total_fiat_balances[currency] += amount
# Add all balances (positive and negative)
total_fiat_balances[currency] += amount
# Return as castle's "balance" - positive means castle owes money
# Return net position
return UserBalance(
user_id=wallet.wallet.user,
balance=total_owed,
balance=net_balance,
accounts=[],
fiat_balances=total_fiat_balances,
)