text: import * as Yup from 'yup';
  import Recipient from '../models/Recipient';
  
  class RecipientController {
    async index(req, res) {
      const recipients = await Recipient.findAll({
        attributes: [
          'id',
          'name',
          'street',
          'number',
          'complement',
          'state',
          'city',
          'cep',
        ],
      });
      return res.json(recipients);
    }
  
    async findOne(req, res) {
      const { id } = req.params;
  
      const recipients = await Recipient.findAll({
        where: {
          id,
        },
        attributes: [
          'id',
          'name',
          'street',
          'number',
          'complement',
          'state',
          'city',
          'cep',
        ],
      });
      return res.json(recipients);
    }
  
    async store(req, res) {
      const schema = Yup.object().shape({
        name: Yup.string().required(),
        street: Yup.string().required(),
        number: Yup.number()
          .positive()
          .required(),
        complement: Yup.string(),
        state: Yup.string().required(),
        city: Yup.string().required(),
        cep: Yup.number()
          .positive()
          .required(),
      });
  
      if (!(await schema.isValid(req.body))) {
        return res.status(400).json({ error: 'Validation fails' });
      }
  
      const recipientExists = await Recipient.findOne({
        where: { name: req.body.name },
      });
  
      if (recipientExists) {
        return res.status(400).json({ error: 'Recipient already exists.' });
      }
      const {
        id,
        name,
        street,
        number,
        complement,
        state,
        city,
        cep,
      } = await Recipient.create(req.body);
      return res.json({
        id,
        name,
        street,
        number,
        complement,
        state,
        city,
        cep,
      });
    }
  
    async update(req, res) {
      const schema = Yup.object().shape({
        name: Yup.string(),
        street: Yup.string(),
        number: Yup.number().positive(),
        complement: Yup.string(),
        state: Yup.string(),
        city: Yup.string(),
        cep: Yup.number().positive(),
      });
  
      if (!(await schema.isValid(req.body))) {
        return res.status(400).json({ error: 'Validation fails' });
      }
      const { name } = req.body;
  
      const recipient = await Recipient.findByPk(req.params.id);
  
      if (name !== recipient.name) {
        const recipientExistis = await Recipient.findOne({
          where: { name },
        });
  
        if (recipientExistis) {
          return res.status(400).json({ error: 'Recipient already exists.' });
        }
      }
  
      const {
        id,
        street,
        number,
        complement,
        state,
        city,
        cep,
      } = await recipient.update(req.body);
  
      return res.json({
        id,
        name,
        street,
        number,
        complement,
        state,
        city,
        cep,
      });
    }
  
    async delete(req, res) {
      const recipient = await Recipient.findByPk(req.params.id);
  
      if (!recipient) {
        return res.status(404).json({ error: 'This recipient does not exists' });
      }
  
      await Recipient.destroy({ where: { id: recipient.id } });
  
      return res.json({ message: 'successfully deleted!' });
    }
  }
  export default new RecipientController();
  