Skip to content

Commit

Permalink
Merge branch 'feature/isolate-odm-validation-error' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
gocreating committed Oct 14, 2016
2 parents 4eff5b5 + 499060d commit f52d11d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
3 changes: 2 additions & 1 deletion specs/endToEnd/apis/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ describe('#user', () => {
expect(err).to.equal(null);
expect(res).to.not.be.undefined;
expect(res.status).to.equal(200);
expect(res.body.errors[0].code).to.equal(Errors.USER_CONFLICT.code);
expect(res.body.errors[0].code)
.to.equal(Errors.ODM_VALIDATION.code);
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/common/constants/ErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export default {
PERMISSION_DENIED: 'PERMISSION_DENIED',
LOCALE_NOT_SUPPORTED: 'LOCALE_NOT_SUPPORTED',
USER_TOKEN_EXPIRATION: 'USER_TOKEN_EXPIRATION',
USER_CONFLICT: 'USER_CONFLICT',
ODM_VALIDATION: 'ODM_VALIDATION',
};
14 changes: 7 additions & 7 deletions src/common/constants/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ export default {
},
[ErrorCodes.ODM_OPERATION_FAIL]: {
code: ErrorCodes.ODM_OPERATION_FAIL,
status: 400,
status: 500,
title: 'Database Operation Failed',
detail: 'Current database operation is invalid.',
},
[ErrorCodes.ODM_VALIDATION]: {
code: ErrorCodes.ODM_VALIDATION,
status: 400,
title: 'Database Validation Failed',
detail: 'The data is invalid.',
},
[ErrorCodes.STATE_PRE_FETCHING_FAIL]: {
code: ErrorCodes.STATE_PRE_FETCHING_FAIL,
status: 500,
Expand Down Expand Up @@ -43,10 +49,4 @@ export default {
title: 'Token Expired',
detail: 'Your jwt token expired. Please re-login.',
},
[ErrorCodes.USER_CONFLICT]: {
code: ErrorCodes.USER_CONFLICT,
status: 400,
title: 'User Already Exists',
detail: 'The user already exists.',
},
};
32 changes: 11 additions & 21 deletions src/server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import Errors from '../../common/constants/Errors';
import { handleDbError } from '../decorators/handleError';
import User from '../models/User';
import filterAttribute from '../utils/filterAttribute';

export default {
create(req, res) {
User.findOne({
'email.value': req.body.email,
}, handleDbError(res)((user) => {
if (user !== null) {
res.errors([Errors.USER_CONFLICT]);
} else {
const user = User({
name: req.body.name,
email: {
value: req.body.email,
},
password: req.body.password,
});

user.save(handleDbError(res)((user) => {
res.json({
user: user,
});
}));
}
const user = User({
name: req.body.name,
email: {
value: req.body.email,
},
password: req.body.password,
});
user.save(handleDbError(res)((user) => {
res.json({
user: user,
});
}));
},

Expand Down
7 changes: 6 additions & 1 deletion src/server/decorators/handleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ let getErrorHandler = (ErrorType) => (res) => (fn) => (err, ...result) => {
if (err) {
switch (ErrorType) {
case ErrorTypes.ODM_OPERATION: {
res.pushError(Errors.ODM_OPERATION_FAIL, err);
if (err.name === 'ValidationError') {
res.pushError(Errors.ODM_VALIDATION, err);
res.errors();
} else {
res.pushError(Errors.ODM_OPERATION_FAIL, err);
}
break;
}
default: {
Expand Down
17 changes: 12 additions & 5 deletions src/server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const hashPassword = (rawPassword = '') => {
return rawPassword;
};

let User = new mongoose.Schema({
let UserSchema = new mongoose.Schema({
name: String,
email: {
value: {
Expand All @@ -41,12 +41,18 @@ let User = new mongoose.Schema({
},
});

User.methods.auth = function(password, cb) {
UserSchema.path('email.value').validate(function(value, cb) {
User.findOne({ 'email.value': value }, (err, user) => {
cb(!err && !user);
});
}, 'This email address is already registered');

UserSchema.methods.auth = function(password, cb) {
const isAuthenticated = (this.password === hashPassword(password));
cb(null, isAuthenticated);
};

User.methods.toJwtToken = function(cb) {
UserSchema.methods.toJwtToken = function(cb) {
const user = {
_id: this._id,
name: this.name,
Expand All @@ -58,10 +64,11 @@ User.methods.toJwtToken = function(cb) {
return token;
};

User.methods.toJSON = function() {
UserSchema.methods.toJSON = function() {
let obj = this.toObject();
delete obj.password;
return obj;
};

export default mongoose.model('User', User);
let User = mongoose.model('User', UserSchema);
export default User;

0 comments on commit f52d11d

Please sign in to comment.