A local-first personal finance app
sidebar: trigger group balance updates on sync (#8994)
Matt Fiddamanf95e66b85ed8
diff --git a/packages/loot-core/src/server/sync/index.ts b/packages/loot-core/src/server/sync/index.tsindex f865f81bb4..434cdb3798 100644--- a/packages/loot-core/src/server/sync/index.ts+++ b/packages/loot-core/src/server/sync/index.ts@@ -569,20 +569,29 @@ async function _applyMessages(messages: Message[], deferUnknownSchema = false) {// Transfers insert the source row in one sync batch and the counterparty in// a second. triggerDatabaseChanges should dirty aggregate query cells, but// explicitly recompute global account totals so the second batch always- // refreshes sidebar "All accounts" / On budget / etc. (see bindings.ts).+ // refreshes sidebar "All accounts" / On budget / account group subtotals /+ // etc. (see bindings.ts).if (idsPerTable.transactions?.length) {const s = sheet.get();- const globalAggregateCells = [- 'accounts-balance',- 'onbudget-accounts-balance',- 'offbudget-accounts-balance',- 'closed-accounts-balance',- ] as const;- for (const cellName of globalAggregateCells) {- const fullName = resolveName('__global', cellName);- if (s.hasCell(fullName)) {- s.recompute(fullName);- }+ const globalAggregateCells = new Set(+ [+ 'accounts-balance',+ 'onbudget-accounts-balance',+ 'offbudget-accounts-balance',+ 'closed-accounts-balance',+ ].map(cellName => resolveName('__global', cellName)),+ );+ const accountGroupCellPrefix = resolveName(+ '__global',+ 'account-group-balance-',+ );+ const cellsToRecompute = [...s.getNodes().keys()].filter(+ name =>+ globalAggregateCells.has(name) ||+ name.startsWith(accountGroupCellPrefix),+ );+ for (const name of cellsToRecompute) {+ s.recompute(name);}}diff --git a/packages/loot-core/src/server/sync/sync.test.ts b/packages/loot-core/src/server/sync/sync.test.tsindex 21322577df..0a9b455972 100644--- a/packages/loot-core/src/server/sync/sync.test.ts+++ b/packages/loot-core/src/server/sync/sync.test.ts@@ -5,6 +5,7 @@ import * as db from '#server/db';import * as prefs from '#server/prefs';import * as sheet from '#server/sheet';import * as mockSyncServer from '#server/tests/mockSyncServer';+import { q } from '#shared/query';import * as encoder from './encoder';import { isError } from './utils';@@ -405,3 +406,51 @@ describe('Sync projections', () => {await applyMessages(secondMessages);});});++describe('Sync account balance cells', () => {+ test('recomputes account totals and group subtotals when transactions sync', async () => {+ void prefs.loadPrefs();+ void prefs.savePrefs({ groupId: 'group' });+ await sheet.loadSpreadsheet(db);++ const spreadsheet = sheet.get();+ function sumOf(filter: Record<string, unknown>) {+ return q('transactions')+ .filter(filter)+ .calculate({ $sum: '$amount' })+ .serialize();+ }+ spreadsheet.createQuery(+ '__global',+ 'onbudget-accounts-balance',+ sumOf({ 'account.offbudget': false }),+ );+ spreadsheet.createQuery(+ '__global',+ 'account-group-balance-g1-on',+ sumOf({ 'account.account_group_id': 'g1', 'account.offbudget': false }),+ );+ spreadsheet.createQuery(+ '__global',+ 'balance-acct1',+ sumOf({ account: 'acct1' }),+ );+ await sheet.waitOnSpreadsheet();++ const recompute = vi.spyOn(spreadsheet, 'recompute');+ await applyMessages([+ global.stepForwardInTime() || {+ dataset: 'transactions',+ row: 'foo',+ column: 'amount',+ value: 3200,+ timestamp: Timestamp.send(),+ },+ ]);++ const recomputed = recompute.mock.calls.map(([name]) => name);+ expect(recomputed).toContain('__global!onbudget-accounts-balance');+ expect(recomputed).toContain('__global!account-group-balance-g1-on');+ expect(recomputed).not.toContain('__global!balance-acct1');+ });+});diff --git a/upcoming-release-notes/fix-account-group-balance-sync.md b/upcoming-release-notes/fix-account-group-balance-sync.mdnew file mode 100644index 0000000000..2e77010e57--- /dev/null+++ b/upcoming-release-notes/fix-account-group-balance-sync.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [matt-fidd]+---++Keep account group totals in the experimental new sidebar up to date after syncing transfers from another device