const Wishlist = require('../models/Wishlist'); const { successResponse, errorResponse } = require('../utils/apiResponse'); const getWishlist = async (req, res, next) => { try { let wishlist = await Wishlist.findOne({ user: req.user._id }).populate('products'); if (!wishlist) { wishlist = await Wishlist.create({ user: req.user._id, products: [] }); } return successResponse(res, 'Wishlist retrieved', wishlist); } catch (error) { next(error); } }; const toggleWishlist = async (req, res, next) => { try { const { productId } = req.body; let wishlist = await Wishlist.findOne({ user: req.user._id }); if (!wishlist) { wishlist = new Wishlist({ user: req.user._id, products: [productId] }); } else { const exists = wishlist.products.includes(productId); if (exists) { wishlist.products.pull(productId); } else { wishlist.products.push(productId); } } await wishlist.save(); const updated = await Wishlist.findById(wishlist._id).populate('products'); return successResponse(res, 'Wishlist updated', updated); } catch (error) { next(error); } }; module.exports = { getWishlist, toggleWishlist };