Skip to content

Instantly share code, notes, and snippets.

@crtr0
Created January 11, 2013 23:09
Show Gist options
  • Select an option

  • Save crtr0/4514741 to your computer and use it in GitHub Desktop.

Select an option

Save crtr0/4514741 to your computer and use it in GitHub Desktop.
flushVotes = function() {
var votesToSave = _und.values(votesCache);
votesCache = {};
if (votesToSave.length > 0) {
db.bulk({docs: votesToSave}, function(err, body) {
if (err) {
console.log("Failed to save votes, popping them back on the cache");
votesToSave.forEach(function(v) {
votesCache[v._id] = v;
});
}
else {
// loop through the response to detect votes that were rejected as duplicates
for (var i in votesToSave) {
if (body[i].error) {
// send the person an SMS to alert them that you can only vote once
console.log('Notifying of duplicate vote: ', votesToSave[i])
client.sendSms({To: votesToSave[i].phonenumber, From: votesToSave[i].event_phonenumber, Body: 'Sorry, you are only allowed to vote once.'});
}
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment