Fix new Frontend 17
This commit is contained in:
@@ -185,30 +185,149 @@ export class DataService {
|
||||
* This should be added to the DataService class
|
||||
*/
|
||||
async getProductsList(tenantId: string): Promise<string[]> {
|
||||
const response = await apiClient.get(`/tenants/${tenantId}/sales/products`);
|
||||
|
||||
// Extract product names from the response
|
||||
return response.map((product: any) =>
|
||||
product.name || product.product_name || product
|
||||
).filter(Boolean);
|
||||
try {
|
||||
const response = await apiClient.get(`/tenants/${tenantId}/sales/products`);
|
||||
|
||||
console.log('🔍 Products API Response Analysis:');
|
||||
console.log('- Type:', typeof response);
|
||||
console.log('- Is Array:', Array.isArray(response));
|
||||
console.log('- Keys:', Object.keys(response || {}));
|
||||
console.log('- Response:', response);
|
||||
|
||||
let productsArray: any[] = [];
|
||||
|
||||
// ✅ FIX: Handle different response formats
|
||||
if (Array.isArray(response)) {
|
||||
// Standard array response
|
||||
productsArray = response;
|
||||
console.log('✅ Response is already an array');
|
||||
} else if (response && typeof response === 'object') {
|
||||
// Object with numeric keys - convert to array
|
||||
const keys = Object.keys(response);
|
||||
if (keys.length > 0 && keys.every(key => !isNaN(Number(key)))) {
|
||||
// Object has numeric keys like {0: {...}, 1: {...}}
|
||||
productsArray = Object.values(response);
|
||||
console.log('✅ Converted object with numeric keys to array');
|
||||
} else {
|
||||
console.warn('⚠️ Response is object but not with numeric keys:', response);
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
console.warn('⚠️ Response is not array or object:', response);
|
||||
return [];
|
||||
}
|
||||
|
||||
console.log('📦 Products array:', productsArray);
|
||||
|
||||
// Extract product names from the array
|
||||
const productNames = productsArray
|
||||
.map((product: any) => {
|
||||
if (typeof product === 'string') {
|
||||
return product;
|
||||
}
|
||||
if (product && typeof product === 'object') {
|
||||
return product.product_name ||
|
||||
product.name ||
|
||||
product.productName ||
|
||||
null;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.filter(Boolean) // Remove null/undefined values
|
||||
.filter((name: string) => name.trim().length > 0); // Remove empty strings
|
||||
|
||||
console.log('📋 Extracted product names:', productNames);
|
||||
|
||||
if (productNames.length === 0) {
|
||||
console.warn('⚠️ No valid product names extracted from response');
|
||||
}
|
||||
|
||||
return productNames;
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to fetch products list:', error);
|
||||
|
||||
// Return fallback products for Madrid bakery
|
||||
return [
|
||||
'Croissants',
|
||||
'Pan de molde',
|
||||
'Baguettes',
|
||||
'Café',
|
||||
'Napolitanas',
|
||||
'Pan integral',
|
||||
'Magdalenas',
|
||||
'Churros'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Current Weather Data
|
||||
* This should be added to the DataService class
|
||||
*/
|
||||
async getCurrentWeather(lat: number, lon: number): Promise<{
|
||||
async getCurrentWeather(
|
||||
tenantId: string,
|
||||
lat: number,
|
||||
lon: number
|
||||
): Promise<{
|
||||
temperature: number;
|
||||
description: string;
|
||||
precipitation: number;
|
||||
humidity?: number;
|
||||
wind_speed?: number;
|
||||
}> {
|
||||
return apiClient.get(`/data/weather/current`, {
|
||||
params: { lat, lon }
|
||||
});
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user