Source: product/productService.js

/**
 * @class ProductManager
 * Manages a collection of products, allowing addition, deletion, update, and retrieval of products.
 */
class productService {
    /**
    * Creates an instance of ProductManager.
    */
   constructor(){
       /**
        * @property {Array} products - Array to store products.
        */
       this.products = [];
   }
   /**
    * Adds a new product to the collection.
    * @param {Object} product - The product object to add.
    */
   addProduct(product) {
       this.products.push(product);
   }
   /**
    * Deletes a product by its ID.
    * @param {number|string} productId - The ID of the product to delete.
    */
   deleteProduct(productId){
       const index =this.products.findIndex(product => product.productId === productId)
           if(index !== -1){
               this.products.splice(index,1);
           }
       }
    /**
    * Retrieves a product by its ID.
    * @param {number|string} productId - The ID of the product to retrieve.
    * @returns {Object|null} The product object if found, otherwise null.
    */
   getProductById(productId){
       for( let product of this.products){
           if(product.productId === productId){
               return product;
           }
       }
       return null;
   }
   /**
    * Updates a product's information by its ID.
    * @param {number|string} productId - The ID of the product to update.
    * @param {Object} updates - An object containing the updated properties.
    * @returns {boolean} True if the update was successful, otherwise false.
    */
   updateProduct(productId, updates){
       const product = this.getProductById(productId);
       if(product){
           Object.assign(product,updates);
           product.updatedAt = new Date()
           return true;
       }
       return false;
   }
    /**
    * Lists all products in the collection.
    * @returns {Array} An array of all products.
    */
   
   listProducts() {
       return this.products;
   }
    /**
    * Retrieves all available products.
    * @returns {Array} An array of available products.
    */
   getAvailableProducts() {
       const availableProducts = [];
       function checkAvailability(product) {
           if (product.isAvailable) {
               availableProducts.push(product);
           }
       }
       this.products.forEach(checkAvailability);
       return availableProducts;
   }
   /**
    * Retrieves all special products.
    * @returns {Array} An array of special products.
    */
   getSpecialProducts() {
       const specialProducts = [];
       function checkSpecial(product) {
           if (product.isSpecial) {
               specialProducts.push(product);
           }
       }
       this.products.forEach(checkSpecial);
       return specialProducts;
   }
    /**
    * Retrieves products by a specific category ID.
    * @param {number|string} categoryId - The ID of the category.
    * @returns {Array} An array of products in the specified category.
    */
   getProductsByCategory(categoryId) {
       const categoryProducts = [];
       function checkCategory(product) {
           if (product.categoryId === categoryId) {
               categoryProducts.push(product);
           }
       }
       this.products.forEach(checkCategory);
       return categoryProducts;
   }
}

module.exports = productService;