A local-first personal finance app
Improve merge functionality with transfers (#7600)
Alec Bakholdin6030f8779456
diff --git a/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx b/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsxindex bfbbd01b75..00e2919f94 100644--- a/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx+++ b/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx@@ -28,6 +28,7 @@ import { styles } from '@actual-app/components/styles';import { Text } from '@actual-app/components/text';import { theme } from '@actual-app/components/theme';import { View } from '@actual-app/components/view';+import { validForMerge } from '@actual-app/core/shared/merge';import * as monthUtils from '@actual-app/core/shared/months';import { isPreviewId } from '@actual-app/core/shared/transactions';import { validForTransfer } from '@actual-app/core/shared/transfer';@@ -382,8 +383,7 @@ function SelectedTransactionsFloatingActionBar({const canMerge = useMemo(() => {return Boolean(- twoTransactions &&- twoTransactions[0].amount === twoTransactions[1].amount,+ twoTransactions && validForMerge(twoTransactions[0], twoTransactions[1]),);}, [twoTransactions]);diff --git a/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsx b/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsxindex f6af0a7db7..ca7e2389e4 100644--- a/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsx+++ b/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsx@@ -3,6 +3,7 @@ import { useHotkeys } from 'react-hotkeys-hook';import { useTranslation } from 'react-i18next';import { Menu } from '@actual-app/components/menu';+import { validForMerge } from '@actual-app/core/shared/merge';import { q } from '@actual-app/core/shared/query';import {extractScheduleConds,@@ -136,8 +137,7 @@ export function SelectedTransactionsButton({const canMerge = useMemo(() => {return Boolean(- twoTransactions &&- twoTransactions[0].amount === twoTransactions[1].amount,+ twoTransactions && validForMerge(twoTransactions[0], twoTransactions[1]),);}, [twoTransactions]);diff --git a/packages/loot-core/src/server/transactions/merge.test.ts b/packages/loot-core/src/server/transactions/merge.test.tsindex cca9f44c7f..bf7587454b 100644--- a/packages/loot-core/src/server/transactions/merge.test.ts+++ b/packages/loot-core/src/server/transactions/merge.test.ts@@ -36,7 +36,7 @@ describe('Merging fails for invalid quantity', () => {});await expect(() =>mergeTransactions([{ id: t1 }, { id: t2 }]),- ).rejects.toThrow('Transaction amounts must match for merge');+ ).rejects.toThrow('Cannot merge transactions with different amounts');});it("fails when transaction id doesn't exist", async () => {@@ -111,7 +111,7 @@ describe('Merging success', () => {} as db.DbViewTransaction;const transaction2 = {- account: 'two',+ account: 'one',date: '2025-02-02',payee: 'payee2',notes: 'notes2',@@ -122,7 +122,7 @@ describe('Merging success', () => {} as TransactionEntity;const dbTransaction2 = {- account: 'two',+ account: 'one',date: 20250202,payee: 'payee2',notes: 'notes2',@@ -420,4 +420,78 @@ describe('Merging success', () => {);expect(orphanedChildren.length).toBe(0);});++ it('transfer link is preserved on drop', async () => {+ const t1 = await db.insertTransaction({+ ...transaction1,+ category: 'category_id',+ imported_id: 'import_1',+ imported_payee: 'payee_import_2',+ });++ const t2 = await db.insertTransaction({+ ...transaction2,+ });+ const t2Transfer = await db.insertTransaction({+ ...transaction2,+ account: 'one',+ amount: -transaction2.amount,+ transfer_id: t2,+ });+ await db.updateTransaction({ id: t2, transfer_id: t2Transfer });++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t1);+ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(2);+ const t1R = transactions.find(tx => tx.id === t1);+ const t2TransferR = transactions.find(tx => tx.id === t2Transfer);+ expect(t1R).toBeTruthy();+ expect(t2TransferR).toBeTruthy();+ expect(t2TransferR?.transfer_id).toBe(t1);+ expect(t1R?.transfer_id).toBe(t2Transfer);+ expect(t1R?.imported_id).toBe('import_1');+ expect(t1R?.imported_payee).toBe('payee_import_2');+ });++ it('merging two transfers selects the best transaction in each account to preserve', async () => {+ // A -> B+ const a = await db.insertTransaction({+ ...transaction1,+ amount: -10,+ date: '2025-01-01', // A is earlier than C+ });+ const b = await db.insertTransaction({+ ...transaction2,+ amount: 10,+ date: '2025-01-05', // B is later than D+ transfer_id: a,+ });+ await db.updateTransaction({ id: a, transfer_id: b });++ // C -> D+ const c = await db.insertTransaction({+ ...transaction1,+ amount: -10,+ date: '2025-01-03', // C is later than A+ });+ const d = await db.insertTransaction({+ ...transaction2,+ amount: 10,+ date: '2025-01-02', // D is earlier than B+ transfer_id: c,+ });+ await db.updateTransaction({ id: c, transfer_id: d });++ // Merge A and C+ expect(await mergeTransactions([{ id: a }, { id: c }])).toBe(a);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(2);++ expect(transactions[0].id).toBe(d); // 2025-01-02 is more recent than 2025-01-01+ expect(transactions[1].id).toBe(a);++ expect(transactions[0].transfer_id).toBe(a);+ expect(transactions[1].transfer_id).toBe(d);+ });});diff --git a/packages/loot-core/src/server/transactions/merge.ts b/packages/loot-core/src/server/transactions/merge.tsindex d76232fde5..8a706e3bd7 100644--- a/packages/loot-core/src/server/transactions/merge.ts+++ b/packages/loot-core/src/server/transactions/merge.ts@@ -1,5 +1,6 @@import { aqlQuery } from '#server/aql';import * as db from '#server/db';+import { validForMergeExplanation } from '#shared/merge';import { q } from '#shared/query';import {deleteTransaction as sharedDeleteTransaction,@@ -20,16 +21,96 @@ export async function mergeTransactions(JSON.stringify(transactions),);}+ const [a, b] = await mapAndValidateTransactions(txIds[0], txIds[1]);+ const aTransferId = a.transfer_id;+ const bTransferId = b.transfer_id;- // get most recent transactions- const [a, b]: TransactionEntity[] = await Promise.all(- txIds.map(db.getTransaction),+ // we don't need all the transfer logic if there are no transfers.+ if (!aTransferId && !bTransferId) return mergeTransactionsNoTransfer(a, b);++ const transferAccount = aTransferId ? a.payee : b.payee;++ await setTransfers([a.id, b.id, aTransferId, bTransferId], null);+ const transferId = await mergeTransfers(aTransferId, bTransferId);+ const keptTxId = await mergeTransactionsNoTransfer(a, b);+ if (transferId) {+ await db.updateTransaction({+ id: keptTxId,+ transfer_id: transferId,+ payee: transferAccount,+ });+ await db.updateTransaction({ id: transferId, transfer_id: keptTxId });+ }++ // transfers to an off-budget account have a category. Transfers to on-budget do not, so+ // when a regular transaction is merged with a non-transfer, we need to make sure any category+ // in the non-transfer is cleared out.+ //+ // However, we cannot blindly clear categories because if the transfer is to an off-budget account then+ // we need to keep the regular merge logic that was performed in mergeTransactionsNoTransfer.+ //+ // We don't have to worry about the transfer transactions in the other account because+ // either there is one transaction or there are two transfers, which does not enter this error state+ if (transferAccount && !(aTransferId && bTransferId)) {+ const payee = await db.getPayee(transferAccount);+ const account = await db.getAccount(payee?.transfer_acct ?? '');+ if (account && !account.offbudget) {+ await db.updateTransaction({ id: keptTxId, category: null });+ }+ }++ return keptTxId;+}++function setTransfers(+ txIds: (TransactionEntity['id'] | undefined)[],+ transfer_id: string | null | undefined,+) {+ return Promise.all(+ txIds+ .filter(Boolean)+ .map(txId =>+ db.updateTransaction({ id: txId, transfer_id: transfer_id ?? null }),+ ),+ );+}++async function mergeTransfers(+ aTransferId: TransactionEntity['id'] | undefined,+ bTransferId: TransactionEntity['id'] | undefined,+) {+ // if only one is a transfer, we can skip merging+ if (!aTransferId) return bTransferId;+ if (!bTransferId) return aTransferId;++ // clear transfers and merge+ const [aTransfer, bTransfer] = await mapAndValidateTransactions(+ aTransferId,+ bTransferId,);- if (!a || !b) {- throw new Error('One of the provided transactions does not exist');- } else if (a.amount !== b.amount) {- throw new Error('Transaction amounts must match for merge');+ await setTransfers([aTransfer.id, bTransfer.id], null);+ return mergeTransactionsNoTransfer(aTransfer, bTransfer);+}++async function mapAndValidateTransactions(+ aId: TransactionEntity['id'],+ bId: TransactionEntity['id'],+): Promise<TransactionEntity[]> {+ // get most recent transactions+ const a: TransactionEntity = await db.getTransaction(aId);+ const b: TransactionEntity = await db.getTransaction(bId);++ const validForMergeError = validForMergeExplanation(a, b);+ if (validForMergeError) {+ throw new Error(validForMergeError);}+ return [a, b];+}++export async function mergeTransactionsNoTransfer(+ a: TransactionEntity,+ b: TransactionEntity,+): Promise<TransactionEntity['id']> {const { keep, drop } = determineKeepDrop(a, b);// Load subtransactions with a single query, then split by parent_id in memorydiff --git a/packages/loot-core/src/shared/merge.test.ts b/packages/loot-core/src/shared/merge.test.tsnew file mode 100644index 0000000000..d91835f95b--- /dev/null+++ b/packages/loot-core/src/shared/merge.test.ts@@ -0,0 +1,42 @@+import type { TransactionEntity } from '#types/models/transaction';++import { validForMerge } from './merge';++describe('validForMerge', () => {+ const t1: TransactionEntity = {+ id: 'id1',+ account: 'one',+ amount: 12,+ date: '2025-01-01',+ };+ const t2: TransactionEntity = {+ id: 'id2',+ account: 'one',+ amount: 12,+ date: '2025-01-01',+ };++ it('cannot merge if different accounts', () => {+ expect(validForMerge({ ...t1, account: 'other' }, t2)).toBe(false);+ });++ it('cannot merge if different amounts', () => {+ expect(validForMerge({ ...t1, amount: 13 }, t2)).toBe(false);+ });++ it('can merge if everything matches', () => {+ expect(validForMerge(t1, t2)).toBe(true);+ });++ it('cannot merge if both transfers but transfer to different accounts', () => {+ const transfer1 = { ...t1, transfer_id: 'transfer1', payee: 'account3' };+ const transfer2 = { ...t2, transfer_id: 'transfer2', payee: 'account4' };+ expect(validForMerge(transfer1, transfer2)).toBe(false);+ });++ it('can merge if both transfers and transfer to the same account', () => {+ const transfer1 = { ...t1, transfer_id: 'transfer1', payee: 'account3' };+ const transfer2 = { ...t2, transfer_id: 'transfer2', payee: 'account3' };+ expect(validForMerge(transfer1, transfer2)).toBe(true);+ });+});diff --git a/packages/loot-core/src/shared/merge.ts b/packages/loot-core/src/shared/merge.tsnew file mode 100644index 0000000000..304949b766--- /dev/null+++ b/packages/loot-core/src/shared/merge.ts@@ -0,0 +1,33 @@+import type { TransactionEntity } from '#types/models';++export function validForMerge(+ transactionA: TransactionEntity | undefined,+ transactionB: TransactionEntity | undefined,+) {+ return !validForMergeExplanation(transactionA, transactionB);+}++export function validForMergeExplanation(+ transactionA: TransactionEntity | undefined,+ transactionB: TransactionEntity | undefined,+): string {+ if (!transactionA || !transactionB) {+ return 'One of the provided transactions does not exist';+ }+ if (transactionA.account !== transactionB.account) {+ return 'Cannot merge transactions from different accounts';+ }+ if (transactionA.amount !== transactionB.amount) {+ return 'Cannot merge transactions with different amounts';+ }+ // if both transfers, they should both be transferring to the same account to be eligible for merge.+ // In other words, you cannot merge A->B with A->C, but you can merge A->B with A->B+ if (+ transactionA.transfer_id &&+ transactionB.transfer_id &&+ transactionA.payee !== transactionB.payee+ ) {+ return 'Cannot merge transfers to different accounts';+ }+ return '';+}diff --git a/upcoming-release-notes/7600.md b/upcoming-release-notes/7600.mdnew file mode 100644index 0000000000..1d17e5d7cb--- /dev/null+++ b/upcoming-release-notes/7600.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [alecbakholdin]+---++Merge now prioritizes transfers and is only allowed for same-account transactions