Fix new services implementation 11

This commit is contained in:
Urtzi Alfaro
2025-08-16 08:43:35 +02:00
parent 119beb541f
commit 995a51e285
8 changed files with 114 additions and 71 deletions

View File

@@ -304,9 +304,11 @@ private buildURL(endpoint: string): string {
}
const responseData = await response.json();
console.log('🔍 Raw responseData from fetch:', responseData);
// Apply response interceptors
const processedResponse = await this.applyResponseInterceptors(responseData);
console.log('🔍 processedResponse after interceptors:', processedResponse);
return processedResponse;
});
@@ -335,9 +337,14 @@ private buildURL(endpoint: string): string {
// Handle both wrapped and unwrapped responses
// If result has a 'data' property, return it; otherwise return the result itself
console.log('🔍 Final result before return:', result);
console.log('🔍 Result has data property?', result && typeof result === 'object' && 'data' in result);
if (result && typeof result === 'object' && 'data' in result) {
console.log('🔍 Returning result.data:', result.data);
return result.data as T;
}
console.log('🔍 Returning raw result:', result);
return result as T;
} catch (error) {
// Record error metrics

View File

@@ -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 [];
}
}

View File

@@ -12,6 +12,10 @@ export interface ProductInfo {
sales_count?: number;
total_quantity?: number;
last_sale_date?: string;
// Additional inventory fields
current_stock?: number;
unit?: string;
cost_per_unit?: number;
}
export interface SalesData {