All files / src/api/inventory stockMutations.ts

100% Statements 61/61
100% Branches 2/2
100% Functions 1/1
100% Lines 61/61

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 621x 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 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x  
/**
 * @file stockMutations.ts
 * @module api/inventory/stockMutations
 *
 * @summary
 * Stock quantity adjustment mutations for inventory management.
 * Handles inbound purchases, outbound corrections, and inventory adjustments.
 *
 * @enterprise
 * - Purchase-style delta operations (positive/negative quantities)
 * - Business reason tracking for audit trails
 * - Tolerant error handling with boolean returns
 * - PATCH endpoint pattern for partial updates
 */
 
import http from '../httpClient';
import type { AdjustQuantityRequest } from './types';
 
/** Centralized endpoint base. */
export const INVENTORY_BASE = '/api/inventory';
 
/**
 * Adjust item quantity by delta (purchase/correction style).
 * Positive delta = purchase/inbound; negative = correction/outbound.
 *
 * @param req - Adjustment payload with item id, delta, and business reason
 * @returns true if successful, false otherwise
 *
 * @enterprise
 * Server commonly exposes: PATCH /{id}/quantity?delta=&reason=
 * Reasons typically map to business enums (PURCHASE, CORRECTION, WRITE_OFF, RETURN, etc.)
 *
 * @example
 * ```typescript
 * // Record a purchase (inbound)
 * const success = await adjustQuantity({
 *   id: 'ITEM-123',
 *   delta: 50,
 *   reason: 'PURCHASE'
 * });
 *
 * // Record a correction (outbound)
 * const corrected = await adjustQuantity({
 *   id: 'ITEM-123',
 *   delta: -5,
 *   reason: 'CORRECTION'
 * });
 * ```
 */
export async function adjustQuantity(req: AdjustQuantityRequest): Promise<boolean> {
  try {
    await http.patch(
      `${INVENTORY_BASE}/${encodeURIComponent(req.id)}/quantity`,
      null,
      { params: { delta: req.delta, reason: req.reason } }
    );
    return true;
  } catch {
    return false;
  }
}