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

100% Statements 70/70
100% Branches 6/6
100% Functions 4/4
100% Lines 70/70

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 711x 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 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x  
/**
 * @file useLowStockAlerts.ts
 * @module api/analytics/hooks
 *
 * @summary
 * Low-stock and inventory frequency hooks for alert and monitoring features.
 * Provides hooks for identifying low-stock items and analyzing item update patterns.
 *
 * @enterprise
 * - Consistent 5-minute cache strategy for alert data
 * - Supplier-specific filtering support
 * - Conditional fetching with supplier validation
 * - Full TypeDoc documentation for monitoring queries
 */
 
import { useQuery } from '@tanstack/react-query';
import {
  getItemUpdateFrequency,
  getLowStockItems,
} from '../index';
 
/**
 * Hook to load item update frequency for a supplier.
 * Shows which items are modified most frequently.
 *
 * @param supplierId - Supplier to analyze
 * @param enabled - Whether to fetch (defaults to true)
 * @returns React Query result with frequency data per item
 *
 * @example
 * ```typescript
 * const { data: frequencies } = useItemFrequencyQuery('SUP-001');
 *
 * return <Table data={frequencies} />;
 * ```
 */
export function useItemFrequencyQuery(supplierId: string, enabled: boolean = true) {
  return useQuery({
    queryKey: ['analytics', 'item-frequency', supplierId],
    queryFn: () => getItemUpdateFrequency(supplierId),
    enabled: enabled && !!supplierId,
    staleTime: 5 * 60_000,
    gcTime: 15 * 60_000,
  });
}
 
/**
 * Hook to load items below minimum stock threshold.
 * Useful for inventory planning and alerts.
 *
 * @param supplierId - Supplier to check
 * @param enabled - Whether to fetch (defaults to true)
 * @returns React Query result with low stock items
 *
 * @example
 * ```typescript
 * const { data: lowStockItems } = useLowStockQuery('SUP-001');
 *
 * return <AlertTable data={lowStockItems} />;
 * ```
 */
export function useLowStockQuery(supplierId: string, enabled: boolean = true) {
  return useQuery({
    queryKey: ['analytics', 'low-stock-items', supplierId],
    queryFn: () => getLowStockItems(supplierId),
    enabled: enabled && !!supplierId,
    staleTime: 5 * 60_000,
    gcTime: 15 * 60_000,
  });
}