mitlist/fe/src/services/expenseService.ts
google-labs-jules[bot] 57b913d135 Fix: Correct API endpoint pathing for expenses to resolve 404 errors
The expenses frontend was encountering 404 errors due to mismatched API paths
between the frontend calls and backend routing.

This commit addresses the issue by:

1. Modifying backend API routing in `be/app/api/v1/api.py`:
   - Added a `/financials` prefix to the `financials.router`. Expense endpoints are now served under `/api/v1/financials/expenses`.

2. Updating frontend API configuration in `fe/src/config/api-config.ts`:
   - Prepended `/api/v1` to all paths within the `API_ENDPOINTS.FINANCIALS` object to match the new backend structure (e.g., `API_ENDPOINTS.FINANCIALS.EXPENSES` is now `/api/v1/financials/expenses`).

3. Updating frontend expense service in `fe/src/services/expenseService.ts`:
   - Replaced hardcoded relative URLs with the updated constants from `API_ENDPOINTS.FINANCIALS`.
   - Ensured `API_ENDPOINTS` is correctly imported.

These changes align the frontend API calls with the backend endpoint definitions,
resolving the 404 errors.
2025-06-03 10:04:42 +00:00

66 lines
1.8 KiB
TypeScript

import type { Expense, RecurrencePattern } from '@/types/expense'
import { api, API_ENDPOINTS } from '@/services/api'
export interface CreateExpenseData {
description: string
total_amount: string
currency: string
split_type: string
isRecurring: boolean
recurrencePattern?: {
type: 'daily' | 'weekly' | 'monthly' | 'yearly'
interval: number
daysOfWeek?: number[]
endDate?: string
maxOccurrences?: number
}
list_id?: number
group_id?: number
item_id?: number
paid_by_user_id: number
splits_in?: Array<{
user_id: number
amount: string
percentage?: number
shares?: number
}>
}
export interface UpdateExpenseData extends Partial<CreateExpenseData> {
version: number
}
export const expenseService = {
async createExpense(data: CreateExpenseData): Promise<Expense> {
const response = await api.post<Expense>(API_ENDPOINTS.FINANCIALS.EXPENSES, data)
return response.data
},
async updateExpense(id: number, data: UpdateExpenseData): Promise<Expense> {
const response = await api.put<Expense>(API_ENDPOINTS.FINANCIALS.EXPENSE(id.toString()), data)
return response.data
},
async deleteExpense(id: number): Promise<void> {
await api.delete(API_ENDPOINTS.FINANCIALS.EXPENSE(id.toString()))
},
async getExpense(id: number): Promise<Expense> {
const response = await api.get<Expense>(API_ENDPOINTS.FINANCIALS.EXPENSE(id.toString()))
return response.data
},
async getExpenses(params?: {
list_id?: number
group_id?: number
isRecurring?: boolean
}): Promise<Expense[]> {
const response = await api.get<Expense[]>(API_ENDPOINTS.FINANCIALS.EXPENSES, { params })
return response.data
},
async getRecurringExpenses(): Promise<Expense[]> {
return this.getExpenses({ isRecurring: true })
},
}