text: const { UnauthorizedError } = require("../helper/customErrors");
  const { bcryptHash } = require("../helper/bcrypt");
  
  //* Current User
  const currentUser = async (req, res, next) => {
    try {
      const { loggedUser } = req;
      if (!loggedUser) throw new UnauthorizedError();
  
      loggedUser.dataValues.email = req.headers.email;
      delete req.headers.email;
  
      res.json({ user: loggedUser });
    } catch (error) {
      next(error);
    }
  };
  
  //* Update User
  const updateUser = async (req, res, next) => {
    try {
      const { loggedUser } = req;
      if (!loggedUser) throw new UnauthorizedError();
  
      const {
        user: { password },
        user,
      } = req.body;
  
      Object.entries(user).forEach((entry) => {
        const [key, value] = entry;
  
        if (value !== undefined && key !== "password") loggedUser[key] = value;
      });
  
      if (password !== undefined || password !== "") {
        loggedUser.password = await bcryptHash(password);
      }
  
      await loggedUser.save();
  
      res.json({ user: loggedUser });
    } catch (error) {
      next(error);
    }
  };
  
  module.exports = { currentUser, updateUser };
  