A local-first personal finance app
[AI] Reserve children of matched bank splits (#8909)
Maksim Ustinov24deae74e653
diff --git a/packages/loot-core/src/server/accounts/sync.test.ts b/packages/loot-core/src/server/accounts/sync.test.tsindex 6db27f7d03..73896ed1d2 100644--- a/packages/loot-core/src/server/accounts/sync.test.ts+++ b/packages/loot-core/src/server/accounts/sync.test.ts@@ -638,6 +638,119 @@ describe('Account sync', () => {).toBe(-1239);});+ test('bank sync does not fuzzy-match children of an exactly matched split', async () => {+ const { id } = await prepareDatabase();++ await db.insertTransaction({+ id: 'split-parent',+ account: id,+ amount: -1000,+ date: '2024-04-05',+ imported_id: 'parent-provider-id',+ is_parent: true,+ });+ await db.insertTransaction({+ id: 'split-child-1',+ account: id,+ amount: -299,+ date: '2024-04-05',+ is_child: true,+ parent_id: 'split-parent',+ });+ await db.insertTransaction({+ id: 'split-child-2',+ account: id,+ amount: -701,+ date: '2024-04-05',+ is_child: true,+ parent_id: 'split-parent',+ });++ const result = await reconcileTransactions(+ id,+ [+ {+ transactionId: 'new-provider-id',+ transactionAmount: { amount: '-2.99' },+ date: '2024-04-07',+ payeeName: 'New merchant',+ booked: true,+ },+ {+ transactionId: 'parent-provider-id',+ transactionAmount: { amount: '-10.00' },+ date: '2024-04-05',+ payeeName: 'Split merchant',+ booked: true,+ },+ ],+ { isBankSyncAccount: true, strictIdChecking: false },+ );++ expect(result.added).toHaveLength(1);++ const transactions = await getAllTransactions();+ expect(+ transactions.find(transaction => transaction.id === 'split-child-1')+ .imported_id,+ ).toBeNull();+ expect(+ transactions.find(+ transaction => transaction.imported_id === 'new-provider-id',+ ),+ ).toMatchObject({ amount: -299, parent_id: null });+ });++ test('bank sync can fuzzy-match children of an unmatched split', async () => {+ const { id } = await prepareDatabase();++ await db.insertTransaction({+ id: 'split-parent',+ account: id,+ amount: -1000,+ date: '2024-04-05',+ is_parent: true,+ });+ await db.insertTransaction({+ id: 'split-child-1',+ account: id,+ amount: -299,+ date: '2024-04-05',+ is_child: true,+ parent_id: 'split-parent',+ });+ await db.insertTransaction({+ id: 'split-child-2',+ account: id,+ amount: -701,+ date: '2024-04-05',+ is_child: true,+ parent_id: 'split-parent',+ });++ const result = await reconcileTransactions(+ id,+ [+ {+ transactionId: 'child-provider-id',+ transactionAmount: { amount: '-2.99' },+ date: '2024-04-07',+ payeeName: 'Child merchant',+ booked: true,+ },+ ],+ { isBankSyncAccount: true, strictIdChecking: false },+ );++ expect(result.added).toHaveLength(0);++ const transactions = await getAllTransactions();+ expect(+ transactions.find(transaction => transaction.id === 'split-child-1')+ .imported_id,+ ).toBe('child-provider-id');+ });+test('given an imported tx with no imported_id, ' +'when using fuzzy search V2, existing transaction has an imported_id, matches amount, and is within 7 days of imported tx, ' +diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.tsindex efcf65e14e..29eeb2861f 100644--- a/packages/loot-core/src/server/accounts/sync.ts+++ b/packages/loot-core/src/server/accounts/sync.ts@@ -816,6 +816,7 @@ export async function matchTransactions().then(data => String(data?.data?.[0]?.value ?? 'true') === 'true');const hasMatched = new Set();+ const exactMatchedParentIds = new Set<db.DbViewTransaction['id']>();const { normalized, payeesToCreate } = isBankSyncAccount? await normalizeBankSyncTransactions(transactions, acctId)@@ -853,6 +854,10 @@ export async function matchTransactions(if (match) {hasMatched.add(match.id);++ if (isBankSyncAccount && match.is_parent) {+ exactMatchedParentIds.add(match.id);+ }}}@@ -872,6 +877,7 @@ export async function matchTransactions(db.DbViewTransaction,| 'id'| 'is_parent'+ | 'parent_id'| 'date'| 'imported_id'| 'payee'@@ -883,7 +889,7 @@ export async function matchTransactions(| 'amount'>>(- `SELECT id, is_parent, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amount+ `SELECT id, is_parent, parent_id, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amountFROM v_transactionsWHERE-- If both ids are set, and we didn't match earlier then skip dedup@@ -904,6 +910,7 @@ export async function matchTransactions(db.DbViewTransaction,| 'id'| 'is_parent'+ | 'parent_id'| 'date'| 'imported_id'| 'payee'@@ -915,7 +922,7 @@ export async function matchTransactions(| 'amount'>>(- `SELECT id, is_parent, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amount+ `SELECT id, is_parent, parent_id, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amountFROM v_transactionsWHERE date >= ? AND date <= ? AND amount = ? AND account = ?`,[sevenDaysBefore, sevenDaysAfter, trans.amount || 0, acctId],@@ -961,7 +968,10 @@ export async function matchTransactions(if (!data.match && data.fuzzyDataset) {// Try to find one where the payees match.const match = data.fuzzyDataset.find(- row => !hasMatched.has(row.id) && data.trans.payee === row.payee,+ row =>+ !hasMatched.has(row.id) &&+ !exactMatchedParentIds.has(row.parent_id) &&+ data.trans.payee === row.payee,);if (match) {@@ -978,7 +988,10 @@ export async function matchTransactions(// around the same date with the same amount.const transactionsStep3 = transactionsStep2.map(data => {if (!data.match && data.fuzzyDataset) {- const match = data.fuzzyDataset.find(row => !hasMatched.has(row.id));+ const match = data.fuzzyDataset.find(+ row =>+ !hasMatched.has(row.id) && !exactMatchedParentIds.has(row.parent_id),+ );if (match) {hasMatched.add(match.id);return { ...data, match };diff --git a/upcoming-release-notes/protect-bank-synced-split-children.md b/upcoming-release-notes/protect-bank-synced-split-children.mdnew file mode 100644index 0000000000..21f886d03f--- /dev/null+++ b/upcoming-release-notes/protect-bank-synced-split-children.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [madesst]+---++Prevent new bank transactions from matching parts of an already matched split transaction