|
|
|
@ -387,31 +387,33 @@ exports.updateProductQuantity = async (req, res) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.removeProductFromCart = async (req, res) => {
|
|
|
|
|
exports.removeProductsFromCart = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const productId = req.params.productId;
|
|
|
|
|
const productIds = req.body.productIds; // Array of product IDs to remove
|
|
|
|
|
const user = await User.findById(req.user.id);
|
|
|
|
|
const existingItemIndex = user.myCart.findIndex(item => item.productId === productId);
|
|
|
|
|
|
|
|
|
|
let x = await User.findByIdAndUpdate(
|
|
|
|
|
// Update the user's cart by removing the products with the given IDs
|
|
|
|
|
const updatedUser = await User.findByIdAndUpdate(
|
|
|
|
|
req.user.id,
|
|
|
|
|
{
|
|
|
|
|
$pull: { myCart: { productId } }
|
|
|
|
|
$pull: { myCart: { productId: { $in: productIds } } }
|
|
|
|
|
},
|
|
|
|
|
{ new: true } // Return the updated user document
|
|
|
|
|
{ new: true }
|
|
|
|
|
).exec();
|
|
|
|
|
|
|
|
|
|
if (existingItemIndex <= 0) {
|
|
|
|
|
res.json({ message: 'Product not in cart' });
|
|
|
|
|
}else{
|
|
|
|
|
res.json({ message: 'Product removed from cart successfully' });
|
|
|
|
|
}
|
|
|
|
|
// Check if any products were removed from the cart
|
|
|
|
|
if (updatedUser) {
|
|
|
|
|
res.json({ message: 'Products removed from cart successfully' });
|
|
|
|
|
} else {
|
|
|
|
|
res.status(404).json({ message: 'No products removed from cart' });
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({ message: 'Error removing product from cart' });
|
|
|
|
|
res.status(500).json({ message: 'Error removing products from cart' });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getCartDetails = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const user = await User.findById(req.user.id);
|
|
|
|
|