All files / src/api/analytics/hooks useStockAnalytics.ts

100% Statements 103/103
100% Branches 6/6
100% Functions 6/6
100% Lines 103/103

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 93 94 95 96 97 98 99 100 101 102 103 1041x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x  
/**
 * @file useStockAnalytics.ts
 * @module api/analytics/hooks
 *
 * @summary
 * Stock-focused analytics hooks for inventory trend analysis.
 * Provides hooks for stock value trends, monthly movements, and supplier distribution.
 *
 * @enterprise
 * - Consistent 5-minute cache strategy for trend data
 * - Parameter validation before API calls
 * - Full TypeDoc documentation for analytics queries
 * - Composable hooks for dashboard and report views
 */
 
import { useQuery } from '@tanstack/react-query';
import {
  getStockValueOverTime,
  getMonthlyStockMovement,
  getStockPerSupplier,
} from '../index';
import type { AnalyticsParams, StockMovementParams } from '../validation';
 
/**
 * Hook to load stock value over time (trend chart data).
 * Caches results for 5 minutes.
 *
 * @param params - Date range and optional supplier filter
 * @param enabled - Whether to fetch (defaults to true)
 * @returns React Query result with stock value points
 *
 * @example
 * ```typescript
 * const { data: points, isLoading } = useStockValueQuery({
 *   from: '2025-09-01',
 *   to: '2025-11-30',
 *   supplierId: 'SUP-001'
 * });
 *
 * return <LineChart data={points} loading={isLoading} />;
 * ```
 */
export function useStockValueQuery(params: AnalyticsParams, enabled: boolean = true) {
  return useQuery({
    queryKey: ['analytics', 'stock-value', params],
    queryFn: () => getStockValueOverTime(params),
    enabled,
    staleTime: 5 * 60_000,
    gcTime: 15 * 60_000,
  });
}
 
/**
 * Hook to load monthly stock movement (inbound/outbound).
 * Useful for trend analysis and planning.
 *
 * @param params - Date range and optional supplier filter
 * @param enabled - Whether to fetch (defaults to true)
 * @returns React Query result with monthly movement data
 *
 * @example
 * ```typescript
 * const { data: movements } = useMonthlyMovementQuery({
 *   from: '2025-09-01',
 *   to: '2025-11-30'
 * });
 *
 * return <BarChart data={movements} />;
 * ```
 */
export function useMonthlyMovementQuery(params: StockMovementParams, enabled: boolean = true) {
  return useQuery({
    queryKey: ['analytics', 'monthly-movement', params],
    queryFn: () => getMonthlyStockMovement(params),
    enabled,
    staleTime: 5 * 60_000,
    gcTime: 15 * 60_000,
  });
}
 
/**
 * Hook to load current stock distribution per supplier.
 * Useful for supplier-level analytics and reporting.
 *
 * @param enabled - Whether to fetch (defaults to true)
 * @returns React Query result with per-supplier stock totals
 *
 * @example
 * ```typescript
 * const { data: distribution } = useStockPerSupplierQuery();
 *
 * return <DoughnutChart data={distribution} />;
 * ```
 */
export function useStockPerSupplierQuery(enabled: boolean = true) {
  return useQuery({
    queryKey: ['analytics', 'stock-per-supplier'],
    queryFn: () => getStockPerSupplier(),
    enabled,
    staleTime: 5 * 60_000,
    gcTime: 15 * 60_000,
  });
}