<commit_before>const {Users, Groups} = require('../models/users')
const mongoose = require('mongoose')

module.exports = function(router) {
  router.post('/group-invite/accept/:id', (req, res) => {
    Groups.findOne({_id: req.params.id})
    .then(group => {
      //Check if the user is already in the group
      let isUserAlreadyInGroup = group.users.find(id => id.equals(req.user._id))
      let userContainsGroup = req.user.groups.find(id => id.equals(group._id))
      if(!group) throw 'Group does not exist'
      if(userContainsGroup && !isUserAlreadyInGroup) throw new databaseException('User contains group, but group does not contain the user')
      if(isUserAlreadyInGroup && !userContainsGroup) throw new databaseException('Group contains the user, but the user does not contain the group')
      if(isUserAlreadyInGroup && userContainsGroup) throw 'User is already inside the group'
      group.users.push(req.user._id)
      group.save()
      //remove invite
      req.user.invites.remove(req.params.id)
      req.user.groups.push(group._id)
      return req.user.save()
    })
    .then(() => res.redirect(`/group/${req.params.id}`))
    .catch(err => {
      if(err == 'User is already inside the group') {
        req.user.invites.remove(req.params.id)
        req.user.save()
        return res.redirect('/dashboard')
      }else if(err.msg == 'Group contains the user, but the user does not contain the group') {
        //Group has user use doesn't have the group
        //save the group to the user
        req.user.groups.push(req.params.id)
        req.user.save()
        res.redirect(`/group/${req.params.id}`)
      }else if(err.msg == 'User contains group, but group does not contain the user') {
        //The user contains the group, but the group does not have the user
        //save the user to the group then redirect to the group
        Groups.update({_id: req.params.id}, {$push: {users: req.user._id}})
        .then(data => {
          req.user.invites.remove(req.params.id)
          req.user.save()
          res.redirect(`/group/${req.params.id}`)
        })
      }else if(err == 'Group does not exist'){
        req.user.invites.remove(req.params.id)
        req.user.save()
        res.redirect('/dashboard')
      }else {
        console.log(err)
        res.render('error')
      }
    })
  })

  router.post('/group-invite/decline/:id', (req, res) => {
    //Remove the invite
    req.user.invites.remove(req.params.id)
    req.user.save()
    res.redirect('/dashboard')
  })

  router.post('/borrow-request/accept/:bookId/:requestId/:borrowerId', (req, res) => {
    let findBorrower = Users.findOne({_id: req.params.borrowerId})
    //Remove the request
    let updateOwner = Users.findOneAndUpdate({_id: req.user._id}, {$pull: {borrowRequests: {_id: req.params.requestId}}},{safe: true, multi:true}).exec()
    .then(user => user)
    Promise.all([findBorrower, updateOwner])
    .then( data => {
      let borrower = data[0]
      let book = data[1].books.id(req.params.bookId)
      if(!book) throw 'Book does not exist'
      if(!borrower) throw 'Borrower does not exist'
      if(book.borrower) throw 'Book is already being borrowed'
      //Update book
      book.borrower = borrower.username
      data[1].save()
      //Add book to borrower
      borrower.borrowedBooks.push(book)
      borrower.save()
      res.redirect('/dashboard')
    })
    .catch(err => {
      if(err == 'Book does not exist' || err == 'Borrower does not exist' || err == 'Book is already being borrowed') {
        res.redirect('/dashboard')
      }else {
        console.log(err)
        res.render('error')<commit_msg>bug fix<commit_after>const {Users, Groups} = require('../models/users')
const mongoose = require('mongoose')

module.exports = function(router) {
  router.post('/group-invite/accept/:id', (req, res) => {
    Groups.findOne({_id: req.params.id})
    .then(group => {
      //Check if the user is already in the group
      let isUserAlreadyInGroup = group.users.find(id => id.equals(req.user._id))
      let userContainsGroup = req.user.groups.find(id => id.equals(group._id))
      if(!group) throw 'Group does not exist'
      if(userContainsGroup && !isUserAlreadyInGroup) throw new databaseException('User contains group, but group does not contain the user')
      if(isUserAlreadyInGroup && !userContainsGroup) throw new databaseException('Group contains the user, but the user does not contain the group')
      if(isUserAlreadyInGroup && userContainsGroup) throw 'User is already inside the group'
      group.users.push(req.user._id)
      group.save()
      //remove invite
      req.user.invites.remove(req.params.id)
      req.user.groups.push(group._id)
      return req.user.save()
    })
    .then(() => res.redirect(`/group/${req.params.id}`))
    .catch(err => {
      if(err == 'User is already inside the group') {
        req.user.invites.remove(req.params.id)
        req.user.save()
        return res.redirect('/dashboard')
      }else if(err.msg == 'Group contains the user, but the user does not contain the group') {
        //Group has user use doesn't have the group
        //save the group to the user
        req.user.groups.push(req.params.id)
        req.user.save()
        res.redirect(`/group/${req.params.id}`)
      }else if(err.msg == 'User contains group, but group does not contain the user') {
        //The user contains the group, but the group does not have the user
        //save the user to the group then redirect to the group
        Groups.update({_id: req.params.id}, {$push: {users: req.user._id}})
        .then(data => {
          req.user.invites.remove(req.params.id)
          req.user.save()
          res.redirect(`/group/${req.params.id}`)
        })
      }else if(err == 'Group does not exist'){
        req.user.invites.remove(req.params.id)
        req.user.save()
        res.redirect('/dashboard')
      }else {
        console.log(err)
        res.render('error')
      }
    })
  })

  router.post('/group-invite/decline/:id', (req, res) => {
    //Remove the invite
    req.user.invites.remove(req.params.id)
    req.user.save()
    res.redirect('/dashboard')
  })

  router.post('/borrow-request/accept/:bookId/:requestId/:borrowerId', (req, res) => {
    let findBorrower = Users.findOne({_id: req.params.borrowerId})
    //Remove the request
    let updateOwner = Users.findOneAndUpdate({_id: req.user._id}, {$pull: {borrowRequests: {_id: req.params.requestId}}},{safe: true, multi:true}).exec()
    .then(user => user)
    Promise.all([findBorrower, updateOwner])
    .then( data => {
      let borrower = data[0]
      let book = data[1].books.id(req.params.bookId)
      if(!book) throw 'Book does not exist'
      if(!borrower) throw 'Borrower does not exist'
      if(book.borrower) throw 'Book is already being borrowed'
      //Update book
      book.borrower = borrower.username
      data[1].save()
      //Add book to borrower
      borrower.borrowedBooks.push(book._id)
      return borrower.save()
    })
    .then(() => {
      res.redirect('/dashboard')
    })
    .catch(err => {
      if(err == 'Book does not exist' || err == 'Borrower does not exist' || err == 'Book is already being borrowed') {
        console.log(err)
        res.redirect('/dashboard')
      }else {
        console.log(err)
        res.render('error')
{'fraction_non_alphanumeric': 0.14801248134581468, 'fraction_numerical': 0.000814000814000814, 'mean_word_length': 2.6878439219609804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, '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': '2240484', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2305, 'n_words': 563}