Skip to content

Commit b9399ed

Browse files
authored
Merge pull request #248 from codewithzubair07/feature/financial-insights-api-190
feat: implement financial insights aggregation API (Issue #190)
2 parents b04394d + ab46e20 commit b9399ed

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

app/api/insights/route.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
3+
/**
4+
* Financial Insights Aggregation API
5+
*
6+
* Supported query params:
7+
* ?period=current_month
8+
* ?period=last_3_months
9+
* ?period=last_year
10+
*
11+
* NOTE:
12+
* Currently uses mock transaction data.
13+
* Historical persistence not implemented yet.
14+
*/
15+
16+
type Transaction = {
17+
id: string;
18+
amount: number;
19+
type: "spending" | "savings" | "bill" | "insurance";
20+
category: string;
21+
date: Date;
22+
};
23+
24+
// Mock Data (Replace with DB later)
25+
const transactions: Transaction[] = [
26+
{
27+
id: "1",
28+
amount: 500,
29+
type: "spending",
30+
category: "Food",
31+
date: new Date("2026-02-01"),
32+
},
33+
{
34+
id: "2",
35+
amount: 200,
36+
type: "savings",
37+
category: "Emergency Fund",
38+
date: new Date("2026-02-05"),
39+
},
40+
{
41+
id: "3",
42+
amount: 300,
43+
type: "bill",
44+
category: "Electricity",
45+
date: new Date("2026-01-15"),
46+
},
47+
{
48+
id: "4",
49+
amount: 150,
50+
type: "insurance",
51+
category: "Health Insurance",
52+
date: new Date("2025-12-10"),
53+
},
54+
];
55+
56+
function filterByPeriod(period: string, txs: Transaction[]) {
57+
const now = new Date();
58+
59+
if (period === "current_month") {
60+
return txs.filter(
61+
(t) =>
62+
t.date.getMonth() === now.getMonth() &&
63+
t.date.getFullYear() === now.getFullYear()
64+
);
65+
}
66+
67+
if (period === "last_3_months") {
68+
const past = new Date();
69+
past.setMonth(now.getMonth() - 3);
70+
return txs.filter((t) => t.date >= past);
71+
}
72+
73+
if (period === "last_year") {
74+
const past = new Date();
75+
past.setFullYear(now.getFullYear() - 1);
76+
return txs.filter((t) => t.date >= past);
77+
}
78+
79+
return txs;
80+
}
81+
82+
export async function GET(req: NextRequest) {
83+
const { searchParams } = new URL(req.url);
84+
const period = searchParams.get("period") || "current_month";
85+
86+
const filtered = filterByPeriod(period, transactions);
87+
88+
const spendingTotal = filtered
89+
.filter((t) => t.type === "spending")
90+
.reduce((sum, t) => sum + t.amount, 0);
91+
92+
const savingsTotal = filtered
93+
.filter((t) => t.type === "savings")
94+
.reduce((sum, t) => sum + t.amount, 0);
95+
96+
const billsTotal = filtered
97+
.filter((t) => t.type === "bill")
98+
.reduce((sum, t) => sum + t.amount, 0);
99+
100+
const insuranceTotal = filtered
101+
.filter((t) => t.type === "insurance")
102+
.reduce((sum, t) => sum + t.amount, 0);
103+
104+
// Category Breakdown
105+
const breakdown = filtered.reduce((acc: any, t) => {
106+
acc[t.category] = (acc[t.category] || 0) + t.amount;
107+
return acc;
108+
}, {});
109+
110+
// Trend Data (Monthly grouping)
111+
const trend = filtered.reduce((acc: any, t) => {
112+
const month = `${t.date.getFullYear()}-${t.date.getMonth() + 1}`;
113+
acc[month] = (acc[month] || 0) + t.amount;
114+
return acc;
115+
}, {});
116+
117+
return NextResponse.json({
118+
period,
119+
spendingTotal,
120+
savingsTotal,
121+
billsTotal,
122+
insuranceTotal,
123+
breakdown,
124+
trend,
125+
note:
126+
"Data currently generated from mock transactions. Historical DB integration pending.",
127+
});
128+
}

0 commit comments

Comments
 (0)