<commit_before>/**
 * Module dependencies
 */
/* eslint amd:true */

const nodemailer = require('nodemailer');
const mongoose = require('mongoose');

const Invitation = mongoose.model('Invitation');
require('dotenv').config({ silent: true });


const Invites = {
  emailinvite(req, res) {
    const gameLink = req.body.link;
    const email = req.body.email;
    const sender = req.body.sender;
    const link = `${gameLink}&email=${email}`;
    if (req.user && req.user.id) {
      const transporter = nodemailer.createTransport(process.env.EMAIL_SERVICES);
      const mailOptions = {
        from: process.env.EMAIL_OWNER,
        to: req.body.email,
        subject: 'Invitation to join Game',
        html: `<h3> Cards for Humanity </h3><br/>
        You have been invited by <strong>${sender}</strong> to join a game in cards for humanity<br/>
        click on this link <a href="${link}">here</a> to join the game now.<br/>
        <strong>Cards For Humanity</strong>`
      };
      transporter.sendMail(mailOptions, (error, info) => {
        if (error) return res.status(500).json({ status: 'error' });
        return res.status(200).json({ status: info.response });
      });
    } else {
      return res.status(403).json({ message: 'you do not have permission to access this route' });
    }
  },
  appMessage(req, res) {
    const gameLink = req.body.link;
    const email = req.body.email;
    const sender = req.body.sender;
    if (req.user && req.user.id) {
      const invite = new Invitation({
        to: email,
        userId: req.body.userId,
        from: sender,
        link: gameLink
      });
      invite.save((err) => {
        if (err) return res.status(500).json({ message: 'An error occured while sending the invite', error: err });
        return res.status(200).json({ message: 'Invite successfully sent' });
      });
    } else {
      return res.status(403).json({ message: 'You do not have permission to access this route' });
    }
  },
  getMessages(req, res) {
    if (req.user && req.user.id) {
      const userInfo = req.user.id;
      Invitation.find({ $and: [{ userId: userInfo }, { read: false }] }).sort({ _id: -1 }).limit(10)
      .exec((err, result) => {
        if (err) return res.status(500).json({ message: 'Can not retrieve messages at the moment', error: err });
        return res.status(200).json(result);
      });
    } else {
      return res.status(403).json({ message: 'You do not have permission to access this route' });
    }
  },
  viewMessage(req, res) {
    if (req.user && req.user.id) {
      const messageId = req.params.id;
      const query = { $and: [{ _id: messageId }, { read: false }] };
      Invitation.update(query, {
        read: true
      }, (err, result) => {
        if (err) return res.status(500).json({ message: 'An error occured while updating this data' });
        return res.status(200).json({ message: 'Message has been marked as read' });
      });
    } else {
      return res.status(403).json({ message: 'You do not have permission to access this route' });
    }
  }
};

module.exports = Invites;<commit_msg>feature(invite): add error message to be used in debugging by developers<commit_after>/**
 * Module dependencies
 */
/* eslint amd:true */

const nodemailer = require('nodemailer');
const mongoose = require('mongoose');

const Invitation = mongoose.model('Invitation');
require('dotenv').config({ silent: true });


const Invites = {
  emailinvite(req, res) {
    const gameLink = req.body.link;
    const email = req.body.email;
    const sender = req.body.sender;
    const link = `${gameLink}&email=${email}`;
    if (req.user && req.user.id) {
      const transporter = nodemailer.createTransport(process.env.EMAIL_SERVICES);
      const mailOptions = {
        from: process.env.EMAIL_OWNER,
        to: req.body.email,
        subject: 'Invitation to join Game',
        html: `<h3> Cards for Humanity </h3><br/>
        You have been invited by <strong>${sender}</strong> to join a game in cards for humanity<br/>
        click on this link <a href="${link}">here</a> to join the game now.<br/>
        <strong>Cards For Humanity</strong>`
      };
      transporter.sendMail(mailOptions, (err, info) => {
        if (err) return res.status(500).json({ message: 'An error occured while trying to send the message', error: err });
        return res.status(200).json({ status: info.response });
      });
    } else {
      return res.status(403).json({ message: 'you do not have permission to access this route' });
    }
  },
  appMessage(req, res) {
    const gameLink = req.body.link;
    const email = req.body.email;
    const sender = req.body.sender;
    if (req.user && req.user.id) {
      const invite = new Invitation({
        to: email,
        userId: req.body.userId,
        from: sender,
        link: gameLink
      });
      invite.save((err) => {
        if (err) return res.status(500).json({ message: 'An error occured while sending the invite', error: err });
        return res.status(200).json({ message: 'Invite successfully sent' });
      });
    } else {
      return res.status(403).json({ message: 'You do not have permission to access this route' });
    }
  },
  getMessages(req, res) {
    if (req.user && req.user.id) {
      const userInfo = req.user.id;
      Invitation.find({ $and: [{ userId: userInfo }, { read: false }] }).sort({ _id: -1 }).limit(10)
      .exec((err, result) => {
        if (err) return res.status(500).json({ message: 'Can not retrieve messages at the moment', error: err });
        return res.status(200).json(result);
      });
    } else {
      return res.status(403).json({ message: 'You do not have permission to access this route' });
    }
  },
  viewMessage(req, res) {
    if (req.user && req.user.id) {
      const messageId = req.params.id;
      const query = { $and: [{ _id: messageId }, { read: false }] };
      Invitation.update(query, {
        read: true
      }, (err, result) => {
        if (err) return res.status(500).json({ message: 'An error occured while updating this data', error: err });
        return res.status(200).json({ message: 'Message has been marked as read' });
      });
    } else {
      return res.status(403).json({ message: 'You do not have permission to access this route' });
    }
  }
};

module.exports = Invites;
{'fraction_non_alphanumeric': 0.1531062756228319, 'fraction_numerical': 0.012929675181330811, 'mean_word_length': 2.720234604105572, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2204761', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1942, 'n_words': 571}