<commit_before>const bodyParser = require('body-parser').json;
const express = require('express');

const User = require('../models/user');

const Router = express.Router;
const Users = Router();

function hasEmailAndPassword(req, res, next) {
  const signee = req.body;

  if (!signee.email || !signee.password) {
    return next({
      code: 400,
      error: 'Email and password must be provided',
    });
  }
  next();
}

Users
  .get('/', (req, res) => {
    res.send('helloooo world');
  })

  .post('/signup', bodyParser, hasEmailAndPassword, (req, res, next) => {
    let data = req.body;
    let userObj = {};

    User.find({ email: data.email }).count()
      .then((count) => {
        if (count > 0) {
          throw new Error({
            code: 400,
            error: `The email you have entered, ${data.email}, already has an account. Please signup with a different email.`,
          });
        }
        return new User(data).save();
      })
      .then((user) => {
        userObj = user;
      })
      .then((userObject) => {
        res.send({ userObject });
      })
      .catch(next);
  })

  ; // end Users routes

module.exports = Users;<commit_msg>continues work on User POST route - new way to throw error implemented<commit_after>const bodyParser = require('body-parser').json;
const express = require('express');

const User = require('../models/user');

const Router = express.Router;
const Users = Router();

function hasEmailAndPassword(req, res, next) {

  if (!req.body.email || !req.body.password) {
    return next({
      code: 400,
      error: 'Email and password must be provided',
    });
  }
  return next();
}

Users
  .get('/', (req, res) => {
    res.send('helloooo world');
  })

  .post('/signup', bodyParser, hasEmailAndPassword, (req, res, next) => {
    const data = req.body;

    User.find({ email: data.email }).count()
      .then((count) => {
        if (count > 0) {
          const badEmail = new Error(
            `The email you have entered, ${data.email}, already has an account. Please signup with a different email.`,
          );
          badEmail.code = 400;
          throw badEmail;
        }
        return new User(data).save();
      })
      .then((user) => {
        res.json({ user });
      })
      .catch(next);
  })

  ; // end Users routes

module.exports = Users;
{'fraction_non_alphanumeric': 0.16042553191489362, 'fraction_numerical': 0.005957446808510639, 'mean_word_length': 2.353780313837375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '476959', 'n_tokens_mistral': 739, 'n_tokens_neox': 711, 'n_words': 203}