text: import * as Yup from 'yup';
  import User from '../models/User';
  import File from '../models/File';
  
  class UserController {
    async store(req, res) {
      const schema = Yup.object().shape({
        name: Yup.string().required(),
        email: Yup.string()
          .email()
          .required(),
        password: Yup.string()
          .required()
          .min(6),
      });
  
      if (!(await schema.isValid(req.body))) {
        return res.status(400).json({ error: 'Validation fails' });
      }
  
      const userExists = await User.findOne({ where: { email: req.body.email } });
  
      if (userExists) {
        return res
          .status(401)
          .json({ error: `User ${req.body.email} already exists.` });
      }
      try {
        const { id, name, email, provider } = await User.create(req.body);
        return res.json({ id, name, email, provider });
      } catch (err) {
        // return res.json(err);
        return res.status(401).json({ error: 'User was not stored' });
      }
    }
  
    async update(req, res) {
      const schema = Yup.object().shape({
        name: Yup.string(),
        email: Yup.string().email(),
        oldPassword: Yup.string().min(6),
        password: Yup.string()
          .min(6)
          .when('oldPassword', (oldPassword, field) =>
            oldPassword ? field.required() : field
          ),
        confirmPassword: Yup.string().when('password', (password, field) =>
          password ? field.required().oneOf([Yup.ref('password')]) : field
        ),
      });
  
      if (!(await schema.isValid(req.body))) {
        return res.status(400).json({ error: 'Validation fails' });
      }
  
      const { email, oldPassword } = req.body;
  
      const user = await User.findByPk(req.userId);
  
      if (email !== user.email) {
        const userExists = await User.findOne({ where: { email } });
  
        if (userExists) {
          return res.status(400).json({ error: 'user already exists.' });
        }
      }
  
      if (oldPassword && !(await user.checkPassword(oldPassword))) {
        return res.status(401).json({ error: 'Password does not match.' });
      }
  
      try {
        await user.update(req.body);
        const { id, name, avatar } = await User.findByPk(req.userId, {
          include: [
            {
              model: File,
              as: 'avatar',
              attributes: ['id', 'path', 'url'],
            },
          ],
        });
        return res.json({ id, name, email, avatar });
      } catch (err) {
        // return res.json(err);
        return res.status(401).json({ error: 'User was not updated' });
      }
    }
  }
  export default new UserController();
  