Created
July 20, 2018 08:47
-
-
Save velitchko/3a5479f848c5d27653ddc46454616352 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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