text: import { Op } from 'sequelize';
  import User from '../models/User';
  import File from '../models/File';
  import Meetup from '../models/Meetup';
  import Subscription from '../models/Subscription';
  import Queue from '../../lib/Queue';
  import SubscriptionMail from '../jobs/SubscriptionMail';
  
  class SubscriptionController {
    async index(req, res) {
      const subscriptions = await Subscription.findAll({
        where: {
          user_id: req.userId,
        },
        attributes: ['id'],
        include: [
          {
            model: Meetup,
            where: {
              date: {
                [Op.gt]: new Date(),
              },
            },
            attributes: ['id', 'title', 'description', 'location', 'date'],
            required: true,
            include: [
              {
                model: User,
                as: 'user',
                attributes: ['name', 'email'],
              },
              {
                model: File,
                as: 'imagem',
                attributes: ['id', 'url', 'name', 'path'],
              },
            ],
          },
        ],
        order: [[Meetup, 'date']],
      });
  
      return res.json(subscriptions);
    }
  
    async store(req, res) {
      const user = await User.findByPk(req.userId);
      const meetup = await Meetup.findByPk(req.params.meetupId, {
        include: {
          model: User,
          as: 'user',
          attributes: ['id', 'name', 'email'],
        },
      });
  
      if (meetup.user_id === req.userId) {
        return res
          .status(400)
          .json({ error: "Can't subscribe to you own meetups" });
      }
  
      if (meetup.past) {
        return res.status(400).json({ error: "Can't subscribe to past meetups" });
      }
  
      const checkDate = await Subscription.findOne({
        where: {
          user_id: user.id,
        },
        include: [
          {
            model: Meetup,
            required: true,
            where: {
              date: meetup.date,
            },
          },
        ],
      });
  
      if (checkDate) {
        return res
          .status(400)
          .json({ error: "Can't subscribe to two meetups at the same time" });
      }
  
      const subscription = await Subscription.create({
        user_id: user.id,
        meetup_id: meetup.id,
      });
  
      await Queue.add(SubscriptionMail.key, {
        meetup,
        user,
      });
  
      return res.json(subscription);
    }
  
    async delete(req, res) {
      const subscription = await Subscription.findByPk(req.params.id, {
        include: [
          {
            model: User,
            attributes: ['id'],
          },
        ],
      });
  
      if (subscription.User.id !== req.userId) {
        return res.status(401).json({
          error: "You don't have permission to cancel this subscription",
        });
      }
  
      await subscription.destroy();
  
      return res.json(subscription);
    }
  }
  
  export default new SubscriptionController();
  