Start integrating the onboarding flow with backend 15

This commit is contained in:
Urtzi Alfaro
2025-09-07 22:58:58 +02:00
parent 0060b9cccb
commit 4c5bc0b636
3 changed files with 89 additions and 19 deletions

View File

@@ -166,12 +166,19 @@ export const useOnboardingActions = () => {
}, [store, tenantCreation]);
const processSalesFile = useCallback(async (file: File): Promise<boolean> => {
console.log('🎬 Actions - processSalesFile started');
store.setLoading(true);
const result = await salesProcessing.processFile(file);
console.log('🎬 Actions - processFile result:', result);
store.setLoading(false);
if (!result.success) {
console.error('❌ Actions - Processing failed:', salesProcessing.error);
store.setError(salesProcessing.error || 'Error processing sales file');
} else {
console.log('✅ Actions - Processing succeeded');
}
return result.success;

View File

@@ -74,13 +74,37 @@ export const useSalesProcessing = () => {
}
};
const response = await classifyProductsMutation.mutateAsync(requestPayload);
console.log('📡 Making API request to:', `/tenants/${currentTenant.id}/inventory/classify-products-batch`);
// Add timeout to the API call with shorter timeout
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => reject(new Error('API timeout: The inventory service may be overloaded. Please try again in a few moments.')), 15000); // 15 second timeout
});
const response = await Promise.race([
classifyProductsMutation.mutateAsync(requestPayload),
timeoutPromise
]);
console.log('✅ Generated', response?.length || 0, 'suggestions');
return response || [];
} catch (error) {
console.error('❌ Error generating suggestions:', error);
const isTimeout = error instanceof Error && error.message.includes('timeout');
const isNetworkError = error instanceof Error && (error.message.includes('fetch') || error.message.includes('network'));
console.error('❌ Error generating suggestions:', {
error: error,
message: error instanceof Error ? error.message : 'Unknown error',
isTimeout,
isNetworkError
});
// Re-throw timeout/network errors so they can be handled properly by the UI
if (isTimeout || isNetworkError) {
throw error;
}
return [];
}
}, [classifyProductsMutation, currentTenant]);
@@ -138,12 +162,47 @@ export const useSalesProcessing = () => {
updateProgress(60, 'analyzing', 'Identificando productos únicos...', onProgress);
updateProgress(70, 'analyzing', 'Generando sugerencias de IA...', onProgress);
const suggestions = await generateSuggestions(validationResult.product_list);
let suggestions: ProductSuggestionResponse[] = [];
let suggestionError: string | null = null;
try {
updateProgress(70, 'analyzing', 'Generando sugerencias de IA...', onProgress);
suggestions = await generateSuggestions(validationResult.product_list);
console.log('🔍 After generateSuggestions call:', {
suggestionsReceived: suggestions?.length || 0,
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Error generando sugerencias';
suggestionError = errorMessage;
console.error('❌ Suggestions generation failed:', errorMessage);
// Still continue with empty suggestions - user can manually add products later
updateProgress(80, 'analyzing', 'Preparando productos básicos...', onProgress);
// Create basic suggestions from product names as fallback
suggestions = validationResult.product_list.map((productName, index) => ({
suggestion_id: `manual-${index}`,
original_name: productName,
suggested_name: productName,
product_type: 'ingredient',
category: 'Sin categoría',
unit_of_measure: 'units',
confidence_score: 0.5,
estimated_shelf_life_days: 30,
requires_refrigeration: false,
requires_freezing: false,
is_seasonal: false,
notes: 'Clasificación manual - El servicio de IA no está disponible temporalmente'
}));
console.log('🔧 Created fallback suggestions:', suggestions.length);
}
updateProgress(90, 'analyzing', 'Analizando patrones de venta...', onProgress);
updateProgress(100, 'completed', 'Procesamiento completado exitosamente', onProgress);
// Update state with suggestions
// Update state with suggestions (even if empty or fallback)
setSuggestions(suggestions || []);
setValidationResults(validationResult);
@@ -155,8 +214,11 @@ export const useSalesProcessing = () => {
processingStage: 'completed',
processingResults: validationResult,
suggestions: suggestions || [],
suggestionError: suggestionError,
});
console.log('📊 Updated onboarding store with suggestions');
return {
success: true,
validationResults: validationResult,