{"size": 2281, "ext": "js", "max_stars_repo_path": "controllers/userController.js", "max_stars_repo_name": "meghaldonde/Back-End", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controllers/userController.js", "max_issues_repo_name": "meghaldonde/Back-End", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/userController.js", "max_forks_repo_name": "meghaldonde/Back-End", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.6292134831, "max_line_length": 98, "alphanum_fraction": 0.6514686541}
const multer = require('multer');
const sharp = require('sharp');

const User = require('../models/userModel');

const catchAsync = require('../utils/catchAsync');

//Save photo in memoryStorage as buffer
const multerStorage = multer.memoryStorage();

//multer Filter
const multerFilter = (req, file, cb) => {
  if (file.mimetype.startsWith('image')) {
    cb(null, true);
  } else {
    cb(new AppError('Not an image! Please upload only images!', 400), false);
  }
};

const upload = multer({
  storage: multerStorage,
  fileFilter: multerFilter,
});

exports.uploadUserPhoto = upload.single('photo');

exports.resizeUserPhoto = catchAsync(async (req, res, next) => {
  if (!req.file) return next();

  // name of user photo will be user-userId-curent_date (to avoid same name of users photos)
  req.file.filename = `user-${req.user.id}-${Date.now()}.jpeg`;

  await sharp(req.file.buffer)
    .resize(500, 500)
    .toFormat('jpeg')
    .jpeg({ quality: 90 })
    .toFile(`img/users/${req.file.filename}`);

  next();
});

const filterRequestObj = async (reqBodyObj, ...allowedFields) => {
  const filteredObj = {};
  Object.keys(reqBodyObj).forEach((el) => {
    if (allowedFields.includes(el)) filteredObj[el] = reqBodyObj[el];
  });
  return filteredObj;
};

exports.getMe = catchAsync(async (req, res, next) => {
  const user = await User.findById(req.user.id);
  res.status(200).json({
    status: 'success',
    data: {
      user,
    },
  });
});

exports.updateMe = catchAsync(async (req, res, next) => {
  //To avoid that a malicious user change his role to an admin and grant all the access to the app
  const filteredBody = await filterRequestObj(req.body, 'name', 'email', 'bio');
  //Save image name to the database
  if (req.file) filteredBody.photo = req.file.filename;
  // we use req.user from the protect middellware
  const updatedUser = await User.findByIdAndUpdate(req.user.id, filteredBody, {
    new: true,
    runValidators: true,
  });

  res.status(200).json({
    status: 'success',
    data: {
      user: updatedUser,
    },
  });
});

exports.getAllUsers = catchAsync(async (req, res, next) => {
  const users = await User.find();
  res.status(200).json({
    status: 'success',
    results: users.length,
    data: {
      users,
    },
  });
});
