<commit_before>'use strict';

const debug = require('debug')('ways2go:way');
const createError = require('http-errors');
const jsonParser = require('body-parser').json();
const Promise = require('bluebird');
const mongoose = require('mongoose');
mongoose.Promise = Promise;

const parseLocationGoogle = require('../lib/parse-location-google.js');
const bearerAuth = require('../lib/bearer-auth-middleware.js');
const Way = require('../model/way.js');
const Profile = require('../model/profile.js');
const Location = require('../model/location.js');

const wayRouter = module.exports = require('express').Router();

wayRouter.post('/api/way', bearerAuth, jsonParser, function(req, res, next) {
  debug('POST: /api/way');

  if(!req.body.startLocation) return next(createError(400, 'start location required'));
  if(!req.body.endLocation) return next(createError(400, 'end location required'));

  let promStart = parseLocationGoogle(req.body.startLocation)
  .then( geolocation => new Location(geolocation).save())
  .then( location => {req.body.startLocationID = location._id;} )
  .catch(next);

  let promEnd = parseLocationGoogle(req.body.endLocation)
  .then( geolocation => new Location(geolocation).save())
  .then( location => {req.body.endLocationID = location._id;} )
  .catch(next);

  let promProfile = Profile.findOne({ userID: req.user._id })
  .then ( profile => {
    req.body.profileID = profile._id;
  })
  .catch(next);

  Promise.all([ promStart, promEnd, promProfile ])
  .then( () => {
    console.log('in promise all body', req.body);
    new Way(req.body).save()
    .then( way => {
      way.wayerz.push(way.profileID);
      return way.save();
    })
    .then( way => res.json(way))
    .catch(next);
  });
});

wayRouter.post('/api/way/:wayID/wayerz/:wayerID', bearerAuth, function(req, res, next) {
  debug('POST: /api/way/:wayID/wayerz/:wayerID');

  let tempWayerProfile, tempWay;

  Profile.findById(req.params.wayerID)
  .then( profile => {
    tempWayerProfile = profile;
    return Way.findById(req.params.wayID);
  })
  .then( way => {
    tempWay = way;
    return Profile.findOne({ userID: req.user._id.toString() });<commit_msg>code cleanup<commit_after>'use strict';

const debug = require('debug')('ways2go:way');
const createError = require('http-errors');
const jsonParser = require('body-parser').json();
const Promise = require('bluebird');
const mongoose = require('mongoose');
mongoose.Promise = Promise;

const parseLocationGoogle = require('../lib/parse-location-google.js');
const bearerAuth = require('../lib/bearer-auth-middleware.js');
const Way = require('../model/way.js');
const Profile = require('../model/profile.js');
const Location = require('../model/location.js');

const wayRouter = module.exports = require('express').Router();

wayRouter.post('/api/way', bearerAuth, jsonParser, function(req, res, next) {
  debug('POST: /api/way');

  if(!req.body.startLocation) return next(createError(400, 'start location required'));
  if(!req.body.endLocation) return next(createError(400, 'end location required'));

  let promStart = parseLocationGoogle(req.body.startLocation)
  .then( geolocation => new Location(geolocation).save())
  .then( location => {req.body.startLocationID = location._id;} )
  .catch(next);

  let promEnd = parseLocationGoogle(req.body.endLocation)
  .then( geolocation => new Location(geolocation).save())
  .then( location => {req.body.endLocationID = location._id;} )
  .catch(next);

  let promProfile = Profile.findOne({ userID: req.user._id })
  .then ( profile => {
    req.body.profileID = profile._id;
  })
  .catch(next);

  Promise.all([ promStart, promEnd, promProfile ])
  .then( () => {
    new Way(req.body).save()
    .then( way => {
      way.wayerz.push(way.profileID);
      return way.save();
    })
    .then( way => res.json(way))
    .catch(next);
  });
});

wayRouter.post('/api/way/:wayID/wayerz/:wayerID', bearerAuth, function(req, res, next) {
  debug('POST: /api/way/:wayID/wayerz/:wayerID');

  let tempWayerProfile, tempWay;

  Profile.findById(req.params.wayerID)
  .then( profile => {
    tempWayerProfile = profile;
    return Way.findById(req.params.wayID);
  })
  .then( way => {
    tempWay = way;
    return Profile.findOne({ userID: req.user._id.toString() });
{'fraction_non_alphanumeric': 0.17986451763606634, 'fraction_numerical': 0.003270263957019388, 'mean_word_length': 4.279901356350185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4511216', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1396, 'n_words': 272}