Skip to content

A local-first personal finance app

CLI: Hide hidden categories by default in list commands (#7786)

Matiss Janis Aboltins5d270340a50a
diff --git a/packages/api/methods.ts b/packages/api/methods.ts
index 62405e673c..fc1fc149d8 100644
--- a/packages/api/methods.ts
+++ b/packages/api/methods.ts
@@ -204,8 +204,8 @@ export function getAccountBalance(id: APIAccountEntity['id'], cutoff?: Date) {
return send('api/account-balance', { id, cutoff });
}
-export function getCategoryGroups() {
- return send('api/category-groups-get');
+export function getCategoryGroups(options: { hidden?: boolean } = {}) {
+ return send('api/category-groups-get', options);
}
export function createCategoryGroup(group: Omit<APICategoryGroupEntity, 'id'>) {
@@ -226,8 +226,8 @@ export function deleteCategoryGroup(
return send('api/category-group-delete', { id, transferCategoryId });
}
-export function getCategories() {
- return send('api/categories-get', { grouped: false });
+export function getCategories(options: { hidden?: boolean } = {}) {
+ return send('api/categories-get', { grouped: false, ...options });
}
export function createCategory(category: Omit<APICategoryEntity, 'id'>) {
diff --git a/packages/cli/src/commands/categories.test.ts b/packages/cli/src/commands/categories.test.ts
new file mode 100644
index 0000000000..104bb7cef4
--- /dev/null
+++ b/packages/cli/src/commands/categories.test.ts
@@ -0,0 +1,131 @@
+import * as api from '@actual-app/api';
+import { Command } from 'commander';
+
+import { printOutput } from '#output';
+
+import { registerCategoriesCommand } from './categories';
+import { registerCategoryGroupsCommand } from './category-groups';
+
+vi.mock('@actual-app/api', () => ({
+ getCategories: vi.fn().mockResolvedValue([]),
+ createCategory: vi.fn().mockResolvedValue('new-id'),
+ updateCategory: vi.fn().mockResolvedValue(undefined),
+ deleteCategory: vi.fn().mockResolvedValue(undefined),
+ getCategoryGroups: vi.fn().mockResolvedValue([]),
+ createCategoryGroup: vi.fn().mockResolvedValue('new-group-id'),
+ updateCategoryGroup: vi.fn().mockResolvedValue(undefined),
+ deleteCategoryGroup: vi.fn().mockResolvedValue(undefined),
+}));
+
+vi.mock('#connection', () => ({
+ withConnection: vi.fn((_opts, fn) => fn()),
+}));
+
+vi.mock('#output', () => ({
+ printOutput: vi.fn(),
+}));
+
+function createProgram(): Command {
+ const program = new Command();
+ program.option('--format <format>');
+ program.option('--server-url <url>');
+ program.option('--password <pw>');
+ program.option('--session-token <token>');
+ program.option('--sync-id <id>');
+ program.option('--data-dir <dir>');
+ program.option('--verbose');
+ program.exitOverride();
+ registerCategoriesCommand(program);
+ registerCategoryGroupsCommand(program);
+ return program;
+}
+
+async function run(args: string[]) {
+ const program = createProgram();
+ await program.parseAsync(['node', 'test', ...args]);
+}
+
+describe('categories commands', () => {
+ let stderrSpy: ReturnType<typeof vi.spyOn>;
+ let stdoutSpy: ReturnType<typeof vi.spyOn>;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ stderrSpy = vi
+ .spyOn(process.stderr, 'write')
+ .mockImplementation(() => true);
+ stdoutSpy = vi
+ .spyOn(process.stdout, 'write')
+ .mockImplementation(() => true);
+ });
+
+ afterEach(() => {
+ stderrSpy.mockRestore();
+ stdoutSpy.mockRestore();
+ });
+
+ describe('categories list', () => {
+ it('asks the API to exclude hidden categories by default', async () => {
+ await run(['categories', 'list']);
+
+ expect(api.getCategories).toHaveBeenCalledWith({ hidden: false });
+ });
+
+ it('asks the API for all categories when --include-hidden is passed', async () => {
+ await run(['categories', 'list', '--include-hidden']);
+
+ expect(api.getCategories).toHaveBeenCalledWith({});
+ });
+
+ it('prints whatever the API returns', async () => {
+ const visible = {
+ id: '1',
+ name: 'Visible',
+ group_id: 'g1',
+ hidden: false,
+ };
+ vi.mocked(api.getCategories).mockResolvedValue([visible]);
+
+ await run(['categories', 'list']);
+
+ expect(printOutput).toHaveBeenCalledWith([visible], undefined);
+ });
+
+ it('passes format option to printOutput', async () => {
+ vi.mocked(api.getCategories).mockResolvedValue([]);
+
+ await run(['--format', 'csv', 'categories', 'list']);
+
+ expect(printOutput).toHaveBeenCalledWith([], 'csv');
+ });
+ });
+
+ describe('category-groups list', () => {
+ it('asks the API to exclude hidden groups by default', async () => {
+ await run(['category-groups', 'list']);
+
+ expect(api.getCategoryGroups).toHaveBeenCalledWith({ hidden: false });
+ });
+
+ it('asks the API for all groups when --include-hidden is passed', async () => {
+ await run(['category-groups', 'list', '--include-hidden']);
+
+ expect(api.getCategoryGroups).toHaveBeenCalledWith({});
+ });
+
+ it('prints whatever the API returns', async () => {
+ const group = {
+ id: 'g1',
+ name: 'Group',
+ is_income: false,
+ hidden: false,
+ categories: [{ id: 'c1', name: 'Cat', group_id: 'g1', hidden: false }],
+ };
+ vi.mocked(api.getCategoryGroups).mockResolvedValue([group]);
+
+ await run(['category-groups', 'list']);
+
+ expect(printOutput).toHaveBeenCalledWith([group], undefined);
+ });
+ });
+});
diff --git a/packages/cli/src/commands/categories.ts b/packages/cli/src/commands/categories.ts
index 5b95c4502a..b9c372e6a9 100644
--- a/packages/cli/src/commands/categories.ts
+++ b/packages/cli/src/commands/categories.ts
@@ -12,13 +12,16 @@ export function registerCategoriesCommand(program: Command) {
categories
.command('list')
- .description('List all categories')
- .action(async () => {
+ .description('List categories (excludes hidden by default)')
+ .option('--include-hidden', 'Include hidden categories', false)
+ .action(async cmdOpts => {
const opts = program.opts();
await withConnection(
opts,
async () => {
- const result = await api.getCategories();
+ const result = await api.getCategories(
+ cmdOpts.includeHidden ? {} : { hidden: false },
+ );
printOutput(result, opts.format);
},
{ mutates: false },
diff --git a/packages/cli/src/commands/category-groups.ts b/packages/cli/src/commands/category-groups.ts
index 3887085bb6..05dc0f219f 100644
--- a/packages/cli/src/commands/category-groups.ts
+++ b/packages/cli/src/commands/category-groups.ts
@@ -12,13 +12,16 @@ export function registerCategoryGroupsCommand(program: Command) {
groups
.command('list')
- .description('List all category groups')
- .action(async () => {
+ .description('List category groups (excludes hidden by default)')
+ .option('--include-hidden', 'Include hidden groups and categories', false)
+ .action(async cmdOpts => {
const opts = program.opts();
await withConnection(
opts,
async () => {
- const result = await api.getCategoryGroups();
+ const result = await api.getCategoryGroups(
+ cmdOpts.includeHidden ? {} : { hidden: false },
+ );
printOutput(result, opts.format);
},
{ mutates: false },
diff --git a/packages/docs/docs/api/reference.md b/packages/docs/docs/api/reference.md
index c07a8262ba..973d84120a 100644
--- a/packages/docs/docs/api/reference.md
+++ b/packages/docs/docs/api/reference.md
@@ -445,9 +445,13 @@ let accounts = await getAccounts();
#### `getCategories`
-<Method name="getCategories" args={[]} returns="Promise<Category[]>" />
+<Method name="getCategories" args={[{ name: 'options = {}', type: 'object?' }]} returns="Promise<Category[]>" />
-Get all categories.
+Get categories. By default, returns every category.
+
+The `options` object supports:
+
+- `hidden`: filter by hidden status. Pass `false` to return only visible categories, or `true` to return only hidden ones. Omit to return both.
#### `createCategory`
@@ -500,9 +504,13 @@ There should only ever be one income category group,
#### `getCategoryGroups`
-<Method name="getCategoryGroups" args={[]} returns="Promise<CategoryGroup[]>" />
+<Method name="getCategoryGroups" args={[{ name: 'options = {}', type: 'object?' }]} returns="Promise<CategoryGroup[]>" />
+
+Get category groups. By default, returns every group with all of its categories nested under it.
+
+The `options` object supports:
-Get all category groups.
+- `hidden`: filter by hidden status, applied to both groups and their nested categories. Pass `false` to return only visible groups and categories, or `true` to return only hidden ones. Omit to return both.
#### `createCategoryGroup`
diff --git a/packages/loot-core/src/server/api.ts b/packages/loot-core/src/server/api.ts
index bd27a3378d..88f5277b3a 100644
--- a/packages/loot-core/src/server/api.ts
+++ b/packages/loot-core/src/server/api.ts
@@ -630,17 +630,20 @@ handlers['api/account-balance'] = withMutation(async function ({
handlers['api/categories-get'] = async function ({
grouped,
-}: { grouped? } = {}) {
+ hidden,
+}: { grouped?: boolean; hidden?: boolean } = {}) {
checkFileOpen();
- const result = await handlers['get-categories']();
+ const result = await handlers['get-categories']({ hidden });
return grouped
? result.grouped.map(group => categoryGroupModel.toExternal(group))
: result.list.map(category => categoryModel.toExternal(category));
};
-handlers['api/category-groups-get'] = async function () {
+handlers['api/category-groups-get'] = async function ({
+ hidden,
+}: { hidden?: boolean } = {}) {
checkFileOpen();
- const groups = await handlers['get-category-groups']();
+ const groups = await handlers['get-category-groups']({ hidden });
return groups.map(group => categoryGroupModel.toExternal(group));
};
diff --git a/packages/loot-core/src/server/budget/app.ts b/packages/loot-core/src/server/budget/app.ts
index 0d75275050..b05003345b 100644
--- a/packages/loot-core/src/server/budget/app.ts
+++ b/packages/loot-core/src/server/budget/app.ts
@@ -170,11 +170,23 @@ app.method(
app.method('budget/render-note-templates', goalNoteActions.unparse);
// Server must return AQL entities not the raw DB data
-async function getCategories() {
- const categoryGroups = await getCategoryGroups();
+async function getCategories({ hidden }: { hidden?: boolean } = {}) {
+ const categoryGroups = await getCategoryGroups({ hidden });
+ let list: CategoryEntity[];
+ if (hidden === true) {
+ // A hidden category can live in a visible group, so when the caller
+ // explicitly asks for hidden categories the flat list must look beyond
+ // the (already hidden-filtered) groups returned above.
+ const { data }: { data: CategoryEntity[] } = await aqlQuery(
+ q('categories').filter({ hidden: true }).select('*'),
+ );
+ list = data;
+ } else {
+ list = categoryGroups.flatMap(g => g.categories ?? []);
+ }
return {
grouped: categoryGroups,
- list: categoryGroups.flatMap(g => g.categories ?? []),
+ list,
};
}
@@ -388,10 +400,18 @@ async function deleteCategory({
}
// Server must return AQL entities not the raw DB data
-async function getCategoryGroups() {
+async function getCategoryGroups({ hidden }: { hidden?: boolean } = {}) {
+ const baseQuery = q('category_groups').select('*');
+ const query = hidden === undefined ? baseQuery : baseQuery.filter({ hidden });
const { data: categoryGroups }: { data: CategoryGroupEntity[] } =
- await aqlQuery(q('category_groups').select('*'));
- return categoryGroups;
+ await aqlQuery(query);
+ if (hidden === undefined) {
+ return categoryGroups;
+ }
+ return categoryGroups.map(g => ({
+ ...g,
+ categories: g.categories?.filter(c => Boolean(c.hidden) === hidden),
+ }));
}
async function createCategoryGroup({
diff --git a/packages/loot-core/src/types/api-handlers.ts b/packages/loot-core/src/types/api-handlers.ts
index 8b74b194a1..f22a51e381 100644
--- a/packages/loot-core/src/types/api-handlers.ts
+++ b/packages/loot-core/src/types/api-handlers.ts
@@ -164,9 +164,12 @@ export type ApiHandlers = {
'api/categories-get': (arg: {
grouped?: boolean;
+ hidden?: boolean;
}) => Promise<Array<APICategoryGroupEntity | APICategoryEntity>>;
- 'api/category-groups-get': () => Promise<APICategoryGroupEntity[]>;
+ 'api/category-groups-get': (arg?: {
+ hidden?: boolean;
+ }) => Promise<APICategoryGroupEntity[]>;
'api/category-group-create': (arg: {
group: Omit<APICategoryGroupEntity, 'id'>;
diff --git a/upcoming-release-notes/7786.md b/upcoming-release-notes/7786.md
new file mode 100644
index 0000000000..be35ddd15b
--- /dev/null
+++ b/upcoming-release-notes/7786.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [Matissjanis]
+---
+
+CLI: hide hidden categories by default in list commands.