Files
vaishnavi-ecommerce-backend/src/controllers/users/wishlistController.js
2026-03-10 12:43:27 +05:30

246 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { prisma } = require('../../config/database');
// import Product from '../../models/mongodb/Product';
const Product = require('../../models/mongodb/Product');
// exports.getWishlist = async (req, res, next) => {
// try {
// const wishlist = await prisma.wishlistItem.findMany({
// where: { userId: req.user.id },
// orderBy: { createdAt: 'desc' },
// });
// // Fetch product details from MongoDB
// const detailedWishlist = await Promise.all(
// wishlist.map(async item => {
// const product = await Product.findById(item.productId).select(
// 'name basePrice variants images'
// );
// return {
// ...item,
// product: product || null,
// };
// })
// );
// // res.json({ success: true, data: { wishlist } });
// return res.status(200).json({
// // statusCode: 200,
// status: true,
// message: 'Wishlist fetched successfully',
// // data: { wishlist },
// data: { wishlist: detailedWishlist },
// });
// } catch (error) {
// next(error);
// }
// };
// exports.addToWishlist = async (req, res, next) => {
// try {
// const { productId } = req.body;
// // if (!productId) return res.status(400).json({ success: false, message: 'Product ID is required' });
// if (!productId) {
// return res.status(400).json({
// statusCode: 400,
// status: false,
// message: 'Product ID is required',
// });
// }
// const existing = await prisma.wishlistItem.findUnique({
// where: { userId_productId: { userId: req.user.id, productId } },
// });
// // if (existing) return res.status(400).json({ success: false, message: 'Item already in wishlist' });
// if (existing) {
// return res.status(400).json({
// statusCode: 400,
// status: false,
// message: 'Item already in wishlist',
// });
// }
// const item = await prisma.wishlistItem.create({
// data: { userId: req.user.id, productId },
// });
// // res.status(201).json({ success: true, message: 'Item added to wishlist', data: { item } });
// return res.status(201).json({
// statusCode: 201,
// status: true,
// message: 'Item added to wishlist',
// data: { item },
// });
// } catch (error) {
// next(error);
// }
// };
const mongoose = require("mongoose");
exports.getWishlist = async (req, res, next) => {
try {
const wishlist = await prisma.wishlistItem.findMany({
where: { userId: req.user.id },
orderBy: { createdAt: "desc" },
});
const detailedWishlist = await Promise.all(
wishlist.map(async (item) => {
let product = null;
// Only try MongoDB lookup if valid ObjectId
if (mongoose.Types.ObjectId.isValid(item.productId)) {
product = await Product.findById(item.productId).select(
"name basePrice variants images"
);
}
return {
...item,
product: product || null,
};
})
);
return res.status(200).json({
status: true,
message: "Wishlist fetched successfully",
data: { wishlist: detailedWishlist },
});
} catch (error) {
next(error);
}
};
// exports.addToWishlist = async (req, res, next) => {
// try {
// const { productId } = req.body;
// if (!productId) {
// return res.status(400).json({
// statusCode: 400,
// status: false,
// message: 'Product ID is required',
// });
// }
// const existing = await prisma.wishlistItem.findUnique({
// where: { userId_productId: { userId: req.user.id, productId } },
// });
// if (existing) {
// return res.status(400).json({
// statusCode: 400,
// status: false,
// message: 'Item already in wishlist',
// });
// }
// const item = await prisma.wishlistItem.create({
// data: { userId: req.user.id, productId },
// });
// return res.status(201).json({
// statusCode: 201,
// status: true,
// message: 'Item added to wishlist',
// data: { item },
// });
// } catch (error) {
// next(error);
// }
// };
// const mongoose = require("mongoose");
exports.addToWishlist = async (req, res, next) => {
try {
const { productId } = req.body;
if (!productId) {
return res.status(400).json({
statusCode: 400,
status: false,
message: "Product ID is required",
});
}
// 1⃣ Validate ObjectId
if (!mongoose.Types.ObjectId.isValid(productId)) {
return res.status(400).json({
status: false,
message: "Invalid product ID (must be MongoDB ObjectId)",
});
}
// 2⃣ Ensure product exists in MongoDB
const productExists = await Product.findById(productId);
if (!productExists) {
return res.status(404).json({
status: false,
message: "Product not found in database",
});
}
// 3⃣ Check duplicate
const existing = await prisma.wishlistItem.findUnique({
where: { userId_productId: { userId: req.user.id, productId } },
});
if (existing) {
return res.status(400).json({
status: false,
message: "Item already in wishlist",
});
}
// 4⃣ Save VALID productId in Prisma
const item = await prisma.wishlistItem.create({
data: { userId: req.user.id, productId },
});
return res.status(201).json({
status: true,
message: "Item added to wishlist",
data: { item },
});
} catch (error) {
next(error);
}
};
exports.removeFromWishlist = async (req, res, next) => {
try {
const { productId } = req.params;
const existing = await prisma.wishlistItem.findUnique({
where: { userId_productId: { userId: req.user.id, productId } },
});
// if (!existing) return res.status(404).json({ success: false, message: 'Item not found' });
if (!existing) {
return res.status(404).json({
statusCode: 404,
status: false,
message: 'Item not found in wishlist',
});
}
await prisma.wishlistItem.delete({
where: { userId_productId: { userId: req.user.id, productId } },
});
// res.json({ success: true, message: 'Item removed from wishlist' });
return res.status(200).json({
statusCode: 200,
status: true,
message: 'Item removed from wishlist',
});
} catch (error) {
next(error);
}
};