Skip to content

Instantly share code, notes, and snippets.

@velitchko
Created July 20, 2018 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velitchko/3a5479f848c5d27653ddc46454616352 to your computer and use it in GitHub Desktop.
Save velitchko/3a5479f848c5d27653ddc46454616352 to your computer and use it in GitHub Desktop.
function addNotifyToAllAccounts(input, sender) {
console.log("Trying to find user");
User.find({ username: input[1] }, function(err, userdb) {
if(!err && userdb){
console.log("Found user: " + userdb[0].username);
let newNotification = '';
// rebuilding message
for(var i = 2; i < input.length; i++){
newNotification += input[i] + " ";
}
userdb.forEach(function(user){
user.notification.hasNotification = true;
let notification = {
text: newNotification,
date: new Date(),
fromUser: await User.findOne({ username: sender }, function(err, senderFound) {
if(err) console.log(err);
else console.log(`Found sender: ${senderFound.name} (id: ${senderFound._id})`);
})
}
user.notification.notifications.push(notification);
user.save(function(err, newUser) {
if(err) console.log(err)
else console.log(`Notification updates for ${newUser.username} (id: ${newUser._id})`);
});
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment