Skip to content

Commit

Permalink
Fix Nex accounts save error
Browse files Browse the repository at this point in the history
  • Loading branch information
hvpulok committed Jul 25, 2018
1 parent 19fe288 commit ed962c7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.create = function (req, res) {
var encryptedAccount = myCrypto.encryptObject(req.body.account, req.user._id.toString().substr(0, 3)); // server encrypt the account object data
var account = new Account({
title: req.body.title,
category : req.body.category,
category: req.body.category,
account: encryptedAccount
});
account.author.username = req.user.username;
Expand All @@ -40,11 +40,11 @@ exports.create = function (req, res) {
selectedUser.accounts.push(savedAccount);
selectedUser.save(function (err) {
if (err) {
console.error(err);
return res.status(501).send({
message: errorHandler.getErrorMessage(err)
});
}
else {
} else {
res.jsonp(account);
}
});
Expand All @@ -68,7 +68,9 @@ exports.read = function (req, res) {
}
var data = myCrypto.decryptObject(account.account, req.user._id.toString().substr(0, 3));
// delete account.account;
Object.assign(account, { account: data });
Object.assign(account, {
account: data
});
// account.account = data;
res.jsonp(account);
};
Expand All @@ -86,7 +88,9 @@ exports.update = function (req, res) {
message: 'Unauthorized'
});
}
account = _.extend(account, { account: encryptedAccount });
account = _.extend(account, {
account: encryptedAccount
});
account.title = req.body.title;
account.category = req.body.category;
account.save(function (err) {
Expand Down
63 changes: 43 additions & 20 deletions modules/accounts/server/models/account.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@ var commentSchema = new mongoose.Schema({
},
username: String
},
commentDate: { type: Date, default: Date.now }
commentDate: {
type: Date,
default: Date.now
}
});


/**
* Account Schema
*/
var AccountSchema = new Schema({
title: { type: String, required: [true, 'Need to have valid title'] },
account: { type: String, required: [true, 'Need to have valid account data'] },
category: { type: String},
views : {
lastViewed : { type: Date, default: Date.now },
viewCount: { type: Number, default: 1 }
title: {
type: String,
required: [true, 'Need to have valid title']
},
account: {
type: String,
required: [true, 'Need to have valid account data']
},
category: {
type: String
},
views: {
lastViewed: {
type: Date,
default: Date.now
},
viewCount: {
type: Number,
default: 1
}
},
author: {
id: {
Expand All @@ -38,25 +55,31 @@ var AccountSchema = new Schema({
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}
]
}, { timestamps : true });
comments: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}]
}, {
timestamps: true,
usePushEach: true
});

/**
* Hook a pre remove method to delete related accounts/data/info in user Schema
*/
AccountSchema.post('remove', function(next){
AccountSchema.post('remove', function (next) {
// Remove all the accounts docs that reference the removed user.
// this.model('User').remove({ 'accounts': this._id }, next);
this.model('User').update({ 'accounts': this._id },
{ $pull: { 'accounts':this._id } },
function (err,val) {
console.log(val);
});
this.model('User').update({
'accounts': this._id
}, {
$pull: {
'accounts': this._id
}
},
function (err, val) {
console.log(val);
});
});

module.exports = mongoose.model('Account', AccountSchema);
2 changes: 2 additions & 0 deletions modules/articles/server/models/article.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var ArticleSchema = new Schema({
type: Schema.ObjectId,
ref: 'User'
}
}, {
usePushEach: true
});

mongoose.model('Article', ArticleSchema);
27 changes: 18 additions & 9 deletions modules/users/server/models/user.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var validateLocalStrategyProperty = function (property) {
* A Validation function for local strategy email
*/
var validateLocalStrategyEmail = function (email) {
return ((this.provider !== 'local' && !this.updated) || validator.isEmail(email, { require_tld: false }));
return ((this.provider !== 'local' && !this.updated) || validator.isEmail(email, {
require_tld: false
}));
};

/**
Expand All @@ -39,7 +41,7 @@ var validateLocalStrategyEmail = function (email) {
* - not begin or end with "."
*/

var validateUsername = function(username) {
var validateUsername = function (username) {
var usernameRegex = /^(?=[\w.-]+$)(?!.*[._-]{2})(?!\.)(?!.*\.$).{3,34}$/;
return (
this.provider !== 'local' ||
Expand Down Expand Up @@ -125,7 +127,12 @@ var UserSchema = new Schema({
resetPasswordExpires: {
type: Date
},
accounts: [{ type: Schema.Types.ObjectId, ref: 'Account' }]
accounts: [{
type: Schema.Types.ObjectId,
ref: 'Account'
}]
}, {
usePushEach: true
});

/**
Expand All @@ -143,9 +150,11 @@ UserSchema.pre('save', function (next) {
/**
* Hook a pre remove method to delete related accounts/data/info
*/
UserSchema.pre('remove', function(next){
UserSchema.pre('remove', function (next) {
// Remove all the accounts docs that reference the removed user.
this.model('Account').remove({ 'author.id': this._id }, next);
this.model('Account').remove({
'author.id': this._id
}, next);
});

/**
Expand Down Expand Up @@ -204,10 +213,10 @@ UserSchema.statics.findUniqueUsername = function (username, suffix, callback) {
};

/**
* Generates a random passphrase that passes the owasp test
* Returns a promise that resolves with the generated passphrase, or rejects with an error if something goes wrong.
* NOTE: Passphrases are only tested against the required owasp strength tests, and not the optional tests.
*/
* Generates a random passphrase that passes the owasp test
* Returns a promise that resolves with the generated passphrase, or rejects with an error if something goes wrong.
* NOTE: Passphrases are only tested against the required owasp strength tests, and not the optional tests.
*/
UserSchema.statics.generateRandomPassphrase = function () {
return new Promise(function (resolve, reject) {
var password = '';
Expand Down

0 comments on commit ed962c7

Please sign in to comment.