All files / src/api/analytics metrics.ts

100% Statements 92/92
83.33% Branches 10/12
100% Functions 3/3
100% Lines 92/92

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * @file metrics.ts
 * @module api/analytics/metrics
 *
 * @summary
 * Dashboard KPI metrics - lightweight API helpers for inventory counts.
 * All functions are tolerant and never throw; they return graceful defaults.
 *
 * @enterprise
 * - Resilient: Returns 0 on error instead of throwing
 * - Fast: Direct HTTP calls for simple count operations
 * - Type-safe: Promise<number> return type
 * - No side effects: Pure functions suitable for React Query hooks
 */
 
import http from '../httpClient';
 
/**
 * Fetch total inventory item count from backend.
 * Returns 0 on any error (tolerant).
 *
 * @returns Total number of inventory items
 * @example
 * ```typescript
 * const count = await getItemCount();
 * console.log(count); // 150
 * ```
 */
export async function getItemCount(): Promise<number> {
  try {
    const { data } = await http.get<number>('/api/inventory/count');
    return Number(data ?? 0);
  } catch {
    return 0;
  }
}
 
/**
 * Fetch total suppliers count from backend.
 * Returns 0 on any error (tolerant).
 *
 * @returns Total number of suppliers
 * @example
 * ```typescript
 * const count = await getSupplierCount();
 * console.log(count); // 25
 * ```
 */
export async function getSupplierCount(): Promise<number> {
  try {
    const { data } = await http.get<number>('/api/suppliers/count');
    return Number(data ?? 0);
  } catch {
    return 0;
  }
}
 
/**
 * Fetch count of items below minimum stock threshold.
 * Returns 0 on any error (tolerant).
 *
 * @returns Total count of low-stock items
 * @example
 * ```typescript
 * const count = await getLowStockCount();
 * console.log(count); // 12
 * ```
 */
export async function getLowStockCount(): Promise<number> {
  try {
    const { data } = await http.get<number>('/api/analytics/low-stock/count');
    return Number(data ?? 0);
  } catch {
    return 0;
  }
}
 
/* -------------------------------------------------------------------------- */
/* Aliases for backward compatibility                                         */
/* -------------------------------------------------------------------------- */
 
/**
 * Alias for getItemCount() - kept for backward compatibility with Dashboard.tsx
 * @deprecated Use getItemCount() instead
 */
export const getInventoryCount = getItemCount;
 
/**
 * Alias for getSupplierCount() - kept for backward compatibility with Dashboard.tsx
 * @deprecated Use getSupplierCount() instead
 */
export const getSuppliersCount = getSupplierCount;