A local-first personal finance app
fix: disable oneOf and notOneOf operations for Notes filter (#6567)
lifbb70074f355d
diff --git a/packages/loot-core/src/server/rules/index.test.ts b/packages/loot-core/src/server/rules/index.test.tsindex 13b23c4bc2..1526e283bd 100644--- a/packages/loot-core/src/server/rules/index.test.ts+++ b/packages/loot-core/src/server/rules/index.test.ts@@ -37,8 +37,8 @@ describe('Condition', () => {cond = new Condition('matches', 'notes', '^fo*$', null);expect(cond.eval({ notes: null })).toBe(false);- cond = new Condition('oneOf', 'notes', ['foo'], null);- expect(cond.eval({ notes: null })).toBe(false);+ cond = new Condition('oneOf', 'imported_payee', ['foo'], null);+ expect(cond.eval({ imported_payee: null })).toBe(false);['gt', 'gte', 'lt', 'lte', 'isapprox'].forEach(op => {const cond = new Condition(op, 'date', '2020-01-01', null);@@ -202,11 +202,11 @@ describe('Condition', () => {expect(cond.eval({ notes: 'FOO' })).toBe(true);expect(cond.eval({ notes: 'foo2' })).toBe(false);- cond = new Condition('oneOf', 'notes', ['foo', 'bar'], null);- expect(cond.eval({ notes: 'foo' })).toBe(true);- expect(cond.eval({ notes: 'FOO' })).toBe(true);- expect(cond.eval({ notes: 'Bar' })).toBe(true);- expect(cond.eval({ notes: 'bar2' })).toBe(false);+ cond = new Condition('oneOf', 'imported_payee', ['foo', 'bar'], null);+ expect(cond.eval({ imported_payee: 'foo' })).toBe(true);+ expect(cond.eval({ imported_payee: 'FOO' })).toBe(true);+ expect(cond.eval({ imported_payee: 'Bar' })).toBe(true);+ expect(cond.eval({ imported_payee: 'bar2' })).toBe(false);cond = new Condition('contains', 'notes', 'foo', null);expect(cond.eval({ notes: 'bar foo baz' })).toBe(true);@@ -886,7 +886,9 @@ describe('Rule', () => {rules = [rule('id1', [{ op: 'contains', field: 'notes', value: 'sar' }]),- rule('id2', [{ op: 'oneOf', field: 'notes', value: ['jim', 'sar'] }]),+ rule('id2', [+ { op: 'oneOf', field: 'imported_payee', value: ['jim', 'sar'] },+ ]),rule('id3', [{ op: 'is', field: 'notes', value: 'James' }]),rule('id4', [{ op: 'is', field: 'notes', value: 'James' },@@ -1052,12 +1054,19 @@ describe('RuleIndexer', () => {});test('indexing works with the oneOf operator', () => {- const indexer = new RuleIndexer({ field: 'notes', method: 'firstchar' });+ const indexer = new RuleIndexer({+ field: 'imported_payee',+ method: 'firstchar',+ });const rule = new Rule({conditionsOp: 'and',conditions: [- { op: 'oneOf', field: 'notes', value: ['James', 'Sarah', 'Evy'] },+ {+ op: 'oneOf',+ field: 'imported_payee',+ value: ['James', 'Sarah', 'Evy'],+ },],actions: [{ op: 'set', field: 'category', value: 'Food' }],});@@ -1065,21 +1074,21 @@ describe('RuleIndexer', () => {const rule2 = new Rule({conditionsOp: 'and',- conditions: [{ op: 'is', field: 'notes', value: 'Georgia' }],+ conditions: [{ op: 'is', field: 'imported_payee', value: 'Georgia' }],actions: [{ op: 'set', field: 'category', value: 'Food' }],});indexer.index(rule2);- expect(indexer.getApplicableRules({ notes: 'James' })).toEqual(+ expect(indexer.getApplicableRules({ imported_payee: 'James' })).toEqual(new Set([rule]),);- expect(indexer.getApplicableRules({ notes: 'Evy' })).toEqual(+ expect(indexer.getApplicableRules({ imported_payee: 'Evy' })).toEqual(new Set([rule]),);- expect(indexer.getApplicableRules({ notes: 'Charlotte' })).toEqual(+ expect(indexer.getApplicableRules({ imported_payee: 'Charlotte' })).toEqual(new Set([]),);- expect(indexer.getApplicableRules({ notes: 'Georgia' })).toEqual(+ expect(indexer.getApplicableRules({ imported_payee: 'Georgia' })).toEqual(new Set([rule2]),);});diff --git a/packages/loot-core/src/server/transactions/transaction-rules.test.ts b/packages/loot-core/src/server/transactions/transaction-rules.test.tsindex a4241ffcb6..a5062bb434 100644--- a/packages/loot-core/src/server/transactions/transaction-rules.test.ts+++ b/packages/loot-core/src/server/transactions/transaction-rules.test.ts@@ -451,9 +451,9 @@ describe('Transaction rules', () => {expect(transactions.map(t => t.id)).toEqual(['2', '3']);transactions = await getMatchingTransactions([- { field: 'notes', op: 'oneOf', value: ['fooo', 'barr'] },+ { field: 'notes', op: 'is', value: 'barr' },]);- expect(transactions.map(t => t.id)).toEqual(['2', '3', '1']);+ expect(transactions.map(t => t.id)).toEqual(['1']);transactions = await getMatchingTransactions([{ field: 'notes', op: 'is', value: '' },diff --git a/packages/loot-core/src/shared/rules.test.ts b/packages/loot-core/src/shared/rules.test.tsnew file mode 100644index 0000000000..1c16749caf--- /dev/null+++ b/packages/loot-core/src/shared/rules.test.ts@@ -0,0 +1,56 @@+import { describe, expect, it } from 'vitest';++import { getValidOps, isValidOp } from './rules';++describe('rules', () => {+ describe('isValidOp', () => {+ it('should return false for oneOf operation on notes field', () => {+ expect(isValidOp('notes', 'oneOf')).toBe(false);+ });++ it('should return false for notOneOf operation on notes field', () => {+ expect(isValidOp('notes', 'notOneOf')).toBe(false);+ });++ it('should return true for valid string operations on notes field', () => {+ expect(isValidOp('notes', 'is')).toBe(true);+ expect(isValidOp('notes', 'isNot')).toBe(true);+ expect(isValidOp('notes', 'contains')).toBe(true);+ expect(isValidOp('notes', 'doesNotContain')).toBe(true);+ expect(isValidOp('notes', 'matches')).toBe(true);+ expect(isValidOp('notes', 'hasTags')).toBe(true);+ });++ it('should return true for oneOf operation on payee field', () => {+ expect(isValidOp('payee', 'oneOf')).toBe(true);+ });++ it('should return true for notOneOf operation on payee field', () => {+ expect(isValidOp('payee', 'notOneOf')).toBe(true);+ });+ });++ describe('getValidOps', () => {+ it('should not include oneOf and notOneOf for notes field', () => {+ const validOps = getValidOps('notes');+ expect(validOps).not.toContain('oneOf');+ expect(validOps).not.toContain('notOneOf');+ });++ it('should include other valid string operations for notes field', () => {+ const validOps = getValidOps('notes');+ expect(validOps).toContain('is');+ expect(validOps).toContain('isNot');+ expect(validOps).toContain('contains');+ expect(validOps).toContain('doesNotContain');+ expect(validOps).toContain('matches');+ expect(validOps).toContain('hasTags');+ });++ it('should include oneOf and notOneOf for payee field', () => {+ const validOps = getValidOps('payee');+ expect(validOps).toContain('oneOf');+ expect(validOps).toContain('notOneOf');+ });+ });+});diff --git a/packages/loot-core/src/shared/rules.ts b/packages/loot-core/src/shared/rules.tsindex ea1c0db63c..0c096f6161 100644--- a/packages/loot-core/src/shared/rules.ts+++ b/packages/loot-core/src/shared/rules.ts@@ -68,7 +68,7 @@ const FIELD_INFO = {payee: { type: 'id', disallowedOps: new Set(['onBudget', 'offBudget']) },payee_name: { type: 'string' },date: { type: 'date' },- notes: { type: 'string' },+ notes: { type: 'string', disallowedOps: new Set(['oneOf', 'notOneOf']) },amount: { type: 'number' },category: {type: 'id',diff --git a/packages/loot-core/src/types/models/rule.ts b/packages/loot-core/src/types/models/rule.tsindex 91546b8bc1..3819b69384 100644--- a/packages/loot-core/src/types/models/rule.ts+++ b/packages/loot-core/src/types/models/rule.ts@@ -86,14 +86,7 @@ export type RuleConditionEntity =>| BaseConditionEntity<'notes',- | 'is'- | 'isNot'- | 'oneOf'- | 'notOneOf'- | 'contains'- | 'doesNotContain'- | 'matches'- | 'hasTags'+ 'is' | 'isNot' | 'contains' | 'doesNotContain' | 'matches' | 'hasTags'>| BaseConditionEntity<'payee',diff --git a/upcoming-release-notes/6567.md b/upcoming-release-notes/6567.mdnew file mode 100644index 0000000000..bc5ef428f5--- /dev/null+++ b/upcoming-release-notes/6567.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [majiayu000]+---++Fix crash when changing Notes filter to "one of" or "not one of" operation