forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics-api.ts
More file actions
178 lines (162 loc) · 4.67 KB
/
Copy pathanalytics-api.ts
File metadata and controls
178 lines (162 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { apiClient } from "./api-client";
import type {
SpendingTrend,
CategoryBreakdown,
TopPartner,
DebtBalance,
HeatmapCell,
TimeDistribution,
DateRange,
} from "../types/analytics";
// ── Mock / seed data used when the backend is unavailable ──────────────
const MOCK_SPENDING_TRENDS: SpendingTrend[] = [
{
period: "2025-09-01",
totalSpent: 420,
transactionCount: 8,
avgTransactionAmount: 52.5,
},
{
period: "2025-10-01",
totalSpent: 580,
transactionCount: 12,
avgTransactionAmount: 48.33,
},
{
period: "2025-11-01",
totalSpent: 340,
transactionCount: 6,
avgTransactionAmount: 56.67,
},
{
period: "2025-12-01",
totalSpent: 710,
transactionCount: 14,
avgTransactionAmount: 50.71,
},
{
period: "2026-01-01",
totalSpent: 490,
transactionCount: 10,
avgTransactionAmount: 49.0,
},
{
period: "2026-02-01",
totalSpent: 620,
transactionCount: 11,
avgTransactionAmount: 56.36,
},
];
const MOCK_CATEGORY_BREAKDOWN: CategoryBreakdown[] = [
{ category: "Food & Dining", amount: 890 },
{ category: "Entertainment", amount: 420 },
{ category: "Transport", amount: 310 },
{ category: "Groceries", amount: 540 },
{ category: "Utilities", amount: 260 },
{ category: "Other", amount: 180 },
];
const MOCK_TOP_PARTNERS: TopPartner[] = [
{ partnerId: "u1", name: "Alice", totalAmount: 650, interactions: 14 },
{ partnerId: "u2", name: "Bob", totalAmount: 420, interactions: 9 },
{ partnerId: "u3", name: "Carol", totalAmount: 380, interactions: 7 },
{ partnerId: "u4", name: "Dave", totalAmount: 290, interactions: 5 },
{ partnerId: "u5", name: "Eve", totalAmount: 180, interactions: 3 },
];
const MOCK_DEBT_BALANCES: DebtBalance[] = [
{ userId: "u1", name: "Alice", amount: 45.0, direction: "owe" },
{ userId: "u2", name: "Bob", amount: 120.5, direction: "owed" },
{ userId: "u3", name: "Carol", amount: 30.0, direction: "owe" },
{ userId: "u4", name: "Dave", amount: 85.0, direction: "owed" },
];
function generateHeatmapMock(): HeatmapCell[] {
const cells: HeatmapCell[] = [];
const now = new Date();
for (let i = 180; i >= 0; i--) {
const d = new Date(now);
d.setDate(d.getDate() - i);
const count = Math.floor(Math.random() * 6);
cells.push({
date: d.toISOString().slice(0, 10),
count,
total: count * (15 + Math.random() * 40),
});
}
return cells;
}
const MOCK_HEATMAP: HeatmapCell[] = generateHeatmapMock();
const MOCK_TIME_DISTRIBUTION: TimeDistribution[] = [
{ label: "Mon", count: 12, amount: 340 },
{ label: "Tue", count: 8, amount: 210 },
{ label: "Wed", count: 15, amount: 480 },
{ label: "Thu", count: 10, amount: 290 },
{ label: "Fri", count: 18, amount: 620 },
{ label: "Sat", count: 22, amount: 780 },
{ label: "Sun", count: 14, amount: 410 },
];
// ── API helpers (fall back to mock data on failure) ────────────────────
async function safeFetch<T>(
url: string,
params: Record<string, string>,
fallback: T,
): Promise<T> {
try {
const res = await apiClient.get<T>(url, { params });
return res.data;
} catch {
return fallback;
}
}
export async function fetchSpendingTrends(
range?: DateRange,
): Promise<SpendingTrend[]> {
return safeFetch(
"/api/analytics/spending-trends",
{
...(range?.dateFrom && { dateFrom: range.dateFrom }),
...(range?.dateTo && { dateTo: range.dateTo }),
},
MOCK_SPENDING_TRENDS,
);
}
export async function fetchCategoryBreakdown(
range?: DateRange,
): Promise<CategoryBreakdown[]> {
return safeFetch(
"/api/analytics/category-breakdown",
{
...(range?.dateFrom && { dateFrom: range.dateFrom }),
...(range?.dateTo && { dateTo: range.dateTo }),
},
MOCK_CATEGORY_BREAKDOWN,
);
}
export async function fetchTopPartners(
range?: DateRange,
): Promise<TopPartner[]> {
return safeFetch(
"/api/analytics/top-partners",
{
...(range?.dateFrom && { dateFrom: range.dateFrom }),
...(range?.dateTo && { dateTo: range.dateTo }),
},
MOCK_TOP_PARTNERS,
);
}
export async function fetchDebtBalances(): Promise<DebtBalance[]> {
// No backend endpoint for this yet – always use mock data
return MOCK_DEBT_BALANCES;
}
export async function fetchHeatmapData(
range?: DateRange,
): Promise<HeatmapCell[]> {
// Derived from spending-trends in a real app; using mock for now
void range;
return MOCK_HEATMAP;
}
export async function fetchTimeDistribution(
range?: DateRange,
): Promise<TimeDistribution[]> {
// Derived from spending-trends in a real app; using mock for now
void range;
return MOCK_TIME_DISTRIBUTION;
}