Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
2.0 kB
1
Indexable
Never
const mongoose = require('mongoose');
const User = require('../Models/userModel.js');
const SubCategory = require('../Models/subcategoryModel.js');
const Group = require('../Models/groupModel.js');
const Brand = require('../Models/brandModel.js');
const Category = require('../Models/categoryModel.js');
const Product = require('../Models/productModel.js');
const AppError = require('../utils/appError.js');
const catchAsync = require('../utils/catchAsync.js');
const { isDecimal } = require('validator');
const Client = require('../Models/clientModel.js');
const Cart = require('../Models/cartModel.js');
const { createAndSendToken } = require('./authController.js');
const Order = require('../Models/orderModel.js');
const Retailer = require('../Models/retailerModel.js');
const Image = require('../Models/imageModel.js');
const RetailerRequest = require('../Models/retailerRequestModel.js');
const Notification = require('../Models/notificationModel.js');
const Socket = require('../socket/socketIndex.js')


exports.getNotificationsByUser = catchAsync(async(req,res,next) => {
  const { id} = req.params;
 
  /*const user = await User.find({_id:toObjectId(id,next)})
    .populate({ path: 'notifications',select:['content','vu']});*/
    const user = await User.aggregate([
        {$match:{_id: toObjectId(id,next)}},
        {
            $lookup:{
                from: 'notifications',
                localField: 'notifications',
                foreignField: '_id',
                as: 'notifications'
            }
        }
    ])
  if(!user){
    return next( new AppError('invalid user',400))
  }
  console.log('user',user[0])

    res.status(200).json({
      status: 'success',
      data: user[0]?.notifications
    })
  
})



const toObjectId = (id, next) => {
    try {
        return new mongoose.Types.ObjectId(id);
    }catch (e) {
        return next(new AppError('Invalid id.', 401));
    }
    };
    
Leave a Comment