<commit_before>const OfferPermission = require('../../models').offerPermission;
const User = require('../../models').user;

module.exports = function(req, res) {

  const { email, campaigns } = req.body;

  User.findOne({
    where: {
      email
    }
  })
  .then(userInstance => {
    if(!userInstance) {
      res.status(400).send({ message: 'This user does not exist' });
    } else {

      OfferPermission.findOne({
        where: {
          userId: req.user.id,
          toUserId: String(userInstance.getDataValue('id'))
        }
      })
      .then(opInstance => {

        if (opInstance) {
          res.status(400).send({ message: 'You have already granted this user permissions. If you wish to change them, please delete them first' });
        } else {
          OfferPermission.create({
            userId: req.user.id,
            toUserId: String(userInstance.getDataValue('id')),
            toUserEmail: email,
            campaigns
          })
          .then(() => res.send({ message: 'Your offer to grant this user these permissions has been sent' }))
          .catch(err => {
            throw err;
          });
        }

      })
      .catch(err => {
        throw err;
      });

    }
  })
  .catch(err => {
    throw err;
  });
};<commit_msg>Forbid offering a user permissions when existing permissions for this user exist<commit_after>const OfferPermission = require('../../models').offerPermission;
const ACL = require('../../models').acl;
const User = require('../../models').user;

module.exports = function(req, res) {

  const { email, campaigns } = req.body;

  User.findOne({
    where: {
      email
    }
  })
  .then(userInstance => {
    if(!userInstance) {
      res.status(400).send({ message: 'This user does not exist' });
    } else {

      ACL.findOne({
        where: {
          userId: String(req.user.id),
          toUserId: String(userInstance.getDataValue('id'))
        }
      })
      .then(aclInstance => {
        if (aclInstance) {
          // User has already been granted permissions
          res.status(400).send({ message: 'You have already granted this user permissions. If you wish to change them, please delete them first' });
        } else {

          OfferPermission.findOne({
            where: {
              userId: req.user.id,
              toUserId: String(userInstance.getDataValue('id'))
            }
          })
          .then(opInstance => {

            if (opInstance) {
              res.status(400).send({ message: 'You have already offered this user permissions. If you wish to change them, please delete them first' });
            } else {
              OfferPermission.create({
                userId: req.user.id,
                toUserId: String(userInstance.getDataValue('id')),
                toUserEmail: email,
                campaigns
              })
              .then(() => res.send({ message: 'Your offer to grant this user these permissions has been sent' }))
              .catch(err => {
                throw err;
              });
            }

          })
          .catch(err => {
            throw err;
          });

        }
      })
      .catch(err => {
        throw err;
      });

    }
  })
  .catch(err => {
    throw err;
  });
};
{'fraction_non_alphanumeric': 0.1361689106487148, 'fraction_numerical': 0.004589963280293758, 'mean_word_length': 1.7173732335827099, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5825221', 'n_tokens_mistral': 907, 'n_tokens_neox': 889, 'n_words': 224}