Spaces:
Sleeping
Sleeping
File size: 3,598 Bytes
efb87a5 |
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 |
// 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(`⚠️ <b>Low Stock Alert</b>\nProduct: <b>${product.name}</b> 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,
}; |