feat: add total balance calculation and display in account management; update account card to show calculated balance
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 1m26s

This commit is contained in:
Julien Froidefond
2025-12-20 11:30:26 +01:00
parent 4e1e623f93
commit 376bc8f84e
8 changed files with 367 additions and 56 deletions

View File

@@ -369,7 +369,7 @@ export const bankingService = {
},
async getAccountsWithStats(): Promise<
Array<Account & { transactionCount: number }>
Array<Account & { transactionCount: number; calculatedBalance: number }>
> {
const accounts = await prisma.account.findMany({
include: {
@@ -377,32 +377,40 @@ export const bankingService = {
},
});
// Get transaction counts for all accounts in one query
const transactionCounts = await prisma.transaction.groupBy({
// Get transaction counts and sums for all accounts in one query
const transactionStats = await prisma.transaction.groupBy({
by: ["accountId"],
_count: {
id: true,
},
_sum: {
amount: true,
},
});
const countMap = new Map<string, number>();
transactionCounts.forEach((tc) => {
countMap.set(tc.accountId, tc._count.id);
const balanceMap = new Map<string, number>();
transactionStats.forEach((ts) => {
countMap.set(ts.accountId, ts._count.id);
balanceMap.set(ts.accountId, ts._sum.amount || 0);
});
return accounts.map((a): Account & { transactionCount: number } => ({
id: a.id,
name: a.name,
bankId: a.bankId,
accountNumber: a.accountNumber,
type: a.type as Account["type"],
folderId: a.folderId,
balance: a.balance,
initialBalance: a.initialBalance,
currency: a.currency,
lastImport: a.lastImport,
externalUrl: a.externalUrl,
transactionCount: countMap.get(a.id) || 0,
}));
return accounts.map(
(a): Account & { transactionCount: number; calculatedBalance: number } => ({
id: a.id,
name: a.name,
bankId: a.bankId,
accountNumber: a.accountNumber,
type: a.type as Account["type"],
folderId: a.folderId,
balance: a.balance,
initialBalance: a.initialBalance,
currency: a.currency,
lastImport: a.lastImport,
externalUrl: a.externalUrl,
transactionCount: countMap.get(a.id) || 0,
calculatedBalance: balanceMap.get(a.id) || 0,
}),
);
},
};