import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ApiService { private baseUrl = '/api'; constructor(private http: HttpClient) { } // Accounts getAccounts(): Observable { return this.http.get(`${this.baseUrl}/accounts/`); } createAccount(account: {name: string, balance: number, account_type: string}): Observable { return this.http.post(`${this.baseUrl}/accounts/`, account); } updateAccount(id: number, account: {name: string, balance: number, account_type: string}): Observable { return this.http.put(`${this.baseUrl}/accounts/${id}/`, account); } deleteAccount(id: number): Observable { return this.http.delete(`${this.baseUrl}/accounts/${id}/`); } // Budgets getBudgets(): Observable { return this.http.get(`${this.baseUrl}/budgets/`); } createBudget(budget: any): Observable { return this.http.post(`${this.baseUrl}/budgets/`, budget); } updateBudget(id: number, budget: any): Observable { return this.http.put(`${this.baseUrl}/budgets/${id}/`, budget); } deleteBudget(id: number): Observable { return this.http.delete(`${this.baseUrl}/budgets/${id}/`); } // Transactions getTransactions(): Observable { return this.http.get(`${this.baseUrl}/transactions/`); } createTransaction(transaction: any): Observable { return this.http.post(`${this.baseUrl}/transactions/`, transaction); } updateTransaction(id: number, transaction: any): Observable { return this.http.put(`${this.baseUrl}/transactions/${id}/`, transaction); } deleteTransaction(id: number): Observable { return this.http.delete(`${this.baseUrl}/transactions/${id}/`); } // Expenses getExpenses(): Observable { return this.http.get(`${this.baseUrl}/expenses/`); } createExpense(expense: any): Observable { return this.http.post(`${this.baseUrl}/expenses/`, expense); } updateExpense(id: number, expense: any): Observable { return this.http.put(`${this.baseUrl}/expenses/${id}/`, expense); } deleteExpense(id: number): Observable { return this.http.delete(`${this.baseUrl}/expenses/${id}/`); } // Profile getProfile(): Observable { return this.http.get(`${this.baseUrl}/profile/`); } updateProfile(data: any): Observable { const formData = new FormData(); Object.keys(data).forEach((key) => { if (data[key] !== null && data[key] !== undefined) { formData.append(key, data[key]); } }); return this.http.put(`${this.baseUrl}/profile/`, formData); } deleteProfile(password: string): Observable { return this.http.delete(`${this.baseUrl}/profile/`, { body: { password } }); } changePassword(password: string): Observable { return this.http.post(`${this.baseUrl}/auth/password/`, { password }); } // Deadlines getDeadlines(): Observable { return this.http.get(`${this.baseUrl}/deadlines/`); } createDeadline(d: any): Observable { return this.http.post(`${this.baseUrl}/deadlines/`, d); } updateDeadline(id: number, d: any): Observable { return this.http.put(`${this.baseUrl}/deadlines/${id}/`, d); } deleteDeadline(id: number): Observable { return this.http.delete(`${this.baseUrl}/deadlines/${id}/`); } getICalUrl(): Observable<{ url: string }> { return this.http.get<{ url: string }>(`${this.baseUrl}/calendar/ical-url/`); } search(q: string): Observable> { return this.http.get>(`${this.baseUrl}/search/?q=${encodeURIComponent(q)}`); } getNotifications(): Observable { return this.http.get(`${this.baseUrl}/notifications/`); } markNotificationRead(event_type: string, event_id: number): Observable { return this.http.post(`${this.baseUrl}/notifications/`, { event_type, event_id }); } // 2FA get2FASetup(): Observable<{ secret: string; uri: string }> { return this.http.get<{ secret: string; uri: string }>(`${this.baseUrl}/auth/2fa/setup/`); } enable2FA(code: string): Observable { return this.http.post(`${this.baseUrl}/auth/2fa/enable/`, { code }); } disable2FA(code: string): Observable { return this.http.post(`${this.baseUrl}/auth/2fa/disable/`, { code }); } login2FA(temp_token: string, code: string): Observable<{ access: string; refresh: string; session_key?: string }> { return this.http.post<{ access: string; refresh: string; session_key?: string }>(`${this.baseUrl}/auth/2fa/login/`, { temp_token, code }); } request2FARecovery(temp_token: string): Observable { return this.http.post(`${this.baseUrl}/auth/2fa/recover/`, { temp_token }); } confirm2FARecovery(temp_token: string, recovery_code: string): Observable<{ access: string; refresh: string; session_key?: string }> { return this.http.post<{ access: string; refresh: string; session_key?: string }>(`${this.baseUrl}/auth/2fa/recover/confirm/`, { temp_token, recovery_code }); } // Sessions getSessions(): Observable { return this.http.get(`${this.baseUrl}/auth/sessions/`); } revokeSession(sessionKey: string): Observable { return this.http.delete(`${this.baseUrl}/auth/sessions/${sessionKey}/`); } revokeAllOtherSessions(): Observable { return this.http.delete(`${this.baseUrl}/auth/sessions/revoke-all/`); } // Data export downloadExport(): Observable { return this.http.get(`${this.baseUrl}/export/`, { responseType: 'blob' }); } // Notification preferences updateNotificationPrefs(prefs: { notif_deadlines?: boolean; notif_budget_alerts?: boolean; notif_monthly_summary?: boolean }): Observable { return this.http.patch(`${this.baseUrl}/notifications/prefs/`, prefs); } // Email verification & password reset verifyEmail(token: string): Observable { return this.http.post(`${this.baseUrl}/auth/verify-email/`, { token }); } requestPasswordReset(email: string): Observable { return this.http.post(`${this.baseUrl}/auth/password-reset/`, { email }); } confirmPasswordReset(token: string, password: string): Observable { return this.http.post(`${this.baseUrl}/auth/password-reset/confirm/`, { token, password }); } }