All files / src/api ProductService.ts

86.66% Statements 13/15
100% Branches 4/4
75% Functions 6/8
86.66% Lines 13/15

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                    9x             2x 1x                       13x     6x                         5x 4x                   4x 3x                     8x     5x                   17x 6x                                                      
/**
 * @file ProductService.ts
 * @description
 * Product API service providing CRUD operations and product search functionality.
 *
 * **Responsibilities:**
 * - Handle all product-related API requests
 * - Provide abstraction layer for backend API calls
 * - Process API responses and error handling
 *
 * **API Endpoints Covered:**
 * - GET /api/products - Fetch all products
 * - GET /api/products/paged - Fetch paginated products
 * - POST /api/products - Create new product
 * - DELETE /api/products/:id - Delete product
 * - GET /api/products/search - Search products by name
 * - GET /api/products/:id - Get product by ID
 * - PUT /api/products/:id/quantity - Update product quantity
 * - PUT /api/products/:id/price - Update product price
 *
 * @module ProductService
 * @requires ../services/apiClient
 */
 
import apiClient from '../services/apiClient';
 
const ProductService = {
  /**
   * Fetches all products from the backend
   * @async
   * @returns {Promise<any>} List of all products
   */
  fetchProducts: async () => {
    const response = await apiClient.get('/api/products');
    return response.data;
  },
 
  /**
   * Fetches paginated products with page and size parameters
   * @async
   * @param {number} page - Page number (0-indexed)
   * @param {number} size - Items per page
   * @returns {Promise<any>} Paginated product data
   * @throws {Error} API request failure
   */
  fetchPagedProducts: async (page: number, size: number) => {
    const response = await apiClient.get('/api/products/paged', {
      params: { page, size },
    });
    return response.data.data;
  },
 
  /**
   * Creates a new product in the inventory
   * @async
   * @param {Object} product - Product data
   * @param {string} product.name - Product name
   * @param {number} product.quantity - Stock quantity
   * @param {number} product.price - Product price
   * @returns {Promise<any>} Created product response
   */
  addProduct: async (product: { name: string; quantity: number; price: number }) => {
    const response = await apiClient.post('/api/products', product);
    return response.data;
  },
 
  /**
   * Deletes a product from inventory by ID
   * @async
   * @param {number} id - Product ID to delete
   * @returns {Promise<any>} Deletion confirmation response
   */
  deleteProduct: async (id: number) => {
    const response = await apiClient.delete(`/api/products/${id}`);
    return response.data;
  },
 
  /**
   * Searches products by name (partial match supported)
   * @async
   * @param {string} name - Product name or partial name to search
   * @returns {Promise<any[]>} Array of matching products (empty if none found)
   * @throws {Error} API request failure
   */
  searchProductsByName: async (name: string) => {
    const response = await apiClient.get(`/api/products/search`, {
      params: { name },
    });
    return response.status === 204 || !response.data ? [] : response.data;
  },
 
  /**
   * Retrieves a specific product by ID
   * @async
   * @param {number} id - Product ID
   * @returns {Promise<any>} Product data
   */
  getProductById: async (id: number) => {
    const response = await apiClient.get(`/api/products/${id}`);
    return response.data.data;
  },
 
  /**
   * Updates product quantity
   * @async
   * @param {number} id - Product ID
   * @param {number} quantity - New quantity value
   * @returns {Promise<any>} Update confirmation response
   */
  updateProductQuantity: async (id: number, quantity: number) => {
    return apiClient.put(`/api/products/${id}/quantity`, { quantity });
  },
 
  /**
   * Updates product price
   * @async
   * @param {number} id - Product ID
   * @param {number} price - New price value
   * @returns {Promise<any>} Update confirmation response
   */
  updateProductPrice: async (id: number, price: number) => {
    return apiClient.put(`/api/products/${id}/price`, { price });
  },
};
 
export default ProductService;