236 lines
5.4 KiB
TypeScript
236 lines
5.4 KiB
TypeScript
// src/api/services/DataService.ts
|
|
import { apiClient } from '../base/apiClient';
|
|
import {
|
|
ApiResponse
|
|
} 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;
|
|
}
|
|
|
|
// Data types
|
|
export interface WeatherData {
|
|
date: string;
|
|
temperature: number;
|
|
humidity: number;
|
|
precipitation: number;
|
|
wind_speed: number;
|
|
}
|
|
|
|
export interface TrafficData {
|
|
date: string;
|
|
traffic_volume: number;
|
|
pedestrian_count: number;
|
|
}
|
|
|
|
export interface SalesRecord {
|
|
id: string;
|
|
tenant_id: string;
|
|
product_name: string;
|
|
quantity_sold: number;
|
|
revenue: number;
|
|
date: string;
|
|
created_at: string;
|
|
}
|
|
|
|
export interface CreateSalesRequest {
|
|
product_name: string;
|
|
quantity_sold: number;
|
|
revenue: number;
|
|
date: string;
|
|
}
|
|
|
|
export class DataService {
|
|
/**
|
|
* Upload sales history file
|
|
*/
|
|
async uploadSalesHistory(
|
|
file: File,
|
|
additionalData?: Record<string, any>
|
|
): Promise<UploadResponse> {
|
|
const response = await apiClient.upload<ApiResponse<UploadResponse>>(
|
|
'/api/v1/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>>(
|
|
'/api/v1/data/validate-sales',
|
|
file
|
|
);
|
|
return response.data!;
|
|
}
|
|
|
|
/**
|
|
* Get dashboard statistics
|
|
*/
|
|
async getDashboardStats(): Promise<DashboardStats> {
|
|
const response = await apiClient.get<ApiResponse<DashboardStats>>(
|
|
'/api/v1/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;
|
|
}>>('/api/v1/data/sales', { params });
|
|
return response.data!;
|
|
}
|
|
|
|
/**
|
|
* Create single sales record
|
|
*/
|
|
async createSalesRecord(record: CreateSalesRequest): Promise<SalesRecord> {
|
|
const response = await apiClient.post<ApiResponse<SalesRecord>>(
|
|
'/api/v1/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>>(
|
|
`/api/v1/data/sales/${id}`,
|
|
updates
|
|
);
|
|
return response.data!;
|
|
}
|
|
|
|
/**
|
|
* Delete sales record
|
|
*/
|
|
async deleteSalesRecord(id: string): Promise<void> {
|
|
await apiClient.delete(`/api/v1/data/sales/${id}`);
|
|
}
|
|
|
|
/**
|
|
* Get weather data
|
|
*/
|
|
async getWeatherData(params?: {
|
|
startDate?: string;
|
|
endDate?: string;
|
|
location?: string;
|
|
}): Promise<WeatherData[]> {
|
|
const response = await apiClient.get<ApiResponse<WeatherData[]>>(
|
|
'/api/v1/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[]>>(
|
|
'/api/v1/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>>('/api/v1/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('/api/v1/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[]>>('/api/v1/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>>('/api/v1/data/sync/status');
|
|
return response.data!;
|
|
}
|
|
|
|
/**
|
|
* Trigger manual data sync
|
|
*/
|
|
async triggerSync(dataType: 'weather' | 'traffic' | 'all'): Promise<void> {
|
|
await apiClient.post('/api/v1/data/sync/trigger', { data_type: dataType });
|
|
}
|
|
}
|
|
|
|
export const dataService = new DataService(); |