All files / src/api/inventory priceMutations.ts

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

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 581x 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 priceMutations.ts
 * @module api/inventory/priceMutations
 *
 * @summary
 * Price change mutations for inventory items.
 * Handles item unit price updates with audit trail support.
 *
 * @enterprise
 * - Single-responsibility: focused on price changes only
 * - Tolerant error handling with boolean returns
 * - PATCH endpoint pattern for atomic price updates
 * - URL-safe ID encoding for special characters
 */
 
import http from '../httpClient';
import type { ChangePriceRequest } from './types';
 
/** Centralized endpoint base. */
export const INVENTORY_BASE = '/api/inventory';
 
/**
 * Change item unit price.
 * Updates the unit price for the specified item.
 *
 * @param req - Price change payload with item id and new price
 * @returns true if successful, false otherwise
 *
 * @enterprise
 * Server commonly exposes: PATCH /{id}/price?price=
 * Price validation (minimum/maximum) typically handled by backend.
 *
 * @example
 * ```typescript
 * // Update item price
 * const success = await changePrice({
 *   id: 'ITEM-123',
 *   price: 29.99
 * });
 *
 * if (!success) {
 *   console.error('Price update failed');
 * }
 * ```
 */
export async function changePrice(req: ChangePriceRequest): Promise<boolean> {
  try {
    await http.patch(
      `${INVENTORY_BASE}/${encodeURIComponent(req.id)}/price`,
      null,
      { params: { price: req.price } }
    );
    return true;
  } catch {
    return false;
  }
}