diff --git a/static/js/index.js b/static/js/index.js index 93efa41..1553b27 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -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() diff --git a/views_api.py b/views_api.py index a588639..81784b4 100644 --- a/views_api.py +++ b/views_api.py @@ -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, )