Skip to content

Commit 60430fb

Browse files
Finished POST /users route
1 parent 53fa350 commit 60430fb

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

src/models/user.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const mongoose = require('mongoose');
22
const bcrypt = require('bcrypt');
33
import isEmail from 'validator/lib/isEmail';
4+
45
const Schema = mongoose.Schema;
56

67
const UserSchema = new mongoose.Schema({
@@ -11,7 +12,7 @@ const UserSchema = new mongoose.Schema({
1112
trim: true,
1213
index: true
1314
},
14-
fullName:{
15+
fullName: {
1516
type: String,
1617
required: true,
1718
trim: true
@@ -23,40 +24,52 @@ const UserSchema = new mongoose.Schema({
2324
});
2425

2526
// authenticate input against database documents
26-
UserSchema.statics.authenticate = function(email, password, callback) {
27-
User.findOne({ emailAddress: email })
27+
UserSchema.statics.authenticate = function (email, password, callback) {
28+
User.findOne({emailAddress: email})
2829
.exec(function (error, user) {
2930
if (error) {
3031
return callback(error);
31-
} else if ( !user ) {
32+
} else if (!user) {
3233
let err = new Error('User not found.');
3334
err.status = 401;
3435
return callback(err);
3536
}
36-
bcrypt.compare(password, user.password , function(error, result) {
37+
bcrypt.compare(password, user.password, function (error, result) {
3738
if (result === true) {
3839
return callback(null, user);
3940
}
4041
return callback();
41-
42-
})
42+
});
4343
});
4444
};
4545

4646

47-
4847
/*
4948
May need some logic here later to check if email in correct format.
5049
*/
51-
UserSchema.statics.validEmail = function(email) {
50+
UserSchema.statics.validEmail = function (email) {
5251
email = email + '';
5352
return isEmail(email);
5453
};
5554

55+
UserSchema.statics.userExist = function (email, callback) {
56+
User.findOne({emailAddress: email})
57+
.exec(function (error, user) {
58+
if (error) {
59+
return callback(error);
60+
} else if (user) {
61+
const err = new Error('User Already Exists');
62+
err.status = 409;
63+
return callback(err);
64+
}
65+
return callback(null);
66+
});
67+
};
68+
5669

5770
//hash password before saving to database
5871
// hash password before saving to database
59-
UserSchema.pre('save', function(next){ //This has to be function. for some reason. tried without and it fails..
72+
UserSchema.pre('save', function (next) { //This has to be function. for some reason. tried without and it fails..
6073
const user = this;
6174
bcrypt.hash(user.password, 10, (err, hash) => {
6275
if (err) {

src/routes/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ router.post('/user', (req, res, next) => {
3939
err.status = 400;
4040
return next(err);
4141
}
42-
43-
const userData = req.body;
44-
User.create(userData, (err, user) => {
45-
if (err) {
42+
User.userExist(req.body.emailAddress, (err) => {
43+
if(err){
4644
return next(err);
4745
}
48-
return res.status(201).json({message: "User Successfully added!", status: 201, user});
46+
const userData = req.body;
47+
User.create(userData, (err, user) => {
48+
if (err) {
49+
return next(err);
50+
}
51+
res.location('/');
52+
return res.status(201).json({message: "User Successfully added!", status: 201, user});
53+
});
4954
});
50-
5155
});
5256

5357
module.exports = router;

test/user_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ describe('Users', () => {
5656
.post(postAPI)
5757
.send(user)
5858
.end((err, res) => {
59-
err.message.should.be.equal('test');
60-
res.should.have.status(500);
59+
res.body.should.have.property('message').equal("User Already Exists");
60+
res.should.have.status(409); //Conflict error
6161
done();
6262
});
6363
});

0 commit comments

Comments
 (0)