Skip to content

A local-first personal finance app

🐛 Prevent Pluggy bank sync from repeatedly rewriting transactions (#8392)

lelemm4556e9e24417
diff --git a/packages/loot-core/src/server/accounts/sync.test.ts b/packages/loot-core/src/server/accounts/sync.test.ts
index 314262dc8b..8f2c20a077 100644
--- a/packages/loot-core/src/server/accounts/sync.test.ts
+++ b/packages/loot-core/src/server/accounts/sync.test.ts
@@ -4,11 +4,13 @@ import * as db from '#server/db';
import { loadMappings } from '#server/db/mappings';
import { post } from '#server/post';
import { getServer } from '#server/server-config';
+import { setSyncingMode } from '#server/sync';
import { handlers } from '#server/tests/mockSyncServer';
import { insertRule, loadRules } from '#server/transactions/transaction-rules';
import * as monthUtils from '#shared/months';
import type { SyncedPrefs } from '#types/prefs';
+import { app as accountsApp } from './app';
import {
addTransactions,
reconcileTransactions,
@@ -645,6 +647,91 @@ describe('SimpleFin batch sync', () => {
delete handlers['/simplefin/transactions'];
});
+ test('does not emit transaction CRDT messages when provider category appears later', async () => {
+ const providerAccountId = 'sf-account-1';
+ const acctId = await db.insertAccount({
+ id: 'acct-1',
+ account_id: providerAccountId,
+ name: 'Account 1',
+ account_sync_source: 'simpleFin',
+ });
+
+ const syncTransaction = category => {
+ mockSimpleFinTransactions({
+ [providerAccountId]: {
+ transactions: {
+ all: [
+ {
+ booked: true,
+ ...(category ? { category } : {}),
+ date: '2017-10-02',
+ payeeName: 'Coffee Shop',
+ transactionAmount: {
+ amount: '-12.34',
+ },
+ transactionId: 'provider-tx-1',
+ },
+ ],
+ booked: [],
+ pending: [],
+ },
+ balances: [],
+ startingBalance: 0,
+ },
+ errors: {},
+ });
+
+ return accountsApp.handlers['simplefin-batch-sync']({ ids: [acctId] });
+ };
+
+ setSyncingMode('offline');
+ try {
+ const firstResult = await syncTransaction(null);
+ expect(firstResult[0].res.errors).toHaveLength(0);
+ expect(firstResult[0].res.newTransactions).toHaveLength(2);
+
+ const { count: crdtMessageCount } = await db.first<{ count: number }>(
+ 'SELECT COUNT(*) as count FROM messages_crdt',
+ );
+ expect(crdtMessageCount).toBeGreaterThan(0);
+
+ global.stepForwardInTime();
+ const secondResult = await syncTransaction('provider-category');
+ expect(secondResult[0].res.errors).toHaveLength(0);
+ expect(secondResult[0].res.newTransactions).toHaveLength(0);
+ expect(secondResult[0].res.matchedTransactions).toHaveLength(0);
+
+ const secondSyncMessages = await db.all<db.DbCrdtMessage>(
+ 'SELECT * FROM messages_crdt WHERE id > ? ORDER BY id',
+ [crdtMessageCount],
+ );
+
+ expect(secondSyncMessages).toHaveLength(3);
+ expect(secondSyncMessages.map(message => message.dataset)).toEqual([
+ 'accounts',
+ 'accounts',
+ 'accounts',
+ ]);
+ expect(secondSyncMessages.map(message => message.column).sort()).toEqual([
+ 'balance_current',
+ 'bank_sync_status',
+ 'last_sync',
+ ]);
+ expect(
+ secondSyncMessages.some(message => message.dataset === 'transactions'),
+ ).toBe(false);
+
+ const transactions = await getAllTransactions();
+ const syncedTransaction = transactions.find(
+ transaction => transaction.imported_id === 'provider-tx-1',
+ );
+ expect(syncedTransaction).toBeDefined();
+ expect(syncedTransaction.category).toBeNull();
+ } finally {
+ setSyncingMode('disabled');
+ }
+ });
+
test('returns ACCOUNT_MISSING error when an account is not in the response', async () => {
const presentAccountId = 'sf-account-1';
const missingAccountId = 'sf-account-2';
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index 62db86172b..1d7c19913f 100644
--- a/packages/loot-core/src/server/accounts/sync.ts
+++ b/packages/loot-core/src/server/accounts/sync.ts
@@ -506,6 +506,7 @@ async function normalizeBankSyncTransactions(transactions, acctId) {
? mappingsFromString(customMappingsRaw)
: defaultMappings;
+ const categoryIds = new Set((await db.getCategories()).map(c => c.id));
const normalized = [];
for (const trans of transactions) {
trans.cleared = Boolean(trans.booked);
@@ -556,7 +557,7 @@ async function normalizeBankSyncTransactions(transactions, acctId) {
account: trans.account,
date,
notes: importNotes && notes ? notes.trim().replace(/#/g, '##') : null,
- category: trans.category ?? null,
+ category: categoryIds.has(trans.category) ? trans.category : null,
imported_id,
imported_payee: trans.imported_payee,
cleared: trans.cleared,
diff --git a/packages/sync-server/src/app-pluggyai/app-pluggyai.js b/packages/sync-server/src/app-pluggyai/app-pluggyai.js
index 4a8c3edad9..cf3564d170 100644
--- a/packages/sync-server/src/app-pluggyai/app-pluggyai.js
+++ b/packages/sync-server/src/app-pluggyai/app-pluggyai.js
@@ -236,6 +236,7 @@ app.post(
delete trans.amount;
const finalTrans = { ...flattenObject(trans), ...newTrans };
+
if (newTrans.booked) {
booked.push(finalTrans);
} else {
diff --git a/upcoming-release-notes/pluggy-drop-category.md b/upcoming-release-notes/pluggy-drop-category.md
new file mode 100644
index 0000000000..0d079eb36a
--- /dev/null
+++ b/upcoming-release-notes/pluggy-drop-category.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [lelemm]
+---
+
+Prevent Pluggy bank sync from repeatedly rewriting uncategorized and transfer transactions