Skip to content

Commit 8b72e80

Browse files
digaomatiasRodrigo Leoteclaude
authored
fix: use real-time balances for dashboard net worth calculation (#200)
* fix: use real-time balances for dashboard net worth calculation The dashboard was using only Account.CurrentBalance (the static initial balance) to calculate TotalBalance, NetWorth, TotalAssets, and TotalLiabilities. This ignored all transactions, causing the dashboard to show incorrect values compared to the accounts page. Now uses GetAccountBalancesAsync() which correctly computes InitialBalance + Sum(Transactions) — the same method the accounts page already uses. Fixes #199 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: calculate balance, assets, liabilities in a single pass Refactored the three separate LINQ iterations over userAccounts into a single GroupBy pass using a dictionary, as suggested by code review. This improves performance and consolidates the related calculations. --------- Co-authored-by: Rodrigo Leote <rodrigol@leapthought.co.nz> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cc1168d commit 8b72e80

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

src/Core/MyMascada.Application/Features/Reports/Queries/GetDashboardSummaryQuery.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ public async Task<DashboardSummaryDto> Handle(GetDashboardSummaryQuery request,
3131
// Get user's accounts
3232
var userAccounts = (await _accountRepository.GetByUserIdAsync(request.UserId)).ToList();
3333

34-
// Calculate total balance from accounts
35-
var totalBalance = userAccounts.Sum(a => a.CurrentBalance);
36-
37-
// Net worth breakdown: CreditCard(3) + Loan(5) = liabilities, rest = assets
38-
var totalAssets = userAccounts
39-
.Where(a => a.Type != AccountType.CreditCard && a.Type != AccountType.Loan)
40-
.Sum(a => a.CurrentBalance);
41-
var totalLiabilities = Math.Abs(userAccounts
42-
.Where(a => a.Type == AccountType.CreditCard || a.Type == AccountType.Loan)
43-
.Sum(a => a.CurrentBalance));
34+
// Get real-time balances (initial balance + transaction sums)
35+
var accountBalances = await _transactionRepository.GetAccountBalancesAsync(request.UserId);
36+
37+
decimal GetBalance(Domain.Entities.Account a) => accountBalances.GetValueOrDefault(a.Id, a.CurrentBalance);
38+
39+
// Calculate total balance, assets, and liabilities in one pass
40+
var balanceByType = userAccounts
41+
.GroupBy(a => a.Type == AccountType.CreditCard || a.Type == AccountType.Loan) // true: liabilities, false: assets
42+
.ToDictionary(g => g.Key, g => g.Sum(GetBalance));
43+
44+
var totalAssets = balanceByType.GetValueOrDefault(false, 0m);
45+
var totalLiabilitiesRaw = balanceByType.GetValueOrDefault(true, 0m);
46+
47+
var totalBalance = totalAssets + totalLiabilitiesRaw;
48+
var totalLiabilities = Math.Abs(totalLiabilitiesRaw);
4449
var netWorth = totalAssets - totalLiabilities;
4550

4651
// Get current month boundaries

0 commit comments

Comments
 (0)