Fix issues 4
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user