Spaces:
Sleeping
Sleeping
| import { ProfitData, Order, FulfillmentProvider, Shop, ShopSummary, AllShopsSummaryResponse, AllShopsOrder, AllShopsProfitData, ShopSummaryResponse } from '../types'; | |
| // CONFIGURATION | |
| export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL; | |
| if (!API_BASE_URL) { | |
| console.error("CRITICAL: VITE_API_BASE_URL is missing. The app cannot connect to the backend."); | |
| } | |
| // ========================================== | |
| // SHOP MANAGEMENT APIs | |
| // ========================================== | |
| export const fetchShops = async (): Promise<Shop[]> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| }; | |
| export const createShop = async (name: string, slug: string, description?: string): Promise<Shop> => { | |
| const formData = new FormData(); | |
| formData.append('name', name); | |
| formData.append('slug', slug); | |
| if (description) formData.append('description', description); | |
| const response = await fetch(`${API_BASE_URL}/api/shops`, { | |
| method: 'POST', | |
| body: formData | |
| }); | |
| if (!response.ok) { | |
| const error = await response.json().catch(() => ({ detail: response.statusText })); | |
| throw new Error(error.detail || 'Failed to create shop'); | |
| } | |
| return await response.json(); | |
| }; | |
| export const updateShop = async ( | |
| shopId: number, | |
| data: { name?: string; description?: string; is_active?: boolean } | |
| ): Promise<void> => { | |
| const formData = new FormData(); | |
| if (data.name) formData.append('name', data.name); | |
| if (data.description !== undefined) formData.append('description', data.description); | |
| if (data.is_active !== undefined) formData.append('is_active', data.is_active.toString()); | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}`, { | |
| method: 'PUT', | |
| body: formData | |
| }); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| }; | |
| export const deleteShop = async (shopId: number): Promise<void> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}`, { | |
| method: 'DELETE' | |
| }); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| }; | |
| export const resetShopData = async (shopId: number): Promise<void> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/reset`, { | |
| method: 'POST' | |
| }); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| }; | |
| // ========================================== | |
| // SHOP-SCOPED DATA APIs | |
| // ========================================== | |
| export const fetchShopSummary = async (shopId: number, startDate?: string, endDate?: string): Promise<ShopSummaryResponse> => { | |
| let url = `${API_BASE_URL}/api/shops/${shopId}/analytics/summary`; | |
| const params = new URLSearchParams(); | |
| if (startDate) params.append('start_date', startDate); | |
| if (endDate) params.append('end_date', endDate); | |
| if (params.toString()) url += `?${params.toString()}`; | |
| const response = await fetch(url); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const fetchProfitData = async (shopId: number): Promise<ProfitData[]> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/analytics/net-profit`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| }; | |
| export const fetchOrders = async (shopId: number): Promise<Order[]> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/orders`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const uploadFile = async (shopId: number, type: 'etsy' | 'fulfillment', file: File, providerId?: number) => { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| if (providerId) { | |
| formData.append('provider_id', providerId.toString()); | |
| } | |
| const endpoint = type === 'etsy' | |
| ? `/api/shops/${shopId}/upload/etsy` | |
| : `/api/shops/${shopId}/upload/fulfillment`; | |
| const response = await fetch(`${API_BASE_URL}${endpoint}`, { | |
| method: 'POST', | |
| body: formData, | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`Upload failed: ${response.statusText}`); | |
| } | |
| return await response.json(); | |
| }; | |
| export const createProvider = async (shopId: number, name: string, mapping: any) => { | |
| const formData = new FormData(); | |
| formData.append('name', name); | |
| formData.append('mapping_config', JSON.stringify(mapping)); | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/providers`, { | |
| method: 'POST', | |
| body: formData | |
| }); | |
| if (!response.ok) throw new Error('Failed to create provider'); | |
| return await response.json(); | |
| } | |
| export const fetchProviders = async (shopId: number): Promise<FulfillmentProvider[]> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/providers`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const fetchImportHistory = async (shopId: number, type: string, providerId?: number): Promise<any[]> => { | |
| let url = `${API_BASE_URL}/api/shops/${shopId}/imports?import_type=${type}`; | |
| if (providerId) { | |
| url += `&provider_id=${providerId}`; | |
| } | |
| const response = await fetch(url); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const fetchImportDetails = async (shopId: number, id: number): Promise<{ id: number, file_name: string, raw_content: string }> => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/imports/${id}`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const deleteImport = async (shopId: number, id: number) => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/imports/${id}`, { | |
| method: 'DELETE' | |
| }); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const runMatching = async (shopId: number) => { | |
| const response = await fetch(`${API_BASE_URL}/api/shops/${shopId}/actions/run-matching`, { | |
| method: 'POST' | |
| }); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| // ========================================== | |
| // CROSS-SHOP ANALYTICS APIs | |
| // ========================================== | |
| export const fetchAllShopsSummary = async (startDate?: string, endDate?: string): Promise<AllShopsSummaryResponse> => { | |
| let url = `${API_BASE_URL}/api/analytics/all-shops/summary`; | |
| const params = new URLSearchParams(); | |
| if (startDate) params.append('start_date', startDate); | |
| if (endDate) params.append('end_date', endDate); | |
| if (params.toString()) url += `?${params.toString()}`; | |
| const response = await fetch(url); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const fetchAllShopsOrders = async (): Promise<AllShopsOrder[]> => { | |
| const response = await fetch(`${API_BASE_URL}/api/analytics/all-shops/orders`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| export const fetchAllShopsProfitData = async (): Promise<AllShopsProfitData[]> => { | |
| const response = await fetch(`${API_BASE_URL}/api/analytics/all-shops/profit`); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |
| // ========================================== | |
| // LEGACY/ADMIN APIs | |
| // ========================================== | |
| export const resetDatabase = async () => { | |
| const response = await fetch(`${API_BASE_URL}/api/admin/reset-database`, { | |
| method: 'POST' | |
| }); | |
| if (!response.ok) throw new Error(`Backend error: ${response.statusText}`); | |
| return await response.json(); | |
| } | |