feat(finance): Phase 6 — Budget & Analytics

Add monthly budgets per category with spend-vs-budget dashboard and 6-month trend table.
Includes upsert budget API, monthly analytics endpoint, inline budget editing, and route auth fixes.
This commit is contained in:
2026-03-08 16:57:33 +11:00
parent 93450f7caa
commit d455738732
9 changed files with 650 additions and 8 deletions
+105
View File
@@ -0,0 +1,105 @@
import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { queryRaw } from "@/lib/db";
export async function GET(req: NextRequest) {
const user = await getCurrentUser(req);
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
const { searchParams } = new URL(req.url);
const monthCount = Math.min(Math.max(Number(searchParams.get("months") || "6"), 1), 24);
const now = new Date();
const endDate = new Date(now.getFullYear(), now.getMonth() + 1, 1);
const startDate = new Date(now.getFullYear(), now.getMonth() - monthCount + 1, 1);
const startStr = startDate.toISOString().slice(0, 10);
const endStr = endDate.toISOString().slice(0, 10);
const spendRows = await queryRaw<{
month: string;
category: string;
total_spent: number;
transaction_count: number;
}>(
`SELECT
TO_CHAR(DATE_TRUNC('month', t.transaction_date::date), 'YYYY-MM') as month,
COALESCE(o.category_override, t.category) as category,
SUM(t.amount)::numeric(12,2) as total_spent,
COUNT(*)::int as transaction_count
FROM transactions t
LEFT JOIN transaction_overrides o ON o.transaction_id = t.id
JOIN statements s ON s.id = t.statement_id
WHERE s.owner_id = $1
AND t.transaction_type = 'debit'
AND t.transaction_date >= $2
AND t.transaction_date < $3
GROUP BY 1, 2
ORDER BY 1 DESC, total_spent DESC`,
[user.id, startStr, endStr]
);
const budgetRows = await queryRaw<{ month: string; category: string; amount_limit: number }>(
`SELECT TO_CHAR(month, 'YYYY-MM') as month, category, amount_limit::numeric
FROM budgets
WHERE owner_id = $1 AND month >= $2::date AND month < $3::date`,
[user.id, startStr, endStr]
);
// Build month list (most recent first)
const months: string[] = [];
for (let i = monthCount - 1; i >= 0; i--) {
const d = new Date(now.getFullYear(), now.getMonth() - i, 1);
months.push(`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`);
}
months.reverse();
const spendMap = new Map<string, number>();
const countMap = new Map<string, number>();
const budgetMap = new Map<string, number>();
for (const r of spendRows) {
spendMap.set(`${r.category}:${r.month}`, Number(r.total_spent));
countMap.set(`${r.category}:${r.month}`, r.transaction_count);
}
for (const r of budgetRows) {
budgetMap.set(`${r.category}:${r.month}`, Number(r.amount_limit));
}
const allCategories = new Set<string>();
for (const r of spendRows) allCategories.add(r.category);
for (const r of budgetRows) allCategories.add(r.category);
const rows = Array.from(allCategories)
.sort()
.map((cat) => {
const spent: Record<string, number> = {};
const budget: Record<string, number> = {};
const txCount: Record<string, number> = {};
for (const m of months) {
const s = spendMap.get(`${cat}:${m}`);
const b = budgetMap.get(`${cat}:${m}`);
const c = countMap.get(`${cat}:${m}`);
if (s !== undefined) spent[m] = s;
if (b !== undefined) budget[m] = b;
if (c !== undefined) txCount[m] = c;
}
return { category: cat, spent, budget, txCount };
});
const totals: Record<string, { spent: number; budget: number }> = {};
for (const m of months) {
let s = 0;
let b = 0;
for (const row of rows) {
s += row.spent[m] || 0;
b += row.budget[m] || 0;
}
totals[m] = {
spent: Math.round(s * 100) / 100,
budget: Math.round(b * 100) / 100,
};
}
return NextResponse.json({ months, rows, totals });
}