REFACTOR data service

This commit is contained in:
Urtzi Alfaro
2025-08-12 18:17:30 +02:00
parent 7c237c0acc
commit fbe7470ad9
149 changed files with 8528 additions and 7393 deletions

View File

@@ -193,7 +193,8 @@ const App: React.FC = () => {
isAuthenticated: false,
isLoading: false,
user: null,
currentPage: 'landing' // 👈 Return to landing page after logout
currentPage: 'landing', // 👈 Return to landing page after logout
routingDecision: null
});
};

View File

@@ -333,7 +333,12 @@ private buildURL(endpoint: string): string {
size: JSON.stringify(result).length,
});
return result;
// Handle both wrapped and unwrapped responses
// If result has a 'data' property, return it; otherwise return the result itself
if (result && typeof result === 'object' && 'data' in result) {
return result.data as T;
}
return result as T;
} catch (error) {
// Record error metrics
this.recordMetrics({

View File

@@ -7,12 +7,15 @@ export interface RequestConfig {
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
headers?: Record<string, string>;
params?: Record<string, any>;
body?: any;
url?: string;
timeout?: number;
retries?: number;
cache?: boolean;
cacheTTL?: number;
optimistic?: boolean;
background?: boolean;
metadata?: any;
}
export interface ApiResponse<T = any> {
@@ -20,12 +23,14 @@ export interface ApiResponse<T = any> {
message?: string;
status: string;
timestamp?: string;
metadata?: any;
meta?: {
page?: number;
limit?: number;
total?: number;
hasNext?: boolean;
hasPrev?: boolean;
requestId?: string;
};
}

View File

@@ -5,7 +5,8 @@
export { useAuth, useAuthHeaders } from './useAuth';
export { useTenant } from './useTenant';
export { useData } from './useData';
export { useSales } from './useSales';
export { useExternal } from './useExternal';
export { useTraining } from './useTraining';
export { useForecast } from './useForecast';
export { useNotification } from './useNotification';
@@ -14,7 +15,8 @@ export { useOnboarding, useOnboardingStep } from './useOnboarding';
// Import hooks for combined usage
import { useAuth } from './useAuth';
import { useTenant } from './useTenant';
import { useData } from './useData';
import { useSales } from './useSales';
import { useExternal } from './useExternal';
import { useTraining } from './useTraining';
import { useForecast } from './useForecast';
import { useNotification } from './useNotification';
@@ -24,7 +26,8 @@ import { useOnboarding } from './useOnboarding';
export const useApiHooks = () => {
const auth = useAuth();
const tenant = useTenant();
const data = useData();
const sales = useSales();
const external = useExternal();
const training = useTraining();
const forecast = useForecast();
const notification = useNotification();
@@ -33,7 +36,8 @@ export const useApiHooks = () => {
return {
auth,
tenant,
data,
sales,
external,
training,
forecast,
notification,

View File

@@ -99,7 +99,11 @@ export const useAuth = () => {
const response = await authService.register(data);
// Auto-login after successful registration
if (response.user) {
if (response && response.user) {
await login({ email: data.email, password: data.password });
} else {
// If response doesn't have user property, registration might still be successful
// Try to login anyway in case the user was created but response format is different
await login({ email: data.email, password: data.password });
}
} catch (error) {

View File

@@ -0,0 +1,209 @@
// frontend/src/api/hooks/useExternal.ts
/**
* External Data Management Hooks
* Handles weather and traffic data operations
*/
import { useState, useCallback } from 'react';
import { externalService } from '../services/external.service';
import type { WeatherData, TrafficData, WeatherForecast } from '../services/external.service';
export const useExternal = () => {
const [weatherData, setWeatherData] = useState<WeatherData | null>(null);
const [trafficData, setTrafficData] = useState<TrafficData | null>(null);
const [weatherForecast, setWeatherForecast] = useState<WeatherForecast[]>([]);
const [trafficForecast, setTrafficForecast] = useState<TrafficData[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
/**
* Get Current Weather
*/
const getCurrentWeather = useCallback(async (
tenantId: string,
lat: number,
lon: number
): Promise<WeatherData> => {
try {
setIsLoading(true);
setError(null);
const weather = await externalService.getCurrentWeather(tenantId, lat, lon);
setWeatherData(weather);
return weather;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get weather data';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Get Weather Forecast
*/
const getWeatherForecast = useCallback(async (
tenantId: string,
lat: number,
lon: number,
days: number = 7
): Promise<WeatherForecast[]> => {
try {
setIsLoading(true);
setError(null);
const forecast = await externalService.getWeatherForecast(tenantId, lat, lon, days);
setWeatherForecast(forecast);
return forecast;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get weather forecast';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Get Historical Weather Data
*/
const getHistoricalWeather = useCallback(async (
tenantId: string,
lat: number,
lon: number,
startDate: string,
endDate: string
): Promise<WeatherData[]> => {
try {
setIsLoading(true);
setError(null);
const data = await externalService.getHistoricalWeather(tenantId, lat, lon, startDate, endDate);
return data;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get historical weather';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Get Current Traffic
*/
const getCurrentTraffic = useCallback(async (
tenantId: string,
lat: number,
lon: number
): Promise<TrafficData> => {
try {
setIsLoading(true);
setError(null);
const traffic = await externalService.getCurrentTraffic(tenantId, lat, lon);
setTrafficData(traffic);
return traffic;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get traffic data';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Get Traffic Forecast
*/
const getTrafficForecast = useCallback(async (
tenantId: string,
lat: number,
lon: number,
hours: number = 24
): Promise<TrafficData[]> => {
try {
setIsLoading(true);
setError(null);
const forecast = await externalService.getTrafficForecast(tenantId, lat, lon, hours);
setTrafficForecast(forecast);
return forecast;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get traffic forecast';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Get Historical Traffic Data
*/
const getHistoricalTraffic = useCallback(async (
tenantId: string,
lat: number,
lon: number,
startDate: string,
endDate: string
): Promise<TrafficData[]> => {
try {
setIsLoading(true);
setError(null);
const data = await externalService.getHistoricalTraffic(tenantId, lat, lon, startDate, endDate);
return data;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get historical traffic';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Test External Services Connectivity
*/
const testConnectivity = useCallback(async (tenantId: string) => {
try {
setIsLoading(true);
setError(null);
const results = await externalService.testConnectivity(tenantId);
return results;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to test connectivity';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
return {
weatherData,
trafficData,
weatherForecast,
trafficForecast,
isLoading,
error,
getCurrentWeather,
getWeatherForecast,
getHistoricalWeather,
getCurrentTraffic,
getTrafficForecast,
getHistoricalTraffic,
testConnectivity,
clearError: () => setError(null),
};
};

View File

@@ -1,20 +1,21 @@
// frontend/src/api/hooks/useData.ts
// frontend/src/api/hooks/useSales.ts
/**
* Data Management Hooks
* Sales Data Management Hooks
*/
import { useState, useCallback } from 'react';
import { dataService } from '../services';
import { salesService } from '../services/sales.service';
import type {
SalesData,
SalesValidationResult,
SalesDataQuery,
SalesDataImport,
SalesImportResult,
DashboardStats,
ActivityItem,
} from '../types';
export const useData = () => {
export const useSales = () => {
const [salesData, setSalesData] = useState<SalesData[]>([]);
const [dashboardStats, setDashboardStats] = useState<DashboardStats | null>(null);
const [recentActivity, setRecentActivity] = useState<ActivityItem[]>([]);
@@ -32,7 +33,7 @@ export const useData = () => {
setError(null);
setUploadProgress(0);
const result = await dataService.uploadSalesHistory(tenantId, file, {
const result = await salesService.uploadSalesHistory(tenantId, file, {
...additionalData,
onProgress: (progress) => {
setUploadProgress(progress.percentage);
@@ -58,7 +59,7 @@ export const useData = () => {
setIsLoading(true);
setError(null);
const result = await dataService.validateSalesData(tenantId, file);
const result = await salesService.validateSalesData(tenantId, file);
return result;
} catch (error) {
const message = error instanceof Error ? error.message : 'Validation failed';
@@ -77,7 +78,7 @@ export const useData = () => {
setIsLoading(true);
setError(null);
const response = await dataService.getSalesData(tenantId, query);
const response = await salesService.getSalesData(tenantId, query);
setSalesData(response.data);
return response.data;
@@ -95,7 +96,7 @@ export const useData = () => {
setIsLoading(true);
setError(null);
const stats = await dataService.getDashboardStats(tenantId);
const stats = await salesService.getDashboardStats(tenantId);
setDashboardStats(stats);
return stats;
@@ -113,7 +114,7 @@ export const useData = () => {
setIsLoading(true);
setError(null);
const activity = await dataService.getRecentActivity(tenantId, limit);
const activity = await salesService.getRecentActivity(tenantId, limit);
setRecentActivity(activity);
return activity;
@@ -135,7 +136,7 @@ export const useData = () => {
setIsLoading(true);
setError(null);
const blob = await dataService.exportSalesData(tenantId, format, query);
const blob = await salesService.exportSalesData(tenantId, format, query);
// Create download link
const url = window.URL.createObjectURL(blob);
@@ -157,14 +158,13 @@ export const useData = () => {
/**
* Get Products List
* Add this method to the useData hook
*/
const getProductsList = useCallback(async (tenantId: string): Promise<string[]> => {
try {
setIsLoading(true);
setError(null);
const products = await dataService.getProductsList(tenantId);
const products = await salesService.getProductsList(tenantId);
return products;
} catch (error) {
@@ -176,30 +176,8 @@ export const useData = () => {
}
}, []);
/**
* Get Current Weather
* Add this method to the useData hook
*/
const getCurrentWeather = useCallback(async (tenantId: string, lat: number, lon: number) => {
try {
setIsLoading(true);
setError(null);
const weather = await dataService.getCurrentWeather(tenantId, lat, lon);
return weather;
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to get weather data';
setError(message);
throw error;
} finally {
setIsLoading(false);
}
}, []);
/**
* Get Sales Analytics
* Add this method to the useData hook
*/
const getSalesAnalytics = useCallback(async (
tenantId: string,
@@ -210,7 +188,7 @@ export const useData = () => {
setIsLoading(true);
setError(null);
const analytics = await dataService.getSalesAnalytics(tenantId, startDate, endDate);
const analytics = await salesService.getSalesAnalytics(tenantId, startDate, endDate);
return analytics;
} catch (error) {
@@ -236,8 +214,7 @@ export const useData = () => {
getRecentActivity,
exportSalesData,
getProductsList,
getCurrentWeather,
getSalesAnalytics,
clearError: () => setError(null),
};
};
};

View File

@@ -11,7 +11,8 @@ export { apiClient } from './client';
export {
authService,
tenantService,
dataService,
salesService,
externalService,
trainingService,
forecastingService,
notificationService,
@@ -23,7 +24,8 @@ export {
useAuth,
useAuthHeaders,
useTenant,
useData,
useSales,
useExternal,
useTraining,
useForecast,
useNotification,

View File

@@ -24,7 +24,7 @@ export class AuthService {
/**
* User Registration
*/
async register(data: RegisterRequest): Promise<{ user: UserResponse }> {
async register(data: RegisterRequest): Promise<LoginResponse> {
return apiClient.post(`${this.baseEndpoint}/register`, data);
}

View File

@@ -0,0 +1,264 @@
// frontend/src/api/services/external.service.ts
/**
* External Data Service
* Handles weather and traffic data operations for the external microservice
*/
import { apiClient } from '../client';
import { RequestTimeouts } from '../client/config';
// Align with backend WeatherDataResponse schema
export interface WeatherData {
date: string;
temperature?: number;
precipitation?: number;
humidity?: number;
wind_speed?: number;
pressure?: number;
description?: string;
source: string;
}
// Align with backend TrafficDataResponse schema
export interface TrafficData {
date: string;
traffic_volume?: number;
pedestrian_count?: number;
congestion_level?: string;
average_speed?: number;
source: string;
}
export interface WeatherForecast {
date: string;
temperature_min: number;
temperature_max: number;
temperature_avg: number;
precipitation: number;
description: string;
humidity?: number;
wind_speed?: number;
}
export class ExternalService {
/**
* Get Current Weather Data
*/
async getCurrentWeather(
tenantId: string,
lat: number,
lon: number
): Promise<WeatherData> {
try {
// ✅ FIX 1: Correct endpoint path with tenant ID
const endpoint = `/tenants/${tenantId}/weather/current`;
// ✅ FIX 2: Correct parameter names (latitude/longitude, not lat/lon)
const response = await apiClient.get(endpoint, {
params: {
latitude: lat, // Backend expects 'latitude'
longitude: lon // Backend expects 'longitude'
}
});
console.log('Weather API response:', response);
// Return backend response directly (matches WeatherData interface)
return response;
} catch (error) {
console.error('Failed to fetch weather from backend:', error);
// Fallback weather for Madrid (matching WeatherData schema)
return {
date: new Date().toISOString(),
temperature: 18,
description: 'Parcialmente nublado',
precipitation: 0,
humidity: 65,
wind_speed: 10,
source: 'fallback'
};
}
}
/**
* Get Weather Forecast
*/
async getWeatherForecast(
tenantId: string,
lat: number,
lon: number,
days: number = 7
): Promise<WeatherForecast[]> {
try {
// Fix: Use POST with JSON body as expected by backend
const response = await apiClient.post(`/tenants/${tenantId}/weather/forecast`, {
latitude: lat,
longitude: lon,
days: days
});
// Handle response format
if (Array.isArray(response)) {
return response;
} else if (response && response.forecasts) {
return response.forecasts;
} else {
console.warn('Unexpected weather forecast response format:', response);
return [];
}
} catch (error) {
console.error('Failed to fetch weather forecast:', error);
return [];
}
}
/**
* Get Historical Weather Data
*/
async getHistoricalWeather(
tenantId: string,
lat: number,
lon: number,
startDate: string,
endDate: string
): Promise<WeatherData[]> {
try {
// Fix: Use POST with JSON body as expected by backend
const response = await apiClient.post(`/tenants/${tenantId}/weather/historical`, {
latitude: lat,
longitude: lon,
start_date: startDate,
end_date: endDate
});
// Return backend response directly (matches WeatherData interface)
return Array.isArray(response) ? response : response.data || [];
} catch (error) {
console.error('Failed to fetch historical weather:', error);
return [];
}
}
/**
* Get Current Traffic Data
*/
async getCurrentTraffic(
tenantId: string,
lat: number,
lon: number
): Promise<TrafficData> {
try {
const response = await apiClient.get(`/tenants/${tenantId}/traffic/current`, {
params: {
latitude: lat,
longitude: lon
}
});
// Return backend response directly (matches TrafficData interface)
return response;
} catch (error) {
console.error('Failed to fetch traffic data:', error);
// Fallback traffic data (matching TrafficData schema)
return {
date: new Date().toISOString(),
traffic_volume: 50,
pedestrian_count: 25,
congestion_level: 'medium',
average_speed: 30,
source: 'fallback'
};
}
}
/**
* Get Traffic Forecast
*/
async getTrafficForecast(
tenantId: string,
lat: number,
lon: number,
hours: number = 24
): Promise<TrafficData[]> {
try {
// Fix: Use POST with JSON body as expected by backend
const response = await apiClient.post(`/tenants/${tenantId}/traffic/forecast`, {
latitude: lat,
longitude: lon,
hours: hours
});
// Return backend response directly (matches TrafficData interface)
return Array.isArray(response) ? response : response.data || [];
} catch (error) {
console.error('Failed to fetch traffic forecast:', error);
return [];
}
}
/**
* Get Historical Traffic Data
*/
async getHistoricalTraffic(
tenantId: string,
lat: number,
lon: number,
startDate: string,
endDate: string
): Promise<TrafficData[]> {
try {
// Fix: Use POST with JSON body as expected by backend
const response = await apiClient.post(`/tenants/${tenantId}/traffic/historical`, {
latitude: lat,
longitude: lon,
start_date: startDate,
end_date: endDate
});
// Return backend response directly (matches TrafficData interface)
return Array.isArray(response) ? response : response.data || [];
} catch (error) {
console.error('Failed to fetch historical traffic:', error);
return [];
}
}
/**
* Test External Service Connectivity
*/
async testConnectivity(tenantId: string): Promise<{
weather: boolean;
traffic: boolean;
overall: boolean;
}> {
const results = {
weather: false,
traffic: false,
overall: false
};
try {
// Test weather service
await this.getCurrentWeather(tenantId, 40.4168, -3.7038); // Madrid coordinates
results.weather = true;
} catch (error) {
console.warn('Weather service connectivity test failed:', error);
}
try {
// Test traffic service
await this.getCurrentTraffic(tenantId, 40.4168, -3.7038); // Madrid coordinates
results.traffic = true;
} catch (error) {
console.warn('Traffic service connectivity test failed:', error);
}
results.overall = results.weather && results.traffic;
return results;
}
}
export const externalService = new ExternalService();

View File

@@ -7,7 +7,8 @@
// Import and export individual services
import { AuthService } from './auth.service';
import { TenantService } from './tenant.service';
import { DataService } from './data.service';
import { SalesService } from './sales.service';
import { ExternalService } from './external.service';
import { TrainingService } from './training.service';
import { ForecastingService } from './forecasting.service';
import { NotificationService } from './notification.service';
@@ -16,17 +17,28 @@ import { OnboardingService } from './onboarding.service';
// Create service instances
export const authService = new AuthService();
export const tenantService = new TenantService();
export const dataService = new DataService();
export const salesService = new SalesService();
export const externalService = new ExternalService();
export const trainingService = new TrainingService();
export const forecastingService = new ForecastingService();
export const notificationService = new NotificationService();
export const onboardingService = new OnboardingService();
// Export the classes as well
export { AuthService, TenantService, DataService, TrainingService, ForecastingService, NotificationService, OnboardingService };
export {
AuthService,
TenantService,
SalesService,
ExternalService,
TrainingService,
ForecastingService,
NotificationService,
OnboardingService
};
// Import base client
export { apiClient } from '../client';
import { apiClient } from '../client';
export { apiClient };
// Re-export all types
export * from '../types';
@@ -35,7 +47,8 @@ export * from '../types';
export const api = {
auth: authService,
tenant: tenantService,
data: dataService,
sales: salesService,
external: externalService,
training: trainingService,
forecasting: forecastingService,
notification: notificationService,
@@ -56,7 +69,8 @@ export class HealthService {
const services = [
{ name: 'Auth', endpoint: '/auth/health' },
{ name: 'Tenant', endpoint: '/tenants/health' },
{ name: 'Data', endpoint: '/data/health' },
{ name: 'Sales', endpoint: '/sales/health' },
{ name: 'External', endpoint: '/external/health' },
{ name: 'Training', endpoint: '/training/health' },
{ name: 'Forecasting', endpoint: '/forecasting/health' },
{ name: 'Notification', endpoint: '/notifications/health' },

View File

@@ -1,7 +1,7 @@
// frontend/src/api/services/data.service.ts
// frontend/src/api/services/sales.service.ts
/**
* Data Management Service
* Handles sales data operations
* Sales Data Service
* Handles sales data operations for the sales microservice
*/
import { apiClient } from '../client';
@@ -17,7 +17,7 @@ import type {
ActivityItem,
} from '../types';
export class DataService {
export class SalesService {
/**
* Upload Sales History File
*/
@@ -143,7 +143,7 @@ export class DataService {
metrics?: string[];
}
): Promise<any> {
return apiClient.get(`/tenants/${tenantId}/analytics`, { params });
return apiClient.get(`/tenants/${tenantId}/sales/analytics`, { params });
}
/**
@@ -175,14 +175,13 @@ export class DataService {
* Get Recent Activity
*/
async getRecentActivity(tenantId: string, limit?: number): Promise<ActivityItem[]> {
return apiClient.get(`/tenants/${tenantId}/activity`, {
return apiClient.get(`/tenants/${tenantId}/sales/activity`, {
params: { limit },
});
}
/**
* Get Products List from Sales Data
* This should be added to the DataService class
*/
async getProductsList(tenantId: string): Promise<string[]> {
try {
@@ -261,90 +260,8 @@ export class DataService {
}
}
/**
* Get Current Weather Data
* This should be added to the DataService class
*/
async getCurrentWeather(
tenantId: string,
lat: number,
lon: number
): Promise<{
temperature: number;
description: string;
precipitation: number;
humidity?: number;
wind_speed?: number;
}> {
try {
// ✅ FIX 1: Correct endpoint path with tenant ID
const endpoint = `/tenants/${tenantId}/weather/current`;
// ✅ FIX 2: Correct parameter names (latitude/longitude, not lat/lon)
const response = await apiClient.get(endpoint, {
params: {
latitude: lat, // Backend expects 'latitude'
longitude: lon // Backend expects 'longitude'
}
});
// ✅ FIX 3: Handle the actual backend response structure
// Backend returns WeatherDataResponse:
// {
// "date": "2025-08-04T12:00:00Z",
// "temperature": 25.5,
// "precipitation": 0.0,
// "humidity": 65.0,
// "wind_speed": 10.2,
// "pressure": 1013.2,
// "description": "Partly cloudy",
// "source": "aemet"
// }
console.log('Weather API response:', response);
// Map backend response to expected frontend format
return {
temperature: response.temperature || 18,
description: response.description || 'Parcialmente nublado',
precipitation: response.precipitation || 0,
humidity: response.humidity || 65,
wind_speed: response.wind_speed || 10
};
} catch (error) {
console.error('Failed to fetch weather from backend:', error);
// Fallback weather for Madrid
return {
temperature: 18,
description: 'Parcialmente nublado',
precipitation: 0,
humidity: 65,
wind_speed: 10
};
}
}
/**
* Get Weather Forecast
* This should be added to the DataService class
*/
async getWeatherForecast(
lat: number,
lon: number,
days: number = 7
): Promise<any[]> {
return apiClient.get(`/data/weather/forecast`, {
params: { lat, lon, days }
});
}
/**
* Get Sales Summary by Period
* This should be added to the DataService class
*/
async getSalesSummary(
tenantId: string,
@@ -357,7 +274,6 @@ export class DataService {
/**
* Get Sales Analytics
* This should be added to the DataService class
*/
async getSalesAnalytics(
tenantId: string,
@@ -369,14 +285,13 @@ export class DataService {
forecast_accuracy?: number;
stockout_events?: number;
}> {
return apiClient.get(`/tenants/${tenantId}/sales/analytics`, {
return apiClient.get(`/tenants/${tenantId}/sales/analytics/summary`, {
params: {
start_date: startDate,
end_date: endDate
}
});
}
}
export const dataService = new DataService();
export const salesService = new SalesService();

View File

@@ -3,6 +3,8 @@
* Data Management Types
*/
import { BaseQueryParams } from './common';
export interface SalesData {
id: string;
tenant_id: string;
@@ -26,6 +28,8 @@ export interface SalesValidationResult {
errors: ValidationError[];
warnings: ValidationError[];
summary: Record<string, any>;
message?: string;
details?: Record<string, any>;
}
export interface ExternalFactors {
@@ -63,6 +67,7 @@ export interface SalesImportResult {
success: boolean;
message: string;
imported_count: number;
records_imported: number;
skipped_count: number;
error_count: number;
validation_errors?: ValidationError[];

View File

@@ -3,6 +3,8 @@
* Forecasting Service Types
*/
import { ExternalFactors } from './data';
export interface SingleForecastRequest {
product_name: string;
forecast_date: string;

View File

@@ -61,11 +61,16 @@ export interface TenantSubscription {
export interface TenantCreate {
name: string;
address?: string;
business_type?: 'individual' | 'central_workshop';
postal_code: string;
phone: string;
description?: string;
settings?: Partial<TenantSettings>;
location?: TenantLocation;
coordinates?: { lat: number; lng: number };
products?: string[];
has_historical_data?: boolean;
}
export interface TenantUpdate {

View File

@@ -7,6 +7,10 @@ export interface TrainingJobRequest {
config?: TrainingJobConfig;
priority?: number;
schedule_time?: string;
include_weather?: boolean;
include_traffic?: boolean;
min_data_points?: number;
use_default_data?: boolean;
}
export interface SingleProductTrainingRequest {

View File

@@ -3,7 +3,7 @@
* Error Handling Utilities
*/
import type { ApiError } from '../types';
import type { ApiError } from '../client/types';
export class ApiErrorHandler {
static formatError(error: any): string {

View File

@@ -2,7 +2,7 @@
// Complete dashboard hook using your API infrastructure
import { useState, useEffect, useCallback } from 'react';
import { useAuth, useData, useForecast } from '../api';
import { useAuth, useSales, useExternal, useForecast } from '../api';
import { useTenantId } from './useTenantId';
@@ -31,12 +31,16 @@ export const useDashboard = () => {
const { user } = useAuth();
const {
getProductsList,
getCurrentWeather,
getSalesAnalytics,
getDashboardStats,
isLoading: dataLoading,
error: dataError
} = useData();
isLoading: salesLoading,
error: salesError
} = useSales();
const {
getCurrentWeather,
isLoading: externalLoading,
error: externalError
} = useExternal();
const {
createSingleForecast,
@@ -236,8 +240,8 @@ export const useDashboard = () => {
return {
...dashboardData,
isLoading: isLoading || dataLoading || forecastLoading,
error: error || dataError || forecastError,
isLoading: isLoading || salesLoading || externalLoading || forecastLoading,
error: error || salesError || externalError || forecastError,
reload: () => tenantId ? loadDashboardData(tenantId) : Promise.resolve(),
clearError: () => setError(null)
};

View File

@@ -1,6 +1,6 @@
// Real API hook for Order Suggestions using backend data
import { useState, useCallback, useEffect } from 'react';
import { useData, useForecast } from '../api';
import { useSales, useExternal, useForecast } from '../api';
import { useTenantId } from './useTenantId';
import type { DailyOrderItem, WeeklyOrderItem } from '../components/simple/OrderSuggestions';
@@ -44,9 +44,11 @@ export const useOrderSuggestions = () => {
const {
getProductsList,
getSalesAnalytics,
getDashboardStats,
getDashboardStats
} = useSales();
const {
getCurrentWeather
} = useData();
} = useExternal();
const {
createSingleForecast,
getQuickForecasts,
@@ -158,8 +160,8 @@ export const useOrderSuggestions = () => {
console.log('📊 OrderSuggestions: Generating weekly suggestions for tenant:', tenantId);
// Get sales analytics for the past month
const endDate = new Date().toISOString().split('T')[0];
const startDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
const endDate = new Date().toISOString();
const startDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString();
let analytics: any = null;
try {

View File

@@ -58,6 +58,16 @@ interface RegisterForm {
paymentCompleted: boolean;
}
interface RegisterFormErrors {
fullName?: string;
email?: string;
confirmEmail?: string;
password?: string;
confirmPassword?: string;
acceptTerms?: string;
paymentCompleted?: string;
}
const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin }) => {
const { register, isLoading } = useAuth();
@@ -73,7 +83,7 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [errors, setErrors] = useState<Partial<RegisterForm>>({});
const [errors, setErrors] = useState<RegisterFormErrors>({});
const [passwordStrength, setPasswordStrength] = useState<{
score: number;
checks: { [key: string]: boolean };
@@ -246,7 +256,7 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
e.preventDefault();
// Validate form but exclude payment requirement for first step
const newErrors: Partial<RegisterForm> = {};
const newErrors: RegisterFormErrors = {};
if (!formData.fullName.trim()) {
newErrors.fullName = 'El nombre completo es obligatorio';
@@ -346,7 +356,7 @@ const RegisterPage: React.FC<RegisterPageProps> = ({ onLogin, onNavigateToLogin
}));
// Clear error when user starts typing
if (errors[name as keyof RegisterForm]) {
if (errors[name as keyof RegisterFormErrors]) {
setErrors(prev => ({
...prev,
[name]: undefined

View File

@@ -219,9 +219,6 @@ const DashboardPage: React.FC<DashboardPageProps> = ({
onUpdateStatus={(itemId: string, status: any) => {
console.log('Update status:', itemId, status);
}}
onViewDetails={() => {
onNavigateToProduction?.();
}}
/>
{/* Quick Overview - Supporting Information */}

View File

@@ -7,7 +7,7 @@ import SimplifiedTrainingProgress from '../../components/SimplifiedTrainingProgr
import {
useTenant,
useTraining,
useData,
useSales,
useTrainingWebSocket,
useOnboarding,
TenantCreate,
@@ -75,7 +75,7 @@ const OnboardingPage: React.FC<OnboardingPageProps> = ({ user, onComplete }) =>
const [trainingJobId, setTrainingJobId] = useState<string>('');
const { createTenant, getUserTenants, isLoading: tenantLoading } = useTenant();
const { startTrainingJob } = useTraining({ disablePolling: true });
const { uploadSalesHistory, validateSalesData } = useData();
const { uploadSalesHistory, validateSalesData } = useSales();
const steps = [
{ id: 1, title: 'Datos de Panadería', icon: Store },
@@ -315,7 +315,7 @@ const OnboardingPage: React.FC<OnboardingPageProps> = ({ user, onComplete }) =>
const tenantData: TenantCreate = {
name: bakeryData.name,
address: bakeryData.address,
business_type: "bakery",
business_type: "individual",
postal_code: "28010",
phone: "+34655334455",
coordinates: bakeryData.coordinates,

View File

@@ -355,9 +355,9 @@ const OrdersPage: React.FC = () => {
<Package className="mx-auto h-12 w-12 text-gray-400 mb-4" />
<h3 className="text-lg font-medium text-gray-900 mb-2">No hay pedidos</h3>
<p className="text-gray-600 mb-4">
{activeTab === 'all'
{activeTab === 'orders'
? 'Aún no has creado ningún pedido'
: `No hay pedidos ${activeTab === 'pending' ? 'pendientes' : 'entregados'}`
: 'No hay datos disponibles para esta sección'
}
</p>
<button