Skip to content

A local-first personal finance app

[AI] Fix category group filtering for budgeted custom reports (#7629)

Aryan Katiyara7e100276ee6
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.test.ts b/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.test.ts
new file mode 100644
index 0000000000..12a04fdff0
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.test.ts
@@ -0,0 +1,102 @@
+import type {
+ CategoryEntity,
+ CategoryGroupEntity,
+ RuleConditionEntity,
+} from '@actual-app/core/types/models';
+import { describe, expect, it } from 'vitest';
+
+import { filterCategoriesByConditions } from './budgetDataQuery';
+
+const categoryGroups = [
+ { id: 'group-bills', name: 'Bills' },
+ { id: 'group-fun', name: 'Fun Money' },
+ { id: 'group-savings', name: 'Savings' },
+] as CategoryGroupEntity[];
+
+const categories = [
+ { id: 'cat-rent', name: 'Rent', group: 'group-bills' },
+ { id: 'cat-electric', name: 'Electric', group: 'group-bills' },
+ { id: 'cat-dining', name: 'Dining Out', group: 'group-fun' },
+ { id: 'cat-emergency', name: 'Emergency Fund', group: 'group-savings' },
+] as CategoryEntity[];
+
+describe('filterCategoriesByConditions', () => {
+ it('filters budget categories by category group', () => {
+ const result = filterCategoriesByConditions(
+ categories,
+ categoryGroups,
+ [
+ {
+ field: 'category_group',
+ op: 'is',
+ value: 'group-bills',
+ } as RuleConditionEntity,
+ ],
+ 'and',
+ );
+
+ expect(result.map(category => category.id)).toEqual([
+ 'cat-rent',
+ 'cat-electric',
+ ]);
+ });
+
+ it('supports selecting one of multiple category groups', () => {
+ const result = filterCategoriesByConditions(
+ categories,
+ categoryGroups,
+ [
+ {
+ field: 'category_group',
+ op: 'oneOf',
+ value: ['group-fun', 'group-savings'],
+ } as RuleConditionEntity,
+ ],
+ 'and',
+ );
+
+ expect(result.map(category => category.id)).toEqual([
+ 'cat-dining',
+ 'cat-emergency',
+ ]);
+ });
+
+ it('combines category and category group filters with and', () => {
+ const result = filterCategoriesByConditions(
+ categories,
+ categoryGroups,
+ [
+ {
+ field: 'category_group',
+ op: 'is',
+ value: 'group-bills',
+ } as RuleConditionEntity,
+ {
+ field: 'category',
+ op: 'is',
+ value: 'cat-rent',
+ } as RuleConditionEntity,
+ ],
+ 'and',
+ );
+
+ expect(result.map(category => category.id)).toEqual(['cat-rent']);
+ });
+
+ it('matches text operators against category group names', () => {
+ const result = filterCategoriesByConditions(
+ categories,
+ categoryGroups,
+ [
+ {
+ field: 'category_group',
+ op: 'contains',
+ value: 'fun',
+ } as RuleConditionEntity,
+ ],
+ 'and',
+ );
+
+ expect(result.map(category => category.id)).toEqual(['cat-dining']);
+ });
+});
diff --git a/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.ts b/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.ts
index 4574a2d4b4..9a6e05cfba 100644
--- a/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.ts
+++ b/packages/desktop-client/src/components/reports/spreadsheets/budgetDataQuery.ts
@@ -43,8 +43,9 @@ async function mapWithConcurrency<TItem, TResult>(
return results;
}
-function filterCategoriesByConditions(
+export function filterCategoriesByConditions(
categories: CategoryEntity[],
+ categoryGroups: CategoryGroupEntity[],
conditions: RuleConditionEntity[] | undefined,
conditionsOp: BudgetDataConditionsOp | undefined,
) {
@@ -53,16 +54,29 @@ function filterCategoriesByConditions(
}
const categoryConditions = conditions.filter(
- (cond): cond is Extract<RuleConditionEntity, { field: 'category' }> =>
- !cond.customName && cond.field === 'category',
+ (
+ cond,
+ ): cond is Extract<
+ RuleConditionEntity,
+ { field: 'category' | 'category_group' }
+ > =>
+ !cond.customName &&
+ (cond.field === 'category' || cond.field === 'category_group'),
);
if (categoryConditions.length === 0) {
return categories;
}
+ const categoryGroupNameById = new Map(
+ categoryGroups.map(group => [group.id, group.name] as const),
+ );
+
const isSupportedCondition = (
- condition: Extract<RuleConditionEntity, { field: 'category' }>,
+ condition: Extract<
+ RuleConditionEntity,
+ { field: 'category' | 'category_group' }
+ >,
) => {
if (condition.op === 'is' || condition.op === 'isNot') {
return typeof condition.value === 'string';
@@ -75,6 +89,14 @@ function filterCategoriesByConditions(
);
}
+ if (
+ condition.op === 'contains' ||
+ condition.op === 'doesNotContain' ||
+ condition.op === 'matches'
+ ) {
+ return typeof condition.value === 'string';
+ }
+
return false;
};
@@ -86,22 +108,60 @@ function filterCategoriesByConditions(
const evaluateCondition = (
category: CategoryEntity,
- condition: Extract<RuleConditionEntity, { field: 'category' }>,
+ condition: Extract<
+ RuleConditionEntity,
+ { field: 'category' | 'category_group' }
+ >,
): boolean => {
+ const key =
+ condition.field === 'category_group'
+ ? (category.group ?? '')
+ : category.id;
+ const textValue =
+ condition.field === 'category_group'
+ ? (categoryGroupNameById.get(key) ?? key)
+ : category.name;
+
if (condition.op === 'is') {
- return category.id === condition.value;
+ return key === condition.value;
}
if (condition.op === 'isNot') {
- return category.id !== condition.value;
+ return key !== condition.value;
}
if (condition.op === 'oneOf') {
- return condition.value.includes(category.id);
+ return condition.value.includes(key);
}
if (condition.op === 'notOneOf') {
- return !condition.value.includes(category.id);
+ return !condition.value.includes(key);
+ }
+
+ if (condition.op === 'contains') {
+ return (
+ typeof condition.value === 'string' &&
+ textValue.toLowerCase().includes(condition.value.toLowerCase())
+ );
+ }
+
+ if (condition.op === 'doesNotContain') {
+ return (
+ typeof condition.value === 'string' &&
+ !textValue.toLowerCase().includes(condition.value.toLowerCase())
+ );
+ }
+
+ if (
+ condition.op === 'matches' &&
+ typeof condition.value === 'string' &&
+ condition.value.length <= 256
+ ) {
+ try {
+ return new RegExp(condition.value, 'i').test(textValue);
+ } catch {
+ return false;
+ }
}
return true;
@@ -141,6 +201,7 @@ export async function fetchBudgetData({
const filteredCategories = filterCategoriesByConditions(
categories,
+ categoryGroups,
conditions,
conditionsOp,
);
diff --git a/upcoming-release-notes/7629.md b/upcoming-release-notes/7629.md
new file mode 100644
index 0000000000..6f4ed9afd7
--- /dev/null
+++ b/upcoming-release-notes/7629.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [Kelpy2004]
+---
+
+Fix Category Group filtering for Budgeted custom reports.