208 lines
4.8 KiB
TypeScript
208 lines
4.8 KiB
TypeScript
|
|
// src/api/services/DataService.ts
|
||
|
|
import { apiClient } from '../base/apiClient';
|
||
|
|
import {
|
||
|
|
ApiResponse,
|
||
|
|
SalesRecord,
|
||
|
|
CreateSalesRequest,
|
||
|
|
WeatherData,
|
||
|
|
TrafficData,
|
||
|
|
} from '../types/api';
|
||
|
|
|
||
|
|
export interface DashboardStats {
|
||
|
|
totalSales: number;
|
||
|
|
totalRevenue: number;
|
||
|
|
lastTrainingDate: string | null;
|
||
|
|
forecastAccuracy: number;
|
||
|
|
totalProducts: number;
|
||
|
|
activeTenants: number;
|
||
|
|
lastDataUpdate: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface UploadResponse {
|
||
|
|
message: string;
|
||
|
|
records_processed: number;
|
||
|
|
errors?: string[];
|
||
|
|
upload_id?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface DataValidation {
|
||
|
|
valid: boolean;
|
||
|
|
errors: string[];
|
||
|
|
warnings: string[];
|
||
|
|
recordCount: number;
|
||
|
|
duplicates: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class DataService {
|
||
|
|
/**
|
||
|
|
* Upload sales history file
|
||
|
|
*/
|
||
|
|
async uploadSalesHistory(
|
||
|
|
file: File,
|
||
|
|
additionalData?: Record<string, any>
|
||
|
|
): Promise<UploadResponse> {
|
||
|
|
const response = await apiClient.upload<ApiResponse<UploadResponse>>(
|
||
|
|
'/data/upload-sales',
|
||
|
|
file,
|
||
|
|
additionalData
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate sales data before upload
|
||
|
|
*/
|
||
|
|
async validateSalesData(file: File): Promise<DataValidation> {
|
||
|
|
const response = await apiClient.upload<ApiResponse<DataValidation>>(
|
||
|
|
'/data/validate-sales',
|
||
|
|
file
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get dashboard statistics
|
||
|
|
*/
|
||
|
|
async getDashboardStats(): Promise<DashboardStats> {
|
||
|
|
const response = await apiClient.get<ApiResponse<DashboardStats>>(
|
||
|
|
'/data/dashboard/stats'
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get sales records
|
||
|
|
*/
|
||
|
|
async getSalesRecords(params?: {
|
||
|
|
startDate?: string;
|
||
|
|
endDate?: string;
|
||
|
|
productName?: string;
|
||
|
|
page?: number;
|
||
|
|
limit?: number;
|
||
|
|
}): Promise<{ records: SalesRecord[]; total: number; page: number; pages: number }> {
|
||
|
|
const response = await apiClient.get<ApiResponse<{
|
||
|
|
records: SalesRecord[];
|
||
|
|
total: number;
|
||
|
|
page: number;
|
||
|
|
pages: number;
|
||
|
|
}>>('/data/sales', { params });
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create single sales record
|
||
|
|
*/
|
||
|
|
async createSalesRecord(record: CreateSalesRequest): Promise<SalesRecord> {
|
||
|
|
const response = await apiClient.post<ApiResponse<SalesRecord>>(
|
||
|
|
'/data/sales',
|
||
|
|
record
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update sales record
|
||
|
|
*/
|
||
|
|
async updateSalesRecord(
|
||
|
|
id: string,
|
||
|
|
updates: Partial<CreateSalesRequest>
|
||
|
|
): Promise<SalesRecord> {
|
||
|
|
const response = await apiClient.put<ApiResponse<SalesRecord>>(
|
||
|
|
`/data/sales/${id}`,
|
||
|
|
updates
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete sales record
|
||
|
|
*/
|
||
|
|
async deleteSalesRecord(id: string): Promise<void> {
|
||
|
|
await apiClient.delete(`/data/sales/${id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get weather data
|
||
|
|
*/
|
||
|
|
async getWeatherData(params?: {
|
||
|
|
startDate?: string;
|
||
|
|
endDate?: string;
|
||
|
|
location?: string;
|
||
|
|
}): Promise<WeatherData[]> {
|
||
|
|
const response = await apiClient.get<ApiResponse<WeatherData[]>>(
|
||
|
|
'/data/weather',
|
||
|
|
{ params }
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get traffic data
|
||
|
|
*/
|
||
|
|
async getTrafficData(params?: {
|
||
|
|
startDate?: string;
|
||
|
|
endDate?: string;
|
||
|
|
location?: string;
|
||
|
|
}): Promise<TrafficData[]> {
|
||
|
|
const response = await apiClient.get<ApiResponse<TrafficData[]>>(
|
||
|
|
'/data/traffic',
|
||
|
|
{ params }
|
||
|
|
);
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get data quality report
|
||
|
|
*/
|
||
|
|
async getDataQuality(): Promise<{
|
||
|
|
salesData: { completeness: number; quality: number; lastUpdate: string };
|
||
|
|
weatherData: { completeness: number; quality: number; lastUpdate: string };
|
||
|
|
trafficData: { completeness: number; quality: number; lastUpdate: string };
|
||
|
|
}> {
|
||
|
|
const response = await apiClient.get<ApiResponse<any>>('/data/quality');
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Export sales data
|
||
|
|
*/
|
||
|
|
async exportSalesData(params?: {
|
||
|
|
startDate?: string;
|
||
|
|
endDate?: string;
|
||
|
|
format?: 'csv' | 'excel';
|
||
|
|
}): Promise<Blob> {
|
||
|
|
const response = await apiClient.get('/data/sales/export', {
|
||
|
|
params,
|
||
|
|
responseType: 'blob',
|
||
|
|
});
|
||
|
|
return response as unknown as Blob;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get product list
|
||
|
|
*/
|
||
|
|
async getProducts(): Promise<string[]> {
|
||
|
|
const response = await apiClient.get<ApiResponse<string[]>>('/data/products');
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get data sync status
|
||
|
|
*/
|
||
|
|
async getSyncStatus(): Promise<{
|
||
|
|
weather: { lastSync: string; status: 'ok' | 'error'; nextSync: string };
|
||
|
|
traffic: { lastSync: string; status: 'ok' | 'error'; nextSync: string };
|
||
|
|
}> {
|
||
|
|
const response = await apiClient.get<ApiResponse<any>>('/data/sync/status');
|
||
|
|
return response.data!;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Trigger manual data sync
|
||
|
|
*/
|
||
|
|
async triggerSync(dataType: 'weather' | 'traffic' | 'all'): Promise<void> {
|
||
|
|
await apiClient.post('/data/sync/trigger', { data_type: dataType });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const dataService = new DataService();
|