Start integrating the onboarding flow with backend 10
This commit is contained in:
@@ -65,7 +65,14 @@ export const useInventorySetup = () => {
|
||||
createdItems?: any[];
|
||||
inventoryMapping?: { [productName: string]: string };
|
||||
}> => {
|
||||
console.log('useInventorySetup - createInventoryFromSuggestions called with:', {
|
||||
suggestionsCount: suggestions?.length,
|
||||
suggestions: suggestions?.slice(0, 3), // Log first 3 for debugging
|
||||
tenantId: currentTenant?.id
|
||||
});
|
||||
|
||||
if (!suggestions || suggestions.length === 0) {
|
||||
console.error('useInventorySetup - No suggestions provided');
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
error: 'No hay sugerencias para crear el inventario',
|
||||
@@ -74,6 +81,7 @@ export const useInventorySetup = () => {
|
||||
}
|
||||
|
||||
if (!currentTenant?.id) {
|
||||
console.error('useInventorySetup - No tenant ID available');
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
error: 'No se pudo obtener información del tenant',
|
||||
@@ -91,31 +99,54 @@ export const useInventorySetup = () => {
|
||||
const createdItems = [];
|
||||
const inventoryMapping: { [key: string]: string } = {};
|
||||
|
||||
console.log('useInventorySetup - Creating ingredients from suggestions...');
|
||||
|
||||
// Create ingredients from approved suggestions
|
||||
for (const suggestion of suggestions) {
|
||||
try {
|
||||
const ingredientData = {
|
||||
name: suggestion.name,
|
||||
name: suggestion.suggested_name || suggestion.original_name,
|
||||
category: suggestion.category || 'Sin categoría',
|
||||
description: suggestion.description || '',
|
||||
description: suggestion.notes || '',
|
||||
unit_of_measure: suggestion.unit_of_measure || 'unidad',
|
||||
cost_per_unit: suggestion.cost_per_unit || 0,
|
||||
supplier_info: suggestion.supplier_info || {},
|
||||
nutritional_info: suggestion.nutritional_info || {},
|
||||
storage_requirements: suggestion.storage_requirements || {},
|
||||
allergen_info: suggestion.allergen_info || {},
|
||||
is_active: true,
|
||||
minimum_stock_level: 1, // Default minimum stock
|
||||
maximum_stock_level: 100, // Default maximum stock
|
||||
reorder_point: 5, // Default reorder point
|
||||
shelf_life_days: suggestion.estimated_shelf_life_days || 30,
|
||||
requires_refrigeration: suggestion.requires_refrigeration || false,
|
||||
requires_freezing: suggestion.requires_freezing || false,
|
||||
is_seasonal: suggestion.is_seasonal || false,
|
||||
cost_per_unit: 0, // Will be set by user later
|
||||
notes: suggestion.notes || `Producto clasificado automáticamente desde: ${suggestion.original_name}`,
|
||||
};
|
||||
|
||||
console.log('useInventorySetup - Creating ingredient:', {
|
||||
name: ingredientData.name,
|
||||
category: ingredientData.category,
|
||||
original_name: suggestion.original_name,
|
||||
ingredientData: ingredientData,
|
||||
tenantId: currentTenant.id,
|
||||
apiUrl: `/tenants/${currentTenant.id}/ingredients`
|
||||
});
|
||||
|
||||
const createdItem = await createIngredientMutation.mutateAsync({
|
||||
tenantId: currentTenant.id,
|
||||
ingredientData,
|
||||
});
|
||||
|
||||
console.log('useInventorySetup - Created ingredient successfully:', {
|
||||
id: createdItem.id,
|
||||
name: createdItem.name
|
||||
});
|
||||
|
||||
createdItems.push(createdItem);
|
||||
inventoryMapping[suggestion.name] = createdItem.id;
|
||||
// Map both original and suggested names to the same ingredient ID for flexibility
|
||||
inventoryMapping[suggestion.original_name] = createdItem.id;
|
||||
if (suggestion.suggested_name && suggestion.suggested_name !== suggestion.original_name) {
|
||||
inventoryMapping[suggestion.suggested_name] = createdItem.id;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error creating ingredient ${suggestion.name}:`, error);
|
||||
console.error(`Error creating ingredient ${suggestion.suggested_name || suggestion.original_name}:`, error);
|
||||
// Continue with other ingredients even if one fails
|
||||
}
|
||||
}
|
||||
@@ -123,6 +154,12 @@ export const useInventorySetup = () => {
|
||||
if (createdItems.length === 0) {
|
||||
throw new Error('No se pudo crear ningún elemento del inventario');
|
||||
}
|
||||
|
||||
console.log('useInventorySetup - Successfully created ingredients:', {
|
||||
createdCount: createdItems.length,
|
||||
totalSuggestions: suggestions.length,
|
||||
inventoryMapping
|
||||
});
|
||||
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
@@ -139,6 +176,7 @@ export const useInventorySetup = () => {
|
||||
};
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Error creando el inventario';
|
||||
console.error('useInventorySetup - Error in createInventoryFromSuggestions:', error);
|
||||
setState(prev => ({
|
||||
...prev,
|
||||
isLoading: false,
|
||||
|
||||
Reference in New Issue
Block a user