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

@@ -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),
};
};
};