Fix new services implementation 11
This commit is contained in:
@@ -550,27 +550,46 @@ export class InventoryService {
|
||||
const response = await apiClient.get(`/tenants/${tenantId}/ingredients`, {
|
||||
params: {
|
||||
limit: 100,
|
||||
product_type: 'finished_product' // Only get finished products, not raw ingredients
|
||||
product_type: 'finished_product'
|
||||
},
|
||||
});
|
||||
|
||||
console.log('🔍 Inventory Products API Response:', response);
|
||||
console.log('🔍 Raw response data:', response.data);
|
||||
console.log('🔍 Response status:', response.status);
|
||||
console.log('🔍 Response headers:', response.headers);
|
||||
console.log('🔍 Full response object keys:', Object.keys(response || {}));
|
||||
console.log('🔍 Response data type:', typeof response);
|
||||
console.log('🔍 Response data constructor:', response?.constructor?.name);
|
||||
|
||||
// Check if response.data exists and what type it is
|
||||
if (response && 'data' in response) {
|
||||
console.log('🔍 Response.data exists:', typeof response.data);
|
||||
console.log('🔍 Response.data keys:', Object.keys(response.data || {}));
|
||||
console.log('🔍 Response.data constructor:', response.data?.constructor?.name);
|
||||
}
|
||||
|
||||
let productsArray: any[] = [];
|
||||
|
||||
if (Array.isArray(response)) {
|
||||
productsArray = response;
|
||||
} else if (response && typeof response === 'object') {
|
||||
// Check response.data first (typical API client behavior)
|
||||
const dataToProcess = response?.data || response;
|
||||
|
||||
if (Array.isArray(dataToProcess)) {
|
||||
productsArray = dataToProcess;
|
||||
console.log('✅ Found array data with', productsArray.length, 'items');
|
||||
} else if (dataToProcess && typeof dataToProcess === 'object') {
|
||||
// Handle different response formats
|
||||
const keys = Object.keys(response);
|
||||
const keys = Object.keys(dataToProcess);
|
||||
if (keys.length > 0 && keys.every(key => !isNaN(Number(key)))) {
|
||||
productsArray = Object.values(response);
|
||||
productsArray = Object.values(dataToProcess);
|
||||
console.log('✅ Found object with numeric keys, converted to array with', productsArray.length, 'items');
|
||||
} else {
|
||||
console.warn('⚠️ Response is object but not with numeric keys:', response);
|
||||
console.warn('⚠️ Response is object but not with numeric keys:', dataToProcess);
|
||||
console.warn('⚠️ Object keys:', keys);
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
console.warn('⚠️ Response is not array or object:', response);
|
||||
console.warn('⚠️ Response data is not array or object:', dataToProcess);
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -593,8 +612,19 @@ export class InventoryService {
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to fetch inventory products:', error);
|
||||
console.error('❌ Error details:', {
|
||||
message: error instanceof Error ? error.message : 'Unknown error',
|
||||
response: (error as any)?.response,
|
||||
status: (error as any)?.response?.status,
|
||||
data: (error as any)?.response?.data
|
||||
});
|
||||
|
||||
// Return empty array on error - let dashboard handle fallback
|
||||
// If it's an authentication error, throw it to trigger auth flow
|
||||
if ((error as any)?.response?.status === 401) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Return empty array on other errors - let dashboard handle fallback
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user