Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/app/api/export/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { FinanceService } from '@/lib/finance-service';

export const runtime = 'nodejs';

const toSafeCsvCell = (value: unknown) => {
const text = value == null ? '' : String(value);
const escaped = text.replace(/"/g, '""');
const shouldEscapeFormula = /^[=+\-@]/.test(escaped.trimStart());
return `"${shouldEscapeFormula ? `'${escaped}` : escaped}"`;
};

export async function GET(req: Request) {
try {
const user = getCurrentUser(req);
Expand All @@ -16,18 +23,20 @@ export async function GET(req: Request) {

if (format === 'csv') {
const txs = service.getTransactions(user.id, { limit: 10000 });
let csv = 'Date,Type,Amount,Currency,Account,Category,Merchant,Notes\n';
let csv = 'Date,Type,Amount,Currency,Account,Category,Merchant,Tags,Notes\n';
for (const t of txs as any[]) {
const amt = (t.amount / 100).toFixed(2);
const tagStr = Array.isArray(t.tags) ? t.tags.join('; ') : '';
const line = [
t.date,
t.type,
toSafeCsvCell(t.date),
toSafeCsvCell(t.type),
amt,
t.currency,
`"${(t.account_name || '').replace(/"/g, '""')}"`,
`"${(t.category_name || '').replace(/"/g, '""')}"`,
`"${(t.merchant_name || '').replace(/"/g, '""')}"`,
`"${(t.notes || '').replace(/"/g, '""')}"`,
toSafeCsvCell(t.currency),
toSafeCsvCell(t.account_name),
toSafeCsvCell(t.category_name),
toSafeCsvCell(t.merchant_name),
toSafeCsvCell(tagStr),
toSafeCsvCell(t.notes),
].join(',');
csv += line + '\n';
}
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/reports/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export async function GET(req: Request) {

const service = new FinanceService();
const spendingByCategory = service.getSpendingByCategory(user.id, month);
const spendingByTag = service.getSpendingByTag(user.id, month);
const spendingByMerchant = service.getSpendingByMerchant(user.id, 15);
const cashFlowTrend = service.getMonthlyCashFlowTrend(user.id, 12);
const metrics = service.getDashboardMetrics(user.id, month);
const dailySpending = service.getDailySpending(user.id, month);
const splitAnalytics = service.getSplitTransactionsAnalytics(user.id, month);
const budgetHealth = service.getBudgetHealthSummary(user.id, month);

let comparison = null;
if (compareMonth) {
Expand All @@ -27,11 +28,12 @@ export async function GET(req: Request) {
return NextResponse.json({
selectedMonth: month,
spendingByCategory,
spendingByTag,
spendingByMerchant,
cashFlowTrend,
metrics,
dailySpending,
splitAnalytics,
budgetHealth,
comparison,
});
} catch (error: any) {
Expand Down
Loading
Loading