A local-first personal finance app
[AI] fix: preserve explicit category on imported transactions (#7185)
api206291e839353fcf
diff --git a/packages/loot-core/src/server/accounts/sync.test.ts b/packages/loot-core/src/server/accounts/sync.test.tsindex 4787f50e6f..9bace99f2e 100644--- a/packages/loot-core/src/server/accounts/sync.test.ts+++ b/packages/loot-core/src/server/accounts/sync.test.ts@@ -477,6 +477,46 @@ describe('Account sync', () => {]);});+ test('addTransactions does not override explicitly provided category', async () => {+ const { id: acctId } = await prepareDatabase();++ await db.insertCategoryGroup({+ id: 'group2',+ name: 'group2',+ });+ const explicitCategoryId = await db.insertCategory({+ id: 'api-cat',+ name: 'API Category',+ cat_group: 'group2',+ });+ const ruleCategoryId = await db.insertCategory({+ id: 'rule-cat',+ name: 'Rule Category',+ cat_group: 'group2',+ });++ const payeeId = await db.insertPayee({ name: 'P' });+ await insertRule({+ stage: null,+ conditionsOp: 'and',+ conditions: [{ op: 'is', field: 'payee', value: payeeId }],+ actions: [{ op: 'set', field: 'category', value: ruleCategoryId }],+ });++ await addTransactions(acctId, [+ {+ date: '2017-10-21',+ payee_name: 'P',+ amount: -2947,+ category: explicitCategoryId,+ },+ ]);++ const [addedTransaction] = await getAllTransactions();+ expect(addedTransaction.category).toBe(explicitCategoryId);+ expect(addedTransaction.category).not.toBe(ruleCategoryId);+ });+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.tsindex 35659642dd..306d07e290 100644--- a/packages/loot-core/src/server/accounts/sync.ts+++ b/packages/loot-core/src/server/accounts/sync.ts@@ -353,6 +353,9 @@ async function normalizeTransactions(// Strip off the irregular propertiesconst { payee_name: originalPayeeName, subtransactions, ...rest } = trans;trans = rest;+ const explicitFields = Object.entries(rest)+ .filter(([, value]) => value != null)+ .map(([field]) => field);let payee_name = originalPayeeName;if (payee_name) {@@ -379,6 +382,7 @@ async function normalizeTransactions(normalized.push({payee_name,+ explicitFields,subtransactions: subtransactions? subtransactions.map(t => ({ ...t, account: acctId })): null,@@ -879,15 +883,28 @@ export async function addTransactions(const accounts: db.DbAccount[] = await db.getAccounts();const accountsMap = new Map(accounts.map(account => [account.id, account]));- for (const { trans: originalTrans, subtransactions } of normalized) {+ for (const {+ trans: originalTrans,+ subtransactions,+ explicitFields,+ } of normalized) {// Run the rulesconst trans = await runRules(originalTrans, accountsMap);+ // Rules should enrich missing fields but not override explicit values provided by API clients.+ const transWithExplicitFields = { ...trans };+ for (const field of explicitFields) {+ transWithExplicitFields[field] = originalTrans[field];+ }+const finalTransaction = {id: uuidv4(),- ...trans,+ ...transWithExplicitFields,account: acctId,- cleared: trans.cleared != null ? trans.cleared : true,+ cleared:+ transWithExplicitFields.cleared != null+ ? transWithExplicitFields.cleared+ : true,};// Add split transactions if they are givendiff --git a/upcoming-release-notes/7185.md b/upcoming-release-notes/7185.mdnew file mode 100644index 0000000000..906c40ea4b--- /dev/null+++ b/upcoming-release-notes/7185.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [api2062]+---++Preserve explicitly provided transaction category when importing transactions with matching payee rules.