A local-first personal finance app
✨ [Feature] Manually merge transactions (#4739)
Alec Bakholdin4e9d51be1f84
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsxindex e86a1f95bd..36d2a24ce8 100644--- a/packages/desktop-client/src/components/accounts/Account.tsx+++ b/packages/desktop-client/src/components/accounts/Account.tsx@@ -1249,6 +1249,18 @@ class AccountInternal extends PureComponent<});};+ onMergeTransactions = async (ids: string[]) => {+ const keptId = await send(+ 'transactions-merge',+ ids.map(id => ({ id })),+ );+ await this.refetchTransactions();+ this.dispatchSelected?.({+ type: 'select-all',+ ids: [keptId],+ });+ };+checkForReconciledTransactions = async (ids: string[],confirmReason: string,@@ -1834,6 +1846,7 @@ class AccountInternal extends PureComponent<onSetTransfer={this.onSetTransfer}onMakeAsSplitTransaction={this.onMakeAsSplitTransaction}onMakeAsNonSplitTransactions={this.onMakeAsNonSplitTransactions}+ onMergeTransactions={this.onMergeTransactions}/><View style={{ flex: 1 }}>diff --git a/packages/desktop-client/src/components/accounts/Header.tsx b/packages/desktop-client/src/components/accounts/Header.tsxindex bf2a75e742..7988dac4e2 100644--- a/packages/desktop-client/src/components/accounts/Header.tsx+++ b/packages/desktop-client/src/components/accounts/Header.tsx@@ -122,6 +122,7 @@ type AccountHeaderProps = {| 'onSetTransfer'| 'onMakeAsSplitTransaction'| 'onMakeAsNonSplitTransactions'+ | 'onMergeTransactions'> &Pick<ComponentProps<typeof FiltersStack>,@@ -189,6 +190,7 @@ export function AccountHeader({onRunRules,onMakeAsSplitTransaction,onMakeAsNonSplitTransactions,+ onMergeTransactions,}: AccountHeaderProps) {const { t } = useTranslation();@@ -379,6 +381,7 @@ export function AccountHeader({showMakeTransfer={showMakeTransfer}onMakeAsSplitTransaction={onMakeAsSplitTransaction}onMakeAsNonSplitTransactions={onMakeAsNonSplitTransactions}+ onMergeTransactions={onMergeTransactions}/>)}<View style={{ flex: '0 0 auto', marginLeft: 10 }}>diff --git a/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx b/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsxindex 1527a5e5c5..1622fada96 100644--- a/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx+++ b/packages/desktop-client/src/components/mobile/transactions/TransactionList.tsx@@ -293,6 +293,7 @@ function SelectedTransactionsFloatingActionBar({onBatchLinkSchedule,onBatchUnlinkSchedule,onSetTransfer,+ onMerge,} = useTransactionBatchActions();const navigate = useNavigate();@@ -315,25 +316,37 @@ function SelectedTransactionsFloatingActionBar({};}, [dispatch]);- const canBeTransfer = useMemo(() => {- // only two selected- if (selectedTransactionsArray.length !== 2) {- return false;- }- const fromTrans = transactions.find(- t => t.id === selectedTransactionsArray[0],- );- const toTrans = transactions.find(- t => t.id === selectedTransactionsArray[1],- );+ const twoTransactions: [TransactionEntity, TransactionEntity] | undefined =+ useMemo(() => {+ // only two selected+ if (selectedTransactionsArray.length !== 2) {+ return undefined;+ }++ const [a, b] = selectedTransactionsArray.map(id =>+ transactions.find(t => t.id === id),+ );+ if (!a || !b) {+ return undefined;+ }++ return [a, b];+ }, [selectedTransactionsArray, transactions]);- // previously selected transactions aren't always present in current transaction list- if (!fromTrans || !toTrans) {+ const canBeTransfer = useMemo(() => {+ if (!twoTransactions) {return false;}-+ const [fromTrans, toTrans] = twoTransactions;return validForTransfer(fromTrans, toTrans);- }, [selectedTransactionsArray, transactions]);+ }, [twoTransactions]);++ const canMerge = useMemo(() => {+ return Boolean(+ twoTransactions &&+ twoTransactions[0].amount === twoTransactions[1].amount,+ );+ }, [twoTransactions]);const moreOptionsMenuItems: MenuItem<string>[] = [{@@ -350,6 +363,11 @@ function SelectedTransactionsFloatingActionBar({name: 'delete',text: t('Delete'),},+ {+ name: 'merge',+ text: t('Merge'),+ disabled: !canMerge,+ },];if (showMakeTransfer) {@@ -604,6 +622,12 @@ function SelectedTransactionsFloatingActionBar({),}),);+ } else if (type === 'merge') {+ onMerge?.(selectedTransactionsArray, () =>+ showUndoNotification({+ message: t('Successfully merged transactions'),+ }),+ );}setIsMoreOptionsMenuOpen(false);}}diff --git a/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsx b/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsxindex 66034799af..833a93c806 100644--- a/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsx+++ b/packages/desktop-client/src/components/transactions/SelectedTransactionsButton.tsx@@ -47,6 +47,7 @@ type SelectedTransactionsButtonProps = {showMakeTransfer: boolean;onMakeAsSplitTransaction: (selectedIds: string[]) => void;onMakeAsNonSplitTransactions: (selectedIds: string[]) => void;+ onMergeTransactions: (selectedIds: string[]) => void;};export function SelectedTransactionsButton({@@ -64,6 +65,7 @@ export function SelectedTransactionsButton({showMakeTransfer,onMakeAsSplitTransaction,onMakeAsNonSplitTransactions,+ onMergeTransactions,}: SelectedTransactionsButtonProps) {const { t } = useTranslation();const dispatch = useDispatch();@@ -110,21 +112,35 @@ export function SelectedTransactionsButton({);}, [types.preview, selectedIds, getTransaction]);+ const twoTransactions: [TransactionEntity, TransactionEntity] | undefined =+ useMemo(() => {+ if (selectedIds?.length !== 2) {+ return undefined;+ }+ const [t0, t1] = selectedIds.map(getTransaction);+ // previously selected transactions aren't always present in current transaction list+ if (!t0 || !t1) {+ return undefined;+ }++ return [t0, t1];+ }, [selectedIds, getTransaction]);+const canBeTransfer = useMemo(() => {// only two selected- if (selectedIds.length !== 2) {- return false;- }- const fromTrans = getTransaction(selectedIds[0]);- const toTrans = getTransaction(selectedIds[1]);-- // previously selected transactions aren't always present in current transaction list- if (!fromTrans || !toTrans) {+ if (!twoTransactions) {return false;}-+ const [fromTrans, toTrans] = twoTransactions;return validForTransfer(fromTrans, toTrans);- }, [selectedIds, getTransaction]);+ }, [twoTransactions]);++ const canMerge = useMemo(() => {+ return Boolean(+ twoTransactions &&+ twoTransactions[0].amount === twoTransactions[1].amount,+ );+ }, [twoTransactions]);const canBeSkipped = useMemo(() => {const recurringSchedules = selectedSchedules.filter(s => {@@ -249,6 +265,12 @@ export function SelectedTransactionsButton({},[onLinkSchedule, onViewSchedule, linked, selectedIds],);+ useHotkeys(+ 'm',+ () => canMerge && onMergeTransactions(selectedIds),+ hotKeyOptions,+ [onMergeTransactions, selectedIds],+ );return (<SelectedItemsButton@@ -339,6 +361,15 @@ export function SelectedTransactionsButton({} as const,]: []),+ ...(canMerge+ ? [+ {+ name: 'merge-transactions',+ text: t('Merge'),+ key: 'M',+ } as const,+ ]+ : []),Menu.line,{ type: Menu.label, name: t('Edit field'), text: '' } as const,{ name: 'date', text: t('Date') } as const,@@ -367,6 +398,9 @@ export function SelectedTransactionsButton({case 'unsplit-transactions':onMakeAsNonSplitTransactions(selectedIds);break;+ case 'merge-transactions':+ onMergeTransactions(selectedIds);+ break;case 'post-transaction':case 'skip':case 'complete':diff --git a/packages/desktop-client/src/hooks/useTransactionBatchActions.ts b/packages/desktop-client/src/hooks/useTransactionBatchActions.tsindex 0d42461d9a..22792d565c 100644--- a/packages/desktop-client/src/hooks/useTransactionBatchActions.ts+++ b/packages/desktop-client/src/hooks/useTransactionBatchActions.ts@@ -502,6 +502,14 @@ export function useTransactionBatchActions() {);};+ const onMerge = async (ids: string[], onSuccess: () => void) => {+ await send(+ 'transactions-merge',+ ids.map(id => ({ id })),+ );+ onSuccess();+ };+return {onBatchEdit,onBatchDuplicate,@@ -509,5 +517,6 @@ export function useTransactionBatchActions() {onBatchLinkSchedule,onBatchUnlinkSchedule,onSetTransfer,+ onMerge,};}diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.tsindex c2a38cb0fc..0d44b1988e 100644--- a/packages/loot-core/src/server/db/index.ts+++ b/packages/loot-core/src/server/db/index.ts@@ -15,6 +15,7 @@ import * as fs from '../../platform/server/fs';import * as sqlite from '../../platform/server/sqlite';import * as monthUtils from '../../shared/months';import { groupById } from '../../shared/util';+import { TransactionEntity } from '../../types/models';import { WithRequired } from '../../types/util';import {schema,@@ -785,7 +786,9 @@ export async function getTransactions(accountId: DbTransaction['acct']) {);}-export function insertTransaction(transaction) {+export function insertTransaction(+ transaction,+): Promise<TransactionEntity['id']> {return insertWithSchema('transactions', transaction);}diff --git a/packages/loot-core/src/server/transactions/app.ts b/packages/loot-core/src/server/transactions/app.tsindex d762be8f1c..6596d4f38b 100644--- a/packages/loot-core/src/server/transactions/app.ts+++ b/packages/loot-core/src/server/transactions/app.ts@@ -12,6 +12,7 @@ import { undoable } from '../undo';import { exportQueryToCSV, exportToCSV } from './export/export-to-csv';import { parseFile, ParseFileOptions } from './import/parse-file';+import { mergeTransactions } from './merge';import { batchUpdateTransactions } from '.';@@ -23,6 +24,7 @@ export type TransactionHandlers = {'transactions-parse-file': typeof parseTransactionsFile;'transactions-export': typeof exportTransactions;'transactions-export-query': typeof exportTransactionsQuery;+ 'transactions-merge': typeof mergeTransactions;'get-earliest-transaction': typeof getEarliestTransaction;};@@ -106,6 +108,7 @@ app.method('transactions-batch-update',mutator(undoable(handleBatchUpdateTransactions)),);+app.method('transactions-merge', mutator(undoable(mergeTransactions)));app.method('transaction-add', mutator(addTransaction));app.method('transaction-update', mutator(updateTransaction));diff --git a/packages/loot-core/src/server/transactions/merge.test.ts b/packages/loot-core/src/server/transactions/merge.test.tsnew file mode 100644index 0000000000..2568f14485--- /dev/null+++ b/packages/loot-core/src/server/transactions/merge.test.ts@@ -0,0 +1,286 @@+// @ts-strict-ignore+import { TransactionEntity } from '../../types/models';+import * as db from '../db';++import { mergeTransactions } from './merge';++describe('Merging fails for invalid quantity', () => {+ beforeEach(global.emptyDatabase());+ afterEach(global.emptyDatabase());++ const tests: [TransactionEntity[], string][] = [+ [[{} as TransactionEntity], 'one transaction'],+ [[], 'no transactions'],+ [undefined as unknown as TransactionEntity[], 'undefined'],+ [[{}, {}, {}] as TransactionEntity[], 'three transactions'],+ [+ [{}, undefined] as TransactionEntity[],+ 'two transactions but one is undefined',+ ],+ ];+ tests.forEach(([arr, message]) =>+ it(message, () => expect(() => mergeTransactions(arr)).rejects.toThrow()),+ );++ it("fails when amounts don't match", async () => {+ await prepareDatabase();+ const t1 = await db.insertTransaction({+ account: 'one',+ date: '2025-01-01',+ amount: 10,+ });+ const t2 = await db.insertTransaction({+ account: 'one',+ date: '2025-01-01',+ amount: 12,+ });+ expect(() => mergeTransactions([{ id: t1 }, { id: t2 }])).rejects.toThrow(+ 'Transaction amounts must match for merge',+ );+ });++ it("fails when transaction id doesn't exist", async () => {+ await prepareDatabase();+ const t1 = await db.insertTransaction({+ account: 'one',+ date: '2025-01-01',+ amount: 10,+ });+ expect(() =>+ mergeTransactions([{ id: t1 }, { id: 'missing' }]),+ ).rejects.toThrow('One of the provided transactions does not exist');+ });+});++async function prepareDatabase() {+ await db.insertCategoryGroup({ id: 'group1', name: 'group1', is_income: 0 });+ await db.insertCategory({+ id: '1',+ name: 'cat1',+ cat_group: 'group1',+ is_income: 0,+ });+ await db.insertCategory({+ id: '2',+ name: 'cat2',+ cat_group: 'group1',+ is_income: 0,+ });+ await db.insertAccount({ id: 'one', name: 'one' });+ await db.insertAccount({ id: 'two', name: 'two' });+ await db.insertAccount({ id: 'three', name: 'three', offbudget: 1 });+ await db.insertPayee({ id: 'payee1', name: 'one' });+ await db.insertPayee({ id: 'payee2', name: 'two' });+ await db.insertPayee({ id: 'payee3', name: 'three' });+}++function getAllTransactions() {+ return db.all<db.DbViewTransaction & { payee_name: db.DbPayee['name'] }>(+ `SELECT t.*, p.name as payee_name+ FROM v_transactions t+ LEFT JOIN payees p ON p.id = t.payee+ ORDER BY date DESC, amount DESC, id+ `,+ );+}++describe('Merging success', () => {+ beforeEach(global.emptyDatabase());+ beforeEach(prepareDatabase);+ afterEach(global.emptyDatabase());+ const transaction1 = {+ account: 'one',+ date: '2025-01-01',+ payee: 'payee1',+ notes: 'notes1',+ category: '1',+ amount: 5,+ cleared: false,+ reconciled: false,+ } as TransactionEntity;++ const dbTransaction1 = {+ account: 'one',+ date: 20250101,+ payee: 'payee1',+ notes: 'notes1',+ category: '1',+ amount: 5,+ cleared: 1,+ reconciled: 1,+ } as db.DbViewTransaction;++ const transaction2 = {+ account: 'two',+ date: '2025-02-02',+ payee: 'payee2',+ notes: 'notes2',+ category: '2',+ amount: 5,+ cleared: true,+ reconciled: true,+ } as TransactionEntity;++ const dbTransaction2 = {+ account: 'two',+ date: 20250202,+ payee: 'payee2',+ notes: 'notes2',+ category: '2',+ amount: 5,+ cleared: 1,+ reconciled: 1,+ } as db.DbViewTransaction;++ it('two banksynced transactions keeps older transaction', async () => {+ const t1 = await db.insertTransaction({+ ...transaction1,+ imported_id: 'imported_1',+ });+ const t2 = await db.insertTransaction({+ ...transaction2,+ imported_id: 'imported_2',+ });++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t1);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction1,+ imported_id: 'imported_1',+ });+ });++ it('first banksynced, second manual keeps banksynced values', async () => {+ const t1 = await db.insertTransaction({+ ...transaction1,+ imported_id: 'imported_1',+ });+ const t2 = await db.insertTransaction(transaction2);++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t1);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction1,+ imported_id: 'imported_1',+ });+ });++ it('first file imported, second banksycned keeps banksynced values', async () => {+ const t1 = await db.insertTransaction({+ ...transaction1,+ imported_payee: 'payee',+ });+ const t2 = await db.insertTransaction({+ ...transaction2,+ imported_id: 'imported_2',+ });++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t2);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction2,+ imported_id: 'imported_2',+ });+ });++ it('second file imported, first banksycned keeps banksynced values', async () => {+ const t1 = await db.insertTransaction({+ ...transaction1,+ imported_id: 'imported_1',+ });+ const t2 = await db.insertTransaction({+ ...transaction2,+ imported_payee: 'payee',+ });++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t1);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction1,+ imported_id: 'imported_1',+ });+ });++ it('second file imported, first manual keeps file imported values', async () => {+ const t1 = await db.insertTransaction(transaction1);+ const t2 = await db.insertTransaction({+ ...transaction2,+ imported_payee: 'payee',+ });++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t2);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction2,+ imported_payee: 'payee',+ });+ });++ it('first file imported, second manual keeps file imported values', async () => {+ const t1 = await db.insertTransaction({+ ...transaction1,+ imported_payee: 'payee',+ });+ const t2 = await db.insertTransaction(transaction2);++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t1);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction1,+ imported_payee: 'payee',+ });+ });++ it('second banksynced, first manual keeps banksynced values', async () => {+ const t1 = await db.insertTransaction(transaction1);+ const t2 = await db.insertTransaction({+ ...transaction2,+ imported_id: 'imported_2',+ });++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t2);++ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction2,+ imported_id: 'imported_2',+ });+ });++ it('missing values in keep are filled in with drop values', async () => {+ // only insert required fields, imported to be kept+ const t1 = await db.insertTransaction({+ account: 'one',+ amount: 5,+ date: '2025-01-01',+ imported_id: 'imported_1',+ });+ const t2 = await db.insertTransaction(transaction2);++ expect(await mergeTransactions([{ id: t1 }, { id: t2 }])).toBe(t1);+ const transactions = await getAllTransactions();+ expect(transactions.length).toBe(1);+ expect(transactions[0]).toMatchObject({+ ...dbTransaction2,+ // values that should be kept from t1+ id: t1,+ account: 'one',+ amount: 5,+ date: 20250101,+ imported_id: 'imported_1',+ });+ });+});diff --git a/packages/loot-core/src/server/transactions/merge.ts b/packages/loot-core/src/server/transactions/merge.tsnew file mode 100644index 0000000000..897aac9245--- /dev/null+++ b/packages/loot-core/src/server/transactions/merge.ts@@ -0,0 +1,66 @@+import { type TransactionEntity } from '../../types/models';+import * as db from '../db';++export async function mergeTransactions(+ transactions: Pick<TransactionEntity, 'id'>[],+): Promise<TransactionEntity['id']> {+ // make sure all values have ids+ const txIds = transactions?.map(x => x?.id).filter(Boolean) || [];+ if (txIds.length !== 2) {+ throw new Error(+ 'Merging is only possible with 2 transactions, but found ' ++ JSON.stringify(transactions),+ );+ }++ // get most recent transactions+ const [a, b]: TransactionEntity[] = await Promise.all(+ txIds.map(db.getTransaction),+ );+ 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');+ }+ const { keep, drop } = determineKeepDrop(a, b);++ await Promise.all([+ db.updateTransaction({+ id: keep.id,+ payee: keep.payee || drop.payee,+ category: keep.category || drop.category,+ notes: keep.notes || drop.notes,+ cleared: keep.cleared || drop.cleared,+ reconciled: keep.reconciled || drop.reconciled,+ } as TransactionEntity),+ db.deleteTransaction(drop),+ ]);+ return keep.id;+}++function determineKeepDrop(+ a: TransactionEntity,+ b: TransactionEntity,+): { keep: TransactionEntity; drop: TransactionEntity } {+ // if one is imported through bank sync and the other is manual,+ // keep the imported transaction+ if (b.imported_id && !a.imported_id) {+ return { keep: b, drop: a };+ } else if (a.imported_id && !b.imported_id) {+ return { keep: a, drop: b };+ }++ // same logic but for imported transactions+ if (b.imported_payee && !a.imported_payee) {+ return { keep: b, drop: a };+ } else if (a.imported_payee && !b.imported_payee) {+ return { keep: a, drop: b };+ }++ // keep the earlier transaction+ if (a.date.localeCompare(b.date) < 0) {+ return { keep: a, drop: b };+ } else {+ return { keep: b, drop: a };+ }+}diff --git a/upcoming-release-notes/4739.md b/upcoming-release-notes/4739.mdnew file mode 100644index 0000000000..4dcabba6bf--- /dev/null+++ b/upcoming-release-notes/4739.md@@ -0,0 +1,6 @@+---+category: Features+authors: [alecbakholdin]+---++added ability to merge transactions