Fix issues 4

This commit is contained in:
Urtzi Alfaro
2025-08-17 15:21:10 +02:00
parent cafd316c4b
commit f33f5d242a
6 changed files with 255 additions and 45 deletions

View File

@@ -547,6 +547,9 @@ export class InventoryService {
*/
async getProductsList(tenantId: string): Promise<ProductInfo[]> {
try {
console.log('🔍 Fetching products for forecasting...', { tenantId });
// First try to get finished products (preferred for forecasting)
const response = await apiClient.get(`/tenants/${tenantId}/ingredients`, {
params: {
limit: 100,
@@ -606,7 +609,47 @@ export class InventoryService {
}))
.filter(product => product.inventory_product_id && product.name);
console.log('📋 Processed inventory products:', products);
console.log('📋 Processed finished products:', products);
// If no finished products found, try to get all products as fallback
if (products.length === 0) {
console.log('⚠️ No finished products found, trying to get all products as fallback...');
const fallbackResponse = await apiClient.get(`/tenants/${tenantId}/ingredients`, {
params: {
limit: 100,
// No product_type filter to get all products
},
});
console.log('🔍 Fallback API Response:', fallbackResponse);
const fallbackDataToProcess = fallbackResponse?.data || fallbackResponse;
let fallbackProductsArray: any[] = [];
if (Array.isArray(fallbackDataToProcess)) {
fallbackProductsArray = fallbackDataToProcess;
} else if (fallbackDataToProcess && typeof fallbackDataToProcess === 'object') {
const keys = Object.keys(fallbackDataToProcess);
if (keys.length > 0 && keys.every(key => !isNaN(Number(key)))) {
fallbackProductsArray = Object.values(fallbackDataToProcess);
}
}
const fallbackProducts: ProductInfo[] = fallbackProductsArray
.map((product: any) => ({
inventory_product_id: product.id || product.inventory_product_id,
name: product.name || product.product_name || `Product ${product.id || ''}`,
category: product.category,
current_stock: product.current_stock,
unit: product.unit,
cost_per_unit: product.cost_per_unit
}))
.filter(product => product.inventory_product_id && product.name);
console.log('📋 Processed fallback products (all inventory items):', fallbackProducts);
return fallbackProducts;
}
return products;