// src/controllers/productController.js
const Product = require('../models/Product');
const sendTelegramAlert = require('../utils/telegram');
// @desc Fetch all products (with search & category filter)
// @route GET /api/products
// @access Public
const getProducts = async (req, res) => {
try {
const keyword = req.query.keyword
? {
name: {
$regex: req.query.keyword,
$options: 'i', // Case insensitive
},
}
: {};
// Filter by category if provided
const categoryFilter = req.query.category ? { category: req.query.category } : {};
const products = await Product.find({ ...keyword, ...categoryFilter });
res.json(products);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
// @desc Fetch single product
// @route GET /api/products/:id
// @access Public
const getProductById = async (req, res) => {
try {
const product = await Product.findById(req.params.id);
if (product) {
res.json(product);
} else {
res.status(404).json({ message: 'Product not found' });
}
} catch (error) {
res.status(500).json({ message: error.message });
}
};
// @desc Create a product
// @route POST /api/products
// @access Private/Admin
const createProduct = async (req, res) => {
const { name, price, originalPrice, description, image, category, stock } = req.body;
try {
const product = new Product({
name,
price,
originalPrice,
description,
image,
category,
stock,
});
const createdProduct = await product.save();
res.status(201).json(createdProduct);
} catch (error) {
res.status(400).json({ message: error.message });
}
};
// @desc Update a product
// @route PUT /api/products/:id
// @access Private/Admin
const updateProduct = async (req, res) => {
const { name, price, originalPrice, description, image, category, stock, isActive } = req.body;
try {
const product = await Product.findById(req.params.id);
if (product) {
product.name = name || product.name;
product.price = price || product.price;
product.originalPrice = originalPrice !== undefined ? originalPrice : product.originalPrice;
product.description = description || product.description;
product.image = image || product.image;
product.category = category || product.category;
product.stock = stock !== undefined ? stock : product.stock;
product.isActive = isActive !== undefined ? isActive : product.isActive;
// 🔥 Stock Low Alert
if (product.stock < 5) {
sendTelegramAlert(`⚠️ Low Stock Alert\nProduct: ${product.name} is down to ${product.stock} units!`);
}
const updatedProduct = await product.save();
res.json(updatedProduct);
} else {
res.status(404).json({ message: 'Product not found' });
}
} catch (error) {
res.status(400).json({ message: error.message });
}
};
// @desc Delete a product
// @route DELETE /api/products/:id
// @access Private/Admin
const deleteProduct = async (req, res) => {
try {
const product = await Product.findById(req.params.id);
if (product) {
await product.deleteOne();
res.json({ message: 'Product removed' });
} else {
res.status(404).json({ message: 'Product not found' });
}
} catch (error) {
res.status(500).json({ message: error.message });
}
};
module.exports = {
getProducts,
getProductById,
createProduct,
updateProduct,
deleteProduct,
};