From 2a597fb3b85ebba770db4f33e59d01108465f39a Mon Sep 17 00:00:00 2001 From: Syukron Rifa'il Muttaqi Date: Sat, 23 Sep 2023 02:11:21 +0700 Subject: [PATCH] fix: filter empty note (#1708) --- .../loot-core/src/server/accounts/rules.ts | 2 +- .../server/accounts/transaction-rules.test.ts | 27 ++++++++++++++++--- .../src/server/accounts/transaction-rules.ts | 6 +++++ packages/loot-core/src/shared/rules.ts | 2 +- upcoming-release-notes/1708.md | 6 +++++ 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 upcoming-release-notes/1708.md diff --git a/packages/loot-core/src/server/accounts/rules.ts b/packages/loot-core/src/server/accounts/rules.ts index 635f508e1f7..2d09078872b 100644 --- a/packages/loot-core/src/server/accounts/rules.ts +++ b/packages/loot-core/src/server/accounts/rules.ts @@ -131,7 +131,7 @@ let CONDITION_TYPES = { }, string: { ops: ['is', 'contains', 'oneOf', 'isNot', 'doesNotContain', 'notOneOf'], - nullable: false, + nullable: true, parse(op, value, fieldName) { if (op === 'oneOf' || op === 'notOneOf') { assert( diff --git a/packages/loot-core/src/server/accounts/transaction-rules.test.ts b/packages/loot-core/src/server/accounts/transaction-rules.test.ts index 50211264f6e..9827e5cd2c6 100644 --- a/packages/loot-core/src/server/accounts/transaction-rules.test.ts +++ b/packages/loot-core/src/server/accounts/transaction-rules.test.ts @@ -402,6 +402,22 @@ describe('Transaction rules', () => { notes: 'FooO', amount: -322, }); + await db.insertTransaction({ + id: '4', + date: '2020-10-16', + account, + payee: lowesId, + notes: null, + amount: 101, + }); + await db.insertTransaction({ + id: '5', + date: '2020-10-16', + account, + payee: lowesId, + notes: '', + amount: 124, + }); let transactions = await getMatchingTransactions([ { field: 'date', op: 'is', value: '2020-10-15' }, @@ -411,7 +427,7 @@ describe('Transaction rules', () => { transactions = await getMatchingTransactions([ { field: 'payee', op: 'is', value: lowesId }, ]); - expect(transactions.map(t => t.id)).toEqual(['3']); + expect(transactions.map(t => t.id)).toEqual(['4', '5', '3']); transactions = await getMatchingTransactions([ { field: 'amount', op: 'is', value: 353 }, @@ -433,6 +449,11 @@ describe('Transaction rules', () => { ]); expect(transactions.map(t => t.id)).toEqual(['2', '3', '1']); + transactions = await getMatchingTransactions([ + { field: 'notes', op: 'is', value: '' }, + ]); + expect(transactions.map(t => t.id)).toEqual(['4', '5']); + transactions = await getMatchingTransactions([ { field: 'amount', op: 'gt', value: 300 }, ]); @@ -454,7 +475,7 @@ describe('Transaction rules', () => { transactions = await getMatchingTransactions([ { field: 'amount', op: 'gt', value: -1000, options: { inflow: true } }, ]); - expect(transactions.map(t => t.id)).toEqual(['2', '1']); + expect(transactions.map(t => t.id)).toEqual(['4', '5', '2', '1']); // Same thing for `outflow`: never return `inflow` transactions transactions = await getMatchingTransactions([ @@ -465,7 +486,7 @@ describe('Transaction rules', () => { transactions = await getMatchingTransactions([ { field: 'date', op: 'gt', value: '2020-10-10' }, ]); - expect(transactions.map(t => t.id)).toEqual(['2', '3']); + expect(transactions.map(t => t.id)).toEqual(['4', '5', '2', '3']); // todo: isapprox }); diff --git a/packages/loot-core/src/server/accounts/transaction-rules.ts b/packages/loot-core/src/server/accounts/transaction-rules.ts index 758d7f44bb9..4761375a32a 100644 --- a/packages/loot-core/src/server/accounts/transaction-rules.ts +++ b/packages/loot-core/src/server/accounts/transaction-rules.ts @@ -419,6 +419,12 @@ export function conditionsToAQL(conditions, { recurDateBounds = 100 } = {}) { }; } return apply(field, '$eq', number); + } else if (type === 'string') { + if (value === '') { + return { + $or: [apply(field, '$eq', null), apply(field, '$eq', '')], + }; + } } return apply(field, '$eq', value); case 'isNot': diff --git a/packages/loot-core/src/shared/rules.ts b/packages/loot-core/src/shared/rules.ts index 53b143d61a3..d0fa730a44a 100644 --- a/packages/loot-core/src/shared/rules.ts +++ b/packages/loot-core/src/shared/rules.ts @@ -17,7 +17,7 @@ export const TYPE_INFO = { }, string: { ops: ['is', 'contains', 'oneOf', 'isNot', 'doesNotContain', 'notOneOf'], - nullable: false, + nullable: true, }, number: { ops: ['is', 'isapprox', 'isbetween', 'gt', 'gte', 'lt', 'lte'], diff --git a/upcoming-release-notes/1708.md b/upcoming-release-notes/1708.md new file mode 100644 index 00000000000..faae4287d45 --- /dev/null +++ b/upcoming-release-notes/1708.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [syukronrm] +--- + +fixing filter transaction to show empty note instead of showing error "Value cannot be empty"