<commit_before>var Q = require('q');
var PassengerRoutes = require('./passengerRoutesModel');
var DriverRoutes = require('./driverRoutesModel');
var User = require('./../users/usersModel');

var findAllPassengerRoutes = Q.nbind(PassengerRoutes.find, PassengerRoutes);
var findPassengerRoutes = Q.nbind(PassengerRoutes.findOne, PassengerRoutes);
var createPassengerRoutes = Q.nbind(PassengerRoutes.create, PassengerRoutes);

var findAllDriverRoutes = Q.nbind(DriverRoutes.find, DriverRoutes);
var findDriverRoutes = Q.nbind(DriverRoutes.findOne, DriverRoutes);
var createDriverRoutes = Q.nbind(DriverRoutes.create, DriverRoutes);

var findUser = Q.nbind(User.findOne, User);

module.exports = {

  insertPassengerRoute: function(req, res, next){
    var passengerRoute = req.body.passengerRoute;
    var userId = req.body.userId;

    findUser({ _id: userId })
      .then(function(user){
        if(user.IsDriver){
          res.sendStatus(400);
        }

        createPassengerRoutes({
          start: passengerRoute.start,
          end: passengerRoute.end,
          days: passengerRoute.days,
          fromHour: passengerRoute.fromHour,
          fromMinutes: passengerRoute.fromMinutes,
          toHour: passengerRoute.toHour,
          toMinutes: passengerRoute.toMinutes
        })
        .then(function(newRoute){
          user.PassengerRoutes.push(newRoute);
          user.save();
          res.status(200).json(newRoute);
        });
      });
  },

  getPassengerRoutes: function(req, res, next) {
    findAllPassengerRoutes()
      .then(function(routes) {
        res.status(200).json(routes);
      })
      .fail(function(error) {
        next(error);
      });
  },

  insertDriverRoute: function(req, res, next){
    var driverRoute = req.body.driverRoute;
    var userId = req.body.userId;

    findUser({ _id: userId })
      .then(function(user){
        if(!user.IsDriver){
          res.sendStatus(400);
        }

        createDriverRoutes({
          start: driverRoute.start,
          end: driverRoute.end,
          days: driverRoute.days,
          fromHour: driverRoute.fromHour,
          fromMinutes: driverRoute.fromMinutes,
          toHour: driverRoute.toHour,
          toMinutes: driverRoute.toMinutes,
          seats: driverRoute.seats,
          fee: driverRoute.fee
        })
        .then(function(newRoute){
          user.DriverRoutes.push(newRoute);
          user.save();
          res.status(200).json(newRoute);<commit_msg>set IsDriver if we insert driver route<commit_after>var Q = require('q');
var PassengerRoutes = require('./passengerRoutesModel');
var DriverRoutes = require('./driverRoutesModel');
var User = require('./../users/usersModel');

var findAllPassengerRoutes = Q.nbind(PassengerRoutes.find, PassengerRoutes);
var findPassengerRoutes = Q.nbind(PassengerRoutes.findOne, PassengerRoutes);
var createPassengerRoutes = Q.nbind(PassengerRoutes.create, PassengerRoutes);

var findAllDriverRoutes = Q.nbind(DriverRoutes.find, DriverRoutes);
var findDriverRoutes = Q.nbind(DriverRoutes.findOne, DriverRoutes);
var createDriverRoutes = Q.nbind(DriverRoutes.create, DriverRoutes);

var findUser = Q.nbind(User.findOne, User);

module.exports = {

  insertPassengerRoute: function(req, res, next){
    var passengerRoute = req.body.passengerRoute;
    var userId = req.body.userId;

    findUser({ _id: userId })
      .then(function(user){
        if(user.IsDriver){
          res.sendStatus(400);
        }

        createPassengerRoutes({
          start: passengerRoute.start,
          end: passengerRoute.end,
          days: passengerRoute.days,
          fromHour: passengerRoute.fromHour,
          fromMinutes: passengerRoute.fromMinutes,
          toHour: passengerRoute.toHour,
          toMinutes: passengerRoute.toMinutes
        })
        .then(function(newRoute){
          user.PassengerRoutes.push(newRoute);
          user.save();
          res.status(200).json(newRoute);
        });
      });
  },

  getPassengerRoutes: function(req, res, next) {
    findAllPassengerRoutes()
      .then(function(routes) {
        res.status(200).json(routes);
      })
      .fail(function(error) {
        next(error);
      });
  },

  insertDriverRoute: function(req, res, next){
    var driverRoute = req.body.driverRoute;
    var userId = req.body.userId;

    findUser({ _id: userId })
      .then(function(user){
        createDriverRoutes({
          start: driverRoute.start,
          end: driverRoute.end,
          days: driverRoute.days,
          fromHour: driverRoute.fromHour,
          fromMinutes: driverRoute.fromMinutes,
          toHour: driverRoute.toHour,
          toMinutes: driverRoute.toMinutes,
          seats: driverRoute.seats,
          fee: driverRoute.fee
        })
        .then(function(newRoute){
          user.IsDriver = true;
          user.DriverRoutes.push(newRoute);
          user.save();
          res.status(200).json(newRoute);
{'fraction_non_alphanumeric': 0.11784920153628461, 'fraction_numerical': 0.005457853244390539, 'mean_word_length': 2.774218154080854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5098044', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1509, 'n_words': 253}