A local-first personal finance app
[AI] Fix append/prepend notes rules on new transactions (#8300)
Matiss Janis Aboltins98c096a3d06a
diff --git a/packages/desktop-client/package.json b/packages/desktop-client/package.jsonindex 8d2f9d6591..20d7330f85 100644--- a/packages/desktop-client/package.json+++ b/packages/desktop-client/package.json@@ -72,6 +72,7 @@"#components/reports/util": "./src/components/reports/util.ts","#components/schedules": "./src/components/schedules/index.tsx","#components/schedules/schedule-edit-utils": "./src/components/schedules/schedule-edit-utils.ts",+ "#components/transactions/table/utils": "./src/components/transactions/table/utils.ts","#components/util/accountValidation": "./src/components/util/accountValidation.ts","#components/util/countries": "./src/components/util/countries.ts","#components/util/localeToCountry": "./src/components/util/localeToCountry.ts",diff --git a/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx b/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsxindex 568c035869..9389e72f9f 100644--- a/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx+++ b/packages/desktop-client/src/components/mobile/transactions/TransactionEdit.tsx@@ -81,6 +81,7 @@ import {} from '#components/mobile/MobileForms';import { getPrettyPayee } from '#components/mobile/utils';import { MobilePageHeader, Page } from '#components/Page';+import { shouldApplyRuleChange } from '#components/transactions/table/utils';import { createSingleTimeScheduleFromTransaction } from '#components/transactions/TransactionList';import { useAccounts } from '#hooks/useAccounts';import { useCategories } from '#hooks/useCategories';@@ -1778,15 +1779,13 @@ function TransactionEditUnconnected({if (diff) {Object.keys(diff).forEach(key => {const field = key as keyof TransactionEntity;- // Update "empty" fields in general+ // Apply rule changes to "empty" fields and append/prepend notes rules+ // (see shouldApplyRuleChange).// Or update all fields if the payee changes (assists location-based entry by// applying rules to prefill category, notes, etc. based on the selected payee)if (- newTransaction[field] == null ||- newTransaction[field] === '' ||- newTransaction[field] === 0 ||- newTransaction[field] === false ||- updatedField === 'payee'+ updatedField === 'payee' ||+ shouldApplyRuleChange(field, newTransaction[field], diff[field])) {(newTransaction as Record<string, unknown>)[field] = diff[field];changedFields.add(field);diff --git a/packages/desktop-client/src/components/transactions/TransactionList.tsx b/packages/desktop-client/src/components/transactions/TransactionList.tsxindex 5e8e4f179f..dc5b27da05 100644--- a/packages/desktop-client/src/components/transactions/TransactionList.tsx+++ b/packages/desktop-client/src/components/transactions/TransactionList.tsx@@ -41,6 +41,7 @@ import { pushModal } from '#modals/modalsSlice';import { addNotification } from '#notifications/notificationsSlice';import { useDispatch } from '#redux';+import { shouldApplyRuleChange } from './table/utils';import { TransactionTable } from './TransactionsTable';import type { TransactionTableProps } from './TransactionsTable';// When data changes, there are two ways to update the UI:@@ -541,10 +542,7 @@ export function TransactionList({if (diff) {Object.keys(diff).forEach(field => {if (- newTransaction[field] == null ||- newTransaction[field] === '' ||- newTransaction[field] === 0 ||- newTransaction[field] === false+ shouldApplyRuleChange(field, newTransaction[field], diff[field])) {newTransaction[field] = diff[field];}diff --git a/packages/desktop-client/src/components/transactions/table/utils.test.ts b/packages/desktop-client/src/components/transactions/table/utils.test.tsnew file mode 100644index 0000000000..449e03e75c--- /dev/null+++ b/packages/desktop-client/src/components/transactions/table/utils.test.ts@@ -0,0 +1,65 @@+import { shouldApplyRuleChange } from './utils';++describe('shouldApplyRuleChange', () => {+ test('applies rule changes to empty fields', () => {+ expect(shouldApplyRuleChange('category', null, 'food')).toBe(true);+ expect(shouldApplyRuleChange('notes', '', 'memo')).toBe(true);+ expect(shouldApplyRuleChange('cleared', false, true)).toBe(true);+ expect(shouldApplyRuleChange('amount', 0, 1200)).toBe(true);+ });++ test('keeps user-entered values for non-empty fields by default', () => {+ expect(shouldApplyRuleChange('category', 'food', 'home')).toBe(false);+ expect(shouldApplyRuleChange('notes', 'manual note', 'rule note')).toBe(+ false,+ );+ });++ test('applies append and prepend notes rules', () => {+ expect(+ shouldApplyRuleChange('notes', 'Coffee and cake', 'Coffee and cake Tip'),+ ).toBe(true);+ expect(+ shouldApplyRuleChange('notes', 'Coffee and cake', 'Tip Coffee and cake'),+ ).toBe(true);+ });++ test('applies appends with no separator between the note and added text', () => {+ expect(shouldApplyRuleChange('notes', 'Coffee', 'CoffeePAID')).toBe(true);+ });++ test('applies a combined prepend and append in a single rule run', () => {+ expect(shouldApplyRuleChange('notes', 'Coffee', 'A Coffee B')).toBe(true);+ });++ test('is idempotent: does not re-append text already present', () => {+ // Rules re-run on every keystroke during entry; the second run sees the+ // already-appended note and must not append again.+ expect(+ shouldApplyRuleChange(+ 'notes',+ 'Coffee and cake Tip',+ 'Coffee and cake Tip Tip',+ ),+ ).toBe(false);+ expect(+ shouldApplyRuleChange(+ 'notes',+ 'Tip Coffee and cake',+ 'Tip Tip Coffee and cake',+ ),+ ).toBe(false);+ });++ test('does not apply when the rule replaces the note entirely', () => {+ expect(+ shouldApplyRuleChange('notes', 'manual note', 'completely different'),+ ).toBe(false);+ });++ test('only the notes field is allowed to merge', () => {+ expect(shouldApplyRuleChange('imported_payee', 'Store', 'Store Inc')).toBe(+ false,+ );+ });+});diff --git a/packages/desktop-client/src/components/transactions/table/utils.ts b/packages/desktop-client/src/components/transactions/table/utils.tsindex 7a22f13a26..45063b47ae 100644--- a/packages/desktop-client/src/components/transactions/table/utils.ts+++ b/packages/desktop-client/src/components/transactions/table/utils.ts@@ -120,6 +120,55 @@ export function getDisplayValue<T>(obj: T | null | undefined, name: keyof T) {return obj ? obj[name] : '';}+// Decides whether a rule result should be applied to a field while the user is+// entering a new transaction. By default rules only fill fields the user left+// empty, so their manual input isn't overwritten. The exception is the notes+// field: append/prepend notes rules intentionally preserve the existing note and+// add text before or after it, so we allow those through. The check stays+// idempotent — rules are re-run on every keystroke during entry, so we must not+// re-add text that the previous run already applied.+export function shouldApplyRuleChange(+ field: string,+ currentValue: unknown,+ nextValue: unknown,+) {+ if (+ currentValue == null ||+ currentValue === '' ||+ currentValue === 0 ||+ currentValue === false+ ) {+ return true;+ }++ if (+ field !== 'notes' ||+ typeof currentValue !== 'string' ||+ typeof nextValue !== 'string' ||+ nextValue === currentValue+ ) {+ return false;+ }++ // The rule preserved the user's note only if the result still contains it+ // verbatim. Otherwise treat it as an overwrite and keep the manual note.+ const index = nextValue.indexOf(currentValue);+ if (index === -1) {+ return false;+ }++ const prepended = nextValue.slice(0, index);+ const appended = nextValue.slice(index + currentValue.length);++ // If the note already starts/ends with these exact additions, a previous rule+ // run already applied them — applying again would duplicate the text.+ const alreadyApplied =+ (prepended === '' || currentValue.startsWith(prepended)) &&+ (appended === '' || currentValue.endsWith(appended));++ return !alreadyApplied;+}+export function makeTemporaryTransactions(currentAccountId: AccountEntity['id'] | null | undefined,currentCategoryId: CategoryEntity['id'] | null | undefined,diff --git a/upcoming-release-notes/fix-append-prepend-notes-rules-new-transaction.md b/upcoming-release-notes/fix-append-prepend-notes-rules-new-transaction.mdnew file mode 100644index 0000000000..815c52ac83--- /dev/null+++ b/upcoming-release-notes/fix-append-prepend-notes-rules-new-transaction.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [MatissJanis]+---++Fix append and prepend notes rules being ignored when adding a new transaction that already has a note, without duplicating the added text.