Skip to content

Instantly share code, notes, and snippets.

@Streemo
Created July 28, 2016 18:41
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 Streemo/01943bf8fa0a7239b15ed30203beed29 to your computer and use it in GitHub Desktop.
Save Streemo/01943bf8fa0a7239b15ed30203beed29 to your computer and use it in GitHub Desktop.
Instead of doing N findOne operations, do a single find operation. Could add some code to explicitly handle 1 vs. N, but it's cleaner to just use arrays for everything, including single elements.
send(m){
if (m.numbers){
m.numbers.forEach((number)=>{
this._sendSMS(m.text, number);
})
} else {
let sentTo = [];
Devices.find({userId: {$in: m.users}}).forEach((device) => {
const devId = this._sendNotification(device, m.title, m.text, m.data);
devId && sentTo.push(devId)
})
Devices.update({_id:{$in: sentTo}},{$set:{lastUsed: new Date()}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment