@baruchiro/actual-mcp
Version:
Actual Budget MCP server exposing API functionality
35 lines • 2.1 kB
JavaScript
import { SpendingByCategoryInputParser } from './input-parser.js';
import { SpendingByCategoryDataFetcher } from './data-fetcher.js';
import { CategoryMapper } from '../../core/mapping/category-mapper.js';
import { TransactionGrouper } from '../../core/aggregation/transaction-grouper.js';
import { GroupAggregator } from '../../core/aggregation/group-by.js';
import { SpendingByCategoryReportGenerator } from './report-generator.js';
import { success, errorFromCatch } from '../../utils/response.js';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { SpendingByCategoryArgsSchema } from '../../types.js';
export const schema = {
name: 'spending-by-category',
description: 'Get spending breakdown by category for a specified date range',
inputSchema: zodToJsonSchema(SpendingByCategoryArgsSchema),
};
export async function handler(args) {
try {
const input = new SpendingByCategoryInputParser().parse(args);
const { startDate, endDate, accountId, includeIncome } = input;
const { accounts, categories, categoryGroups, transactions } = await new SpendingByCategoryDataFetcher().fetchAll(accountId, startDate, endDate);
const categoryMapper = new CategoryMapper(categories, categoryGroups);
const spendingByCategory = new TransactionGrouper().groupByCategory(transactions, (categoryId) => categoryMapper.getCategoryName(categoryId), (categoryId) => categoryMapper.getGroupInfo(categoryId), includeIncome);
const sortedGroups = new GroupAggregator().aggregateAndSort(spendingByCategory);
let accountLabel = 'Accounts: All on-budget accounts';
if (accountId) {
const account = accounts.find((a) => a.id === accountId);
accountLabel = `Account: ${account ? account.name : accountId}`;
}
const markdown = new SpendingByCategoryReportGenerator().generate(sortedGroups, { start: startDate, end: endDate }, accountLabel, includeIncome);
return success(markdown);
}
catch (err) {
return errorFromCatch(err);
}
}
//# sourceMappingURL=index.js.map