text: //@desc Adding pagination , filter ect... for query
  const customResults = (model, populate) => async (req, res, next) => {
    let query;
    // Copy req.query
    const reqQuery = { ...req.query };
  
    // Field to exclude
    const removeField = ["select", "sort", "page", "limit"];
  
    // Loop over removeFields and delete them from reqQuery
    removeField.forEach((param) => delete reqQuery[param]);
  
    // Create query string
    let queryString = JSON.stringify(reqQuery);
  
    // Create operators ($gt, $gte, ...)
    queryString = queryString.replace(
      /\b(gt|gte|lt|lte|in)\b/g,
      (match) => `$${match}`
    );
  
    //Finding resource
    query = model.find(JSON.parse(queryString));
  
    // Select Fields
    if (req.query.select) {
      const field = req.query.select.split(",").join(" ");
      query = query.select(field);
    }
  
    // Sorting
    if (req.query.sort) {
      const sortBy = req.query.sort.split(",").join(" ");
      query = query.sort(sortBy);
    } else {
      query = query.sort("-createdAt");
    }
  
    //pagination
    const page = parseInt(req.query.page, 10) || 1;
    const limit = parseInt(req.query.limit, 10) || 5;
    const startFrom = (page - 1) * limit;
    const endTo = page * limit;
    const total = await model.countDocuments();
    query.skip(startFrom).limit(limit);
  
    if (populate) {
      query.populate(populate);
    }
  
    //executing query
    const result = await query;
  
    //Pagination result
    const pagination = {};
    if (endTo < total) {
      pagination.next = {
        page: page + 1,
        limit,
      };
    }
    if (startFrom > 0) {
      pagination.prv = {
        page: page - 1,
        limit,
      };
    }
  
    res.customResults = {
      success: true,
      count: result.length,
      data: result,
      pagination,
    };
  
    next();
  };
  
  module.exports = customResults;
  