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
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 1m26s
This commit is contained in:
@@ -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,
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user