Untitled

 avatar
unknown
plain_text
10 months ago
5.9 kB
4
Indexable
exports.AddToWishlist = catchAsync(async (req, res, next) => {
  // Extract necessary information from the request body
  const { userID, productID } = req.body;

  // Find the user by ID and check if the client profile and wishlist exist
  const user = await User.findById(userID);
  if (!user) {
    return next(new AppError('User not found', 404));
  }
  // Check if the product exists and is available for wishlisting
  const product = await Product.findById(productID);
  if (!product) {
    return next(new AppError(`Product with ID ${productID} not found`, 404));
  }
if(user.role == "client"){
  const client = await Client.findById(user.profile);
  if (!client) {
    return next(new AppError('Client not found', 404));
  }

  // Check if the product already exists in the client's wishlist
  const productExistsInWishlist = await Client.findOne({ _id: user.profile, wishlist: { $in: [productID] } });
  if (productExistsInWishlist) {
    return res.status(400).json({ message: 'Product already exists in the wishlist' });
  }

  // Add the product to the wishlist
  client.wishlist.push(product);
  await client.save();

  // Return success response
  res.status(200).json({ 
    message: 'Product added to wishlist successfully',
    wishlist: client.wishlist 
  });
}else{
  const retailer = await Retailer.findById(user.profile);
  if (!retailer) {
    return next(new AppError('retailer not found', 404));
  }


  // Check if the product already exists in the client's wishlist
  const productExistsInWishlist = await Retailer.findOne({ _id: user.profile, wishlist: { $in: [productID] } });
  if (productExistsInWishlist) {
    return res.status(400).json({ message: 'Product already exists in the wishlist' });
  }

  // Add the product to the wishlist
  retailer.wishlist.push(product);
  await retailer.save();

  res.status(200).json({ 
    message: 'Product added to wishlist successfully',
    wishlist: retailer.wishlist 
  });
}
});





exports.RemoveFromWishlist = catchAsync(async (req, res, next) => {
  // Extract necessary information from the request body
  const { userID, productID } = req.body;

  // Find the user by ID and check if the client profile and wishlist exist
  const user = await User.findById(userID);
  if (!user) {
    return next(new AppError('User not found', 404));
  }

  if(user.role == "client"){
  const client = await Client.findById(user.profile);
  if (!client) {
    return next(new AppError('Client not found', 404));
  }
  // Check if the product exists in the client's wishlist
  if (!client.wishlist.includes(productID)) {
    return res.status(400).json({ message: 'Product does not exist in the wishlist' });
  }
  // Remove the product from the client's wishlist using $pull
  await Client.findByIdAndUpdate(client._id, { $pull: { wishlist: productID } });
  // Return success response
  res.status(200).json({
     message: 'Product removed from wishlist successfully'
  });
  }else{
    const retailer = await Retailer.findById(user.profile);
    if (!retailer) {
      return next(new AppError('retailer not found', 404));
    }
    if (!retailer.wishlist.includes(productID)) {
      return res.status(400).json({ message: 'Product does not exist in the wishlist' });
    }
    // Remove the product from the client's wishlist using $pull
    await Retailer.findByIdAndUpdate(retailer._id, { $pull: { wishlist: productID } });
    // Return success response
    res.status(200).json({
       message: 'Product removed from wishlist successfully'
    });
  }
});





exports.getWishList = catchAsync(async (req, res, next) => {
  const { userId } = req.body;
  const user = await User.findById(toObjectId(userId,next));
  if (!user) {
    return next(new AppError('User not found', 404));
  }
  
  if(user.role == "client"){
  const client = await User.aggregate([
    {
      $match: { _id: toObjectId(userId) }
    },
    {
      $lookup: {
        from: 'clients',
        localField: 'profile',
        foreignField: '_id',
        as: 'client',
        pipeline: [
          {
            $lookup: {
              from: 'products',
              localField: 'wishlist',
              foreignField: '_id',
              pipeline: [{
                $lookup: {
                  from: 'offers',
                  localField: 'offer',
                  foreignField: '_id',
                  pipeline: [{$project:{discount:1,status:1}}],
                  as: 'offer'
                }
              }],
              as: 'wishlist'
            }
          }
        ]
      }
    }
  ]);
  if(!client.length){
    return next(new AppError('user not find', 400));
  }
  res.status(200).json({
    message: 'wishlist', 
    wishlist: client[0]?.client[0]?.wishlist 
  });

 }else{
  const retailer = await User.aggregate([
    {
      $match: { _id: toObjectId(userId) }
    },
    {
      $lookup: {
        from: 'retailers',
        localField: 'profile',
        foreignField: '_id',
        as: 'retailer',
        pipeline: [
          {
            $lookup: {
              from: 'products',
              localField: 'wishlist',
              foreignField: '_id',
              pipeline: [{
                $lookup: {
                  from: 'offers',
                  localField: 'offer',
                  foreignField: '_id',
                  pipeline: [{$project:{discount:1,status:1}}],
                  as: 'offer'
                }
              }],
              as: 'wishlist'
            }
          }
        ]
      }
    }
  ]);
  if(!retailer.length){
    return next(new AppError('user not find', 400));
  }
  res.status(200).json({
    message: 'wishlist', 
    wishlist: retailer[0]?.retailer[0]?.wishlist 
  });
 }
});
Editor is loading...
Leave a Comment