@baruchiro/actual-mcp
Version:
Actual Budget MCP server exposing API functionality
63 lines • 2.15 kB
JavaScript
/**
* Get date range parameters with defaults
*/
export function getDateRange(startDate, endDate) {
const today = new Date();
const defaultStartDate = new Date();
defaultStartDate.setMonth(today.getMonth() - 3); // 3 months ago by default
return {
startDate: startDate || formatDate(defaultStartDate),
endDate: endDate || formatDate(today),
};
}
/**
* Format a date as YYYY-MM-DD
*/
export function formatDate(date) {
if (!date)
return '';
if (typeof date === 'string')
return date;
const d = new Date(date);
return d.toISOString().split('T')[0];
}
/**
* Format currency amounts for display
*/
export function formatAmount(amount) {
if (amount === undefined || amount === null)
return 'N/A';
// Convert from cents to dollars
const dollars = amount / 100;
const currencyCode = process.env.ACTUAL_MCP_CURRENCY_SYMBOL;
try {
if (currencyCode?.trim()) {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currencyCode.trim(),
}).format(dollars);
}
else {
return new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(dollars);
}
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to format amount with currency code "${currencyCode}": ${errorMessage}. ` +
`Please provide a valid ISO 4217 currency code (e.g., "USD", "EUR", "GBP").`);
}
}
// Helper to calculate start/end date strings for the N most recent months
export function getDateRangeForMonths(months) {
const now = new Date();
const end = new Date(now.getFullYear(), now.getMonth() + 1, 0); // last day of current month
const start = new Date(end.getFullYear(), end.getMonth() - months + 1, 1); // first day of N months ago
return {
start: start.toISOString().slice(0, 10),
end: end.toISOString().slice(0, 10),
};
}
//# sourceMappingURL=utils.js.map