Skip to content

A local-first personal finance app

[AI] Fix CSV import merging a new transaction after unchecking a matched row (#9135)

Kristián Valentínc65884a27ba3
diff --git a/packages/desktop-client/e2e/accounts.test.ts b/packages/desktop-client/e2e/accounts.test.ts
index e4285d35cd..3034eef10e 100644
--- a/packages/desktop-client/e2e/accounts.test.ts
+++ b/packages/desktop-client/e2e/accounts.test.ts
@@ -374,6 +374,55 @@ test.describe('Accounts', () => {
await expect(importButton).not.toBeVisible();
});
+ test('imports a new transaction when an unchecked match has the same amount', async () => {
+ async function openCsv(content: string) {
+ const fileChooserPromise = page.waitForEvent('filechooser');
+ await accountPage.page.getByRole('button', { name: 'Import' }).click();
+ const fileChooser = await fileChooserPromise;
+ await fileChooser.setFiles({
+ name: 'same-amount.csv',
+ mimeType: 'text/csv',
+ buffer: Buffer.from(content),
+ });
+ return page.getByRole('dialog');
+ }
+
+ let dialog = await openCsv(
+ 'Date,Payee,Notes,Amount\n2024-08-02,Netflix,,-8.99\n',
+ );
+ await dialog
+ .getByRole('button', { name: 'Import 1 transactions' })
+ .click();
+ await expect(dialog).not.toBeVisible();
+
+ // Netflix matches the existing transaction; Corner Coffee is new.
+ dialog = await openCsv(
+ 'Date,Payee,Notes,Amount\n' +
+ '2024-08-02,Netflix,Subscription,-8.99\n' +
+ '2024-07-31,Corner Coffee,Latte,-8.99\n',
+ );
+ await expect(dialog.getByText('Corner Coffee')).toBeVisible();
+
+ // Uncheck the Netflix row: merge -> import as new -> skip
+ const netflixCheckbox = dialog
+ .getByTestId('row')
+ .filter({ hasText: 'Netflix' })
+ .getByRole('checkbox');
+ await netflixCheckbox.click();
+ await netflixCheckbox.click();
+ await dialog
+ .getByRole('button', { name: 'Import 1 transactions' })
+ .click();
+ await expect(dialog).not.toBeVisible();
+
+ await expect(
+ accountPage.transactionTableRow.filter({ hasText: 'Corner Coffee' }),
+ ).toBeVisible();
+ await expect(
+ accountPage.transactionTableRow.filter({ hasText: 'Subscription' }),
+ ).toHaveCount(0);
+ });
+
test('import notes checkbox is not shown for CSV files', async () => {
const fileChooserPromise = page.waitForEvent('filechooser');
await accountPage.page.getByRole('button', { name: 'Import' }).click();
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
index 1c5eb29f2e..b1d59d279f 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
@@ -643,11 +643,11 @@ export function ImportTransactionsModal({
for (let trans of transactions) {
if (
trans.isMatchedTransaction ||
- (reconcile && !trans.selected && !trans.ignored)
+ (reconcile && !trans.selected && !trans.ignored && !trans.existing)
) {
// skip transactions that are
// - matched transaction (existing transaction added to show update changes)
- // - unselected transactions that are not ignored by the reconcilation algorithm (only when reconcilation is enabled)
+ // - unselected transactions that are not ignored or merged by the reconcilation algorithm (only when reconcilation is enabled)
continue;
}
@@ -705,6 +705,12 @@ export function ImportTransactionsModal({
finalTransaction.forceAddTransaction = true;
}
+ if (reconcile && trans.existing && !trans.selected) {
+ // unselected transactions with existing are still sent, so they keep
+ // their match and the other transactions match as in the preview
+ finalTransaction.skipTransaction = true;
+ }
+
finalTransactions.push({
...finalTransaction,
category: category_id,
diff --git a/packages/loot-core/src/server/accounts/sync.test.ts b/packages/loot-core/src/server/accounts/sync.test.ts
index 6159155fff..89efa09f71 100644
--- a/packages/loot-core/src/server/accounts/sync.test.ts
+++ b/packages/loot-core/src/server/accounts/sync.test.ts
@@ -583,6 +583,36 @@ describe('Account sync', () => {
]);
});
+ test('reconcile matches a skipped transaction but does not import it', async () => {
+ const { id } = await prepareDatabase();
+ await reconcileTransactions(id, [
+ { date: '2024-04-05', amount: -899, payee_name: 'Netflix' },
+ ]);
+
+ // The Netflix row matches the existing transaction and is skipped (the
+ // user unchecked it). It must still take the match, so the coffee row
+ // with the same amount is added and not merged into Netflix.
+ const { added, updated } = await reconcileTransactions(id, [
+ {
+ date: '2024-04-05',
+ amount: -899,
+ payee_name: 'Netflix',
+ notes: 'Subscription',
+ skipTransaction: true,
+ },
+ { date: '2024-04-03', amount: -899, payee_name: 'Corner Coffee' },
+ ]);
+
+ expect(added).toHaveLength(1);
+ expect(updated).toHaveLength(0);
+ expect(
+ (await getAllTransactions()).map(t => [t.date, t.payee_name, t.notes]),
+ ).toEqual([
+ [20240405, 'Netflix', null],
+ [20240403, 'Corner Coffee', null],
+ ]);
+ });
+
test("reconcile does not merge transactions with different 'imported_id' values", async () => {
const { id } = await prepareDatabase();
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index c15278cd9e..a23781bd79 100644
--- a/packages/loot-core/src/server/accounts/sync.ts
+++ b/packages/loot-core/src/server/accounts/sync.ts
@@ -668,6 +668,13 @@ export async function reconcileTransactions(
// Finally, generate & commit the changes
for (const { trans, subtransactions, match } of transactionsStep3) {
+ // A skipped transaction still takes part in the matching above, so the
+ // other transactions match the same way as in the import preview. It is
+ // not imported or merged.
+ if (trans.skipTransaction) {
+ continue;
+ }
+
if (match && !trans.forceAddTransaction) {
// Skip updating already reconciled (locked) transactions
if (match.reconciled) {
diff --git a/upcoming-release-notes/csv-import-unchecked-match.md b/upcoming-release-notes/csv-import-unchecked-match.md
new file mode 100644
index 0000000000..a8ee999563
--- /dev/null
+++ b/upcoming-release-notes/csv-import-unchecked-match.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [kikoval]
+---
+
+Fix CSV import that merged a new transaction into an existing one with the same amount after you unchecked a matched row.