All files / src/logic DashboardLogic.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                                                5x                   21x   21x         5x 21x   21x         2x 2x          
/**
 * @file DashboardLogic.ts
 * @description
 * Business logic for dashboard data retrieval and aggregation.
 *
 * **Features:**
 * - Parallel fetching of stock value and low-stock products
 * - Type-safe data response with fallback defaults
 * - Centralized error handling
 *
 * **API Endpoints:**
 * - GET /api/products/total-stock-value - Total inventory value
 * - GET /api/products/low-stock - Products with low quantities
 *
 * @module
 */
 
import apiClient from '../services/apiClient';
import { Product } from '../types/Product';
 
/**
 * Dashboard data retrieval service
 * Fetches aggregated inventory metrics for dashboard display
 */
const DashboardLogic = {
  /**
   * Fetch total stock value and low-stock products in parallel
   * @returns {Promise<{stockValue: number, lowStock: Product[]}>} Stock metrics
   * @throws Will propagate API errors to caller
   */
  async fetchDashboardData(): Promise<{
    stockValue: number;
    lowStock: Product[];
  }> {
    try {
      // Fetch both metrics in parallel for performance
      const [stockValueResponse, lowStockResponse] = await Promise.all([
        apiClient.get('/api/products/total-stock-value'),
        apiClient.get('/api/products/low-stock'),
      ]);
 
      const stockValueData = stockValueResponse.data?.data || 0;
      const lowStockData = Array.isArray(lowStockResponse.data) ? lowStockResponse.data as Product[] : [];
 
      return {
        stockValue: stockValueData,
        lowStock: lowStockData,
      };
    } catch (error) {
      console.error('Error fetching dashboard data:', error);
      throw error;
    }
  },
};
 
export default DashboardLogic;