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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
import axios from 'axios';
// API base URL
const API_BASE_URL = 'http://localhost:8080/api/v1';
// Create axios instance with defaults
export const api = axios.create({
baseURL: API_BASE_URL,
headers: {
'Content-Type': 'application/json',
},
});
// Add auth interceptor
api.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);
// Handle auth errors
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && error.response.status === 401 && typeof window !== 'undefined') {
localStorage.removeItem('token');
window.location.href = '/login';
}
return Promise.reject(error);
}
);
// Auth API
export const authApi = {
login: async (email: string, password: string) => {
const response = await api.post('/auth/login', { email, password });
const { token } = response.data;
localStorage.setItem('token', token);
return response.data;
},
signup: async (name: string, email: string, password: string) => {
const response = await api.post('/auth/signup', { name, email, password });
return response.data;
},
logout: () => {
localStorage.removeItem('token');
window.location.href = '/login';
}
};
// Account API
export interface Account {
id: number;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
userID: number;
name: string;
type: string;
balance: number;
}
export interface AccountInput {
name: string;
type: string;
balance: number;
}
export const accountApi = {
getAccounts: () => api.get('/accounts').then(res => res.data),
getAccount: (id: number) => api.get(`/accounts/${id}`).then(res => res.data),
createAccount: (account: AccountInput) => api.post('/accounts', account).then(res => res.data),
updateAccount: (id: number, account: Partial<AccountInput>) => api.put(`/accounts/${id}`, account).then(res => res.data),
deleteAccount: (id: number) => api.delete(`/accounts/${id}`).then(res => res.data)
};
// Transaction API
export interface Transaction {
id: number;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
userID: number;
accountID: number | null;
description: string;
amount: number;
type: "Income" | "Expense";
date: string;
category: string;
}
export interface TransactionInput {
description: string;
amount: number;
type: "Income" | "Expense";
date: string; // YYYY-MM-DD format
category: string;
accountId?: number;
}
export interface TransactionFilters {
type?: "Income" | "Expense";
accountId?: number;
category?: string;
startDate?: string; // YYYY-MM-DD format
endDate?: string; // YYYY-MM-DD format
goalId?: number;
}
export const transactionApi = {
getTransactions: (filters?: TransactionFilters) => {
const params: Record<string, string | number | undefined> = {};
if (filters) {
if (filters.type) params.type = filters.type;
if (filters.accountId) params.account_id = filters.accountId;
if (filters.category) params.category = filters.category;
if (filters.startDate) params.start_date = filters.startDate;
if (filters.endDate) params.end_date = filters.endDate;
if (filters.goalId) params.goal_id = filters.goalId;
}
return api.get('/transactions', { params }).then(res => res.data);
},
getTransaction: (id: number) => api.get(`/transactions/${id}`).then(res => res.data),
createTransaction: (transaction: TransactionInput) => api.post('/transactions', transaction).then(res => res.data),
updateTransaction: (id: number, transaction: Partial<TransactionInput>) => api.put(`/transactions/${id}`, transaction).then(res => res.data),
deleteTransaction: (id: number) => api.delete(`/transactions/${id}`).then(res => res.data)
};
// Goal API
export interface Goal {
id: number;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
userID: number;
name: string;
targetAmount: number;
currentAmount: number;
targetDate: string | null;
status: "Active" | "Paused" | "Achieved" | "Cancelled";
}
export interface GoalProgress {
goal: Goal;
percentComplete: number;
amountRemaining: number;
daysRemaining: number;
requiredPerDay: number;
requiredPerMonth: number;
onTrack: boolean;
}
export interface GoalInput {
name: string;
targetAmount: number;
currentAmount?: number;
targetDate?: string; // YYYY-MM-DD format
status?: "Active" | "Paused" | "Achieved" | "Cancelled";
}
export const goalApi = {
getGoals: (status?: "Active" | "Paused" | "Achieved" | "Cancelled") => {
const params = status ? { status } : {};
return api.get('/goals', { params }).then(res => res.data);
},
getGoal: (id: number) => api.get(`/goals/${id}`).then(res => res.data),
createGoal: (goal: GoalInput) => api.post('/goals', goal).then(res => res.data),
updateGoal: (id: number, goal: Partial<GoalInput>) => api.put(`/goals/${id}`, goal).then(res => res.data),
updateGoalProgress: (id: number, currentAmount: number) =>
api.patch(`/goals/${id}/progress`, { currentAmount }).then(res => res.data),
deleteGoal: (id: number) => api.delete(`/goals/${id}`).then(res => res.data),
// New goal progress tracking endpoints
getGoalProgress: (id: number) => api.get(`/goals/${id}/progress`).then(res => res.data),
getAllGoalsProgress: (status?: string) => {
const params = status ? { status } : {};
return api.get('/goals/progress/all', { params }).then(res => res.data);
},
linkTransactionToGoal: (goalId: number, transactionId: number) =>
api.post(`/goals/${goalId}/link-transaction`, { transactionId }).then(res => res.data),
recalculateGoalProgress: (id: number) => api.post(`/goals/${id}/recalculate`).then(res => res.data)
};
// Loan API
export interface Loan {
id: number;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
userID: number;
accountID: number | null;
name: string;
originalAmount: number;
currentBalance: number;
interestRate: number;
startDate: string;
endDate: string;
}
export interface LoanInput {
name: string;
originalAmount: number;
currentBalance: number;
interestRate: number;
startDate: string;
endDate: string;
accountId?: number;
}
export const loanApi = {
getLoans: () => api.get('/loans').then(res => res.data),
getLoan: (id: number) => api.get(`/loans/${id}`).then(res => res.data),
createLoan: (loan: LoanInput) => api.post('/loans', loan).then(res => res.data),
updateLoan: (id: number, loan: Partial<LoanInput>) => api.put(`/loans/${id}`, loan).then(res => res.data),
deleteLoan: (id: number) => api.delete(`/loans/${id}`).then(res => res.data)
};
// User API
export const userApi = {
getProfile: () => api.get('/users/me').then(res => res.data)
};
|