Last active
September 26, 2016 18:17
-
-
Save mjlescano/c9b3582562d18711c2afa559d0478c3c to your computer and use it in GitHub Desktop.
Mass-edit emails of users on DemocracyOS on mongoose
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
var OWNER_EMAIL = process.env.OWNER_EMAIL || '[email protected]' | |
//-------------------------- | |
require('lib/models')() | |
var config = require('lib/config') | |
var models = require('lib/models') | |
var User = models.User | |
Promise.all([ | |
User.findOne({email: OWNER_EMAIL}).exec(), | |
User.find({email: {$ne: OWNER_EMAIL}}).count().exec() | |
]).then(function (results) { | |
var owner = results[0] | |
var count = results[1] | |
if (!owner) throw new Error('need to set a real owner reference User') | |
console.log(`· processing ${count} users`) | |
var done = 0 | |
User.find({ | |
email: {$ne: OWNER_EMAIL} | |
}).stream().on('data', function (user) { | |
user.email = owner.email.replace('@', `+${user._id}@`) | |
user.salt = owner.salt | |
user.hash = owner.hash | |
user.save(function (err) { | |
if (err) console.error(err, user.email) | |
if (++done === count) { | |
console.log('· done!') | |
process.exit(0) | |
} | |
}) | |
}).on('error', console.error.bind(console)) | |
}).catch(function (err) { | |
console.error(err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment