Skip to content

A local-first personal finance app

Fixes query for tag starting in $ (#7324)

gust07172bbcbaee7392
diff --git a/packages/loot-core/src/server/transactions/transaction-rules.test.ts b/packages/loot-core/src/server/transactions/transaction-rules.test.ts
index 949f16d8eb..099c501853 100644
--- a/packages/loot-core/src/server/transactions/transaction-rules.test.ts
+++ b/packages/loot-core/src/server/transactions/transaction-rules.test.ts
@@ -510,6 +510,36 @@ describe('Transaction rules', () => {
// todo: isapprox
});
+ test('transactions can be queried by hasTags when tag starts with dollar', async () => {
+ await loadRules();
+ const account = await db.insertAccount({ name: 'bank' });
+ const payeeId = await db.insertPayee({ name: 'payee' });
+
+ await db.insertTransaction({
+ id: '1',
+ date: '2020-10-01',
+ account,
+ payee: payeeId,
+ notes: 'Follow up #$Bug_Test issue',
+ amount: 123,
+ });
+
+ await db.insertTransaction({
+ id: '2',
+ date: '2020-10-01',
+ account,
+ payee: payeeId,
+ notes: 'Follow up #Tag_1 issue',
+ amount: 123,
+ });
+
+ const transactions = await getMatchingTransactions([
+ { field: 'notes', op: 'hasTags', value: '#$Bug_Test' },
+ ]);
+
+ expect(transactions.map(t => t.id)).toEqual(['1']);
+ });
+
test('and sub expression builds $and condition', async () => {
const conds = [{ field: 'category', op: 'is', value: null }];
const { filters } = conditionsToAQL(conds);
diff --git a/packages/loot-core/src/server/transactions/transaction-rules.ts b/packages/loot-core/src/server/transactions/transaction-rules.ts
index 6cbe5087ab..78ffb30af0 100644
--- a/packages/loot-core/src/server/transactions/transaction-rules.ts
+++ b/packages/loot-core/src/server/transactions/transaction-rules.ts
@@ -599,22 +599,26 @@ export function conditionsToAQL(
}
return { $or: values.map(v => apply(field, '$eq', v)) };
- case 'hasTags':
+ case 'hasTags': {
const tagValues = [];
+ const seenTags = new Set();
for (const [_, tag] of value.matchAll(/(?<!#)(#[^#\s]+)/g)) {
- if (!tagValues.find(t => t.tag === tag)) {
+ if (!seenTags.has(tag)) {
+ seenTags.add(tag);
tagValues.push(tag);
}
}
return {
$and: tagValues.map(v => {
- const regex = new RegExp(
- `(?<!#)${v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([\\s#]|$)`,
- );
- return apply(field, '$regexp', regex.source);
+ const escapedTag = v
+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
+ .replace(/\\\$/g, '[$]'); // Use '[$]' instead of '\$' so AQL string unescaping doesn't turn it into a bare '$' end-of-string anchor
+ const pattern = `(?<!#)${escapedTag}([\\s#]|$)`;
+ return apply(field, '$regexp', pattern);
}),
};
+ }
case 'notOneOf':
const notValues = value;
diff --git a/upcoming-release-notes/7324.md b/upcoming-release-notes/7324.md
new file mode 100644
index 0000000000..2a3c4255b8
--- /dev/null
+++ b/upcoming-release-notes/7324.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [gust0717]
+---
+
+Fixes transaction query by tag when tag starts with $