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:
padreug 2025-10-22 15:24:50 +02:00
parent cb7e4ee555
commit b7e4e05469
4 changed files with 117 additions and 4 deletions

37
crud.py
View file

@ -317,6 +317,43 @@ async def get_user_balance(user_id: str) -> UserBalance:
)
async def get_all_user_balances() -> list[UserBalance]:
"""Get balances for all users (used by castle to see who they owe)"""
# Get all user-specific accounts
all_accounts = await db.fetchall(
"SELECT * FROM accounts WHERE user_id IS NOT NULL",
{},
Account,
)
# Group by user_id
users_dict = {}
for account in all_accounts:
if account.user_id not in users_dict:
users_dict[account.user_id] = []
users_dict[account.user_id].append(account)
# Calculate balance for each user
user_balances = []
for user_id, accounts in users_dict.items():
total_balance = 0
for account in accounts:
balance = await get_account_balance(account.id)
if account.account_type == AccountType.LIABILITY:
total_balance += balance
elif account.account_type == AccountType.ASSET:
total_balance -= balance
if total_balance != 0: # Only include users with non-zero balance
user_balances.append(
UserBalance(
user_id=user_id, balance=total_balance, accounts=accounts
)
)
return user_balances
async def get_account_transactions(
account_id: str, limit: int = 100
) -> list[tuple[JournalEntry, EntryLine]]: