Skip to content

A local-first personal finance app

[AI] Fix mobile running balance when reconciled transactions are hidden (#9107)

Kristián Valentínccdd1cff1c3a
diff --git a/packages/desktop-client/src/hooks/useTransactions.test.ts b/packages/desktop-client/src/hooks/useTransactions.test.ts
new file mode 100644
index 0000000000..76106aba7b
--- /dev/null
+++ b/packages/desktop-client/src/hooks/useTransactions.test.ts
@@ -0,0 +1,40 @@
+import type { TransactionEntity } from '@actual-app/core/types/models';
+
+import { calculateRunningBalancesTopDown } from './useTransactions';
+
+function tx(id: string, amount: number, parent_id?: string) {
+ return { id, amount, parent_id } as TransactionEntity;
+}
+
+describe('calculateRunningBalancesTopDown', () => {
+ it('full history: last balance equals its own amount', () => {
+ const balances = calculateRunningBalancesTopDown(
+ [tx('a', 100), tx('b', -50), tx('c', 200)],
+ 'none',
+ 250,
+ );
+
+ expect(Object.fromEntries(balances)).toEqual({ a: 250, b: 150, c: 200 });
+ });
+
+ it('does not reset the last balance to its amount when older transactions are not in the list', () => {
+ // An older transaction (e.g. reconciled and hidden) contributes 1000.
+ const balances = calculateRunningBalancesTopDown(
+ [tx('a', 100), tx('b', -10)],
+ 'none',
+ 1090,
+ );
+
+ expect(Object.fromEntries(balances)).toEqual({ a: 1090, b: 990 });
+ });
+
+ it('skips split children when splits is "all"', () => {
+ const balances = calculateRunningBalancesTopDown(
+ [tx('p', -30), tx('c1', -10, 'p'), tx('c2', -20, 'p'), tx('o', 100)],
+ 'all',
+ 70,
+ );
+
+ expect(Object.fromEntries(balances)).toEqual({ p: 70, o: 100 });
+ });
+});
diff --git a/packages/desktop-client/src/hooks/useTransactions.ts b/packages/desktop-client/src/hooks/useTransactions.ts
index f6f32c2398..ecb7b6769d 100644
--- a/packages/desktop-client/src/hooks/useTransactions.ts
+++ b/packages/desktop-client/src/hooks/useTransactions.ts
@@ -242,13 +242,6 @@ export function calculateRunningBalancesTopDown(
return acc;
}
- if (index === arr.length - 1) {
- // This is the last transaction in the list,
- // so we set the running balance to the amount of the transaction
- acc.set(transaction.id, transaction.amount);
- return acc;
- }
-
const previousTransaction = arr[index - 1];
const previousRunningBalance = acc.get(previousTransaction.id) ?? 0;
const previousAmount = previousTransaction.amount ?? 0;
diff --git a/upcoming-release-notes/fix-mobile-running-balance-hidden-reconciled.md b/upcoming-release-notes/fix-mobile-running-balance-hidden-reconciled.md
new file mode 100644
index 0000000000..92d950eb13
--- /dev/null
+++ b/upcoming-release-notes/fix-mobile-running-balance-hidden-reconciled.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [kikoval]
+---
+
+Fix the wrong running balance on the oldest shown transaction on mobile when reconciled transactions are hidden