Refactored user.server.model.js

This commit is contained in:
David Baldwynn 2017-04-22 21:13:56 -07:00 committed by GitHub
parent 0944541247
commit 37984807bf

View file

@ -52,20 +52,12 @@ var UserSchema = new Schema({
firstName: { firstName: {
type: String, type: String,
trim: true, trim: true,
default: '', default: ''
/*validate: {
validator: validateLocalStrategyProperty,
message: 'Please fill in your first name'
}*/
}, },
lastName: { lastName: {
type: String, type: String,
trim: true, trim: true,
default: '', default: ''
/*validate: {
validator: validateLocalStrategyProperty,
message: 'Please fill in your last name'
}*/
}, },
email: { email: {
type: String, type: String,
@ -136,7 +128,7 @@ var UserSchema = new Schema({
unique: true, unique: true,
index: true, index: true,
sparse: true sparse: true
}, }
}); });
UserSchema.virtual('displayName').get(function () { UserSchema.virtual('displayName').get(function () {
@ -149,28 +141,6 @@ UserSchema.plugin(mUtilities.timestamp, {
useVirtual: false useVirtual: false
}); });
UserSchema.pre('save', function (next) {
//Create folder for user's pdfs
if(process.env.NODE_ENV === 'local-development'){
var newDestination = path.join(config.pdfUploadPath, this.username.replace(/ /g,'')),
stat = null;
try {
stat = fs.statSync(newDestination);
} catch (err) {
fs.mkdirSync(newDestination);
}
if (stat && !stat.isDirectory()) {
// console.log('Directory cannot be created');
next( new Error('Directory cannot be created because an inode of a different type exists at "' + newDestination + '"') );
}else{
next();
}
}
next();
});
/** /**
* Hook a pre save method to hash the password * Hook a pre save method to hash the password
*/ */
@ -217,13 +187,12 @@ UserSchema.statics.findUniqueUsername = function(username, suffix, callback) {
}, function(err, user) { }, function(err, user) {
if (!err) { if (!err) {
if (!user) { if (!user) {
callback(possibleUsername); return callback(possibleUsername);
} else { } else {
return _this.findUniqueUsername(username, (suffix || 0) + 1, callback); return _this.findUniqueUsername(username, (suffix || 0) + 1, callback);
} }
} else {
callback(null);
} }
return callback(null);
}); });
}; };