diff --git a/migrations/users/bulk-email.js b/migrations/users/bulk-email.js index 69b8a23990..adba28f805 100644 --- a/migrations/users/bulk-email.js +++ b/migrations/users/bulk-email.js @@ -1,5 +1,5 @@ /* eslint-disable no-console */ -import { sendTxn } from '../../../website/server/libs/email'; +import { sendTxn } from '../../website/server/libs/email'; import { model as User } from '../../website/server/models/user'; import moment from 'moment'; import nconf from 'nconf'; diff --git a/migrations/users/full-stable.js b/migrations/users/full-stable.js index 76b0d922f6..f41f76f359 100644 --- a/migrations/users/full-stable.js +++ b/migrations/users/full-stable.js @@ -1,67 +1,24 @@ +/* eslint-disable no-console */ +const MIGRATION_NAME = 'full-stable'; import each from 'lodash/each'; import keys from 'lodash/keys'; import content from '../../website/common/script/content/index'; -const migrationName = 'full-stable.js'; -const authorName = 'Sabe'; // in case script author needs to know when their ... -const authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; // ... own data is done + +import { model as User } from '../../website/server/models/user'; + +const progressCount = 1000; +let count = 0; /* * Award users every extant pet and mount */ -const connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE -let monk = require('monk'); -let dbUsers = monk(connectionString).get('users', { castIds: false }); - -function processUsers (lastId) { - // specify a query to limit the affected users (empty for all users): - let query = { - 'profile.name': 'SabreCat', - }; - - if (lastId) { - query._id = { - $gt: lastId, - }; - } - - dbUsers.find(query, { - sort: {_id: 1}, - limit: 250, - fields: [ - ], // specify fields we are interested in to limit retrieved data (empty if we're not reading data): - }) - .then(updateUsers) - .catch((err) => { - console.log(err); - return exiting(1, `ERROR! ${ err}`); - }); -} - -let progressCount = 1000; -let count = 0; - -function updateUsers (users) { - if (!users || users.length === 0) { - console.warn('All appropriate users found and modified.'); - displayData(); - return; - } - - let userPromises = users.map(updateUser); - let lastUser = users[users.length - 1]; - - return Promise.all(userPromises) - .then(() => { - processUsers(lastUser._id); - }); -} - -function updateUser (user) { +async function updateUser (user) { count++; - let set = { - migration: migrationName, - }; + + const set = {}; + + set.migration = MIGRATION_NAME; each(keys(content.pets), (pet) => { set[`items.pets.${pet}`] = 5; @@ -88,30 +45,40 @@ function updateUser (user) { set[`items.mounts.${mount}`] = true; }); - dbUsers.update({_id: user._id}, {$set: set}); + if (count % progressCount === 0) console.warn(`${count} ${user._id}`); - if (count % progressCount === 0) console.warn(`${count } ${ user._id}`); - if (user._id === authorUuid) console.warn(`${authorName } processed`); + return await User.update({_id: user._id}, {$set: set}).exec(); } -function displayData () { - console.warn(`\n${ count } users processed\n`); - return exiting(0); -} +module.exports = async function processUsers () { + let query = { + migration: {$ne: MIGRATION_NAME}, + 'auth.local.username': 'olson22', + }; -function exiting (code, msg) { - code = code || 0; // 0 = success - if (code && !msg) { - msg = 'ERROR!'; - } - if (msg) { - if (code) { - console.error(msg); - } else { - console.log(msg); + const fields = { + _id: 1, + }; + + while (true) { // eslint-disable-line no-constant-condition + const users = await User // eslint-disable-line no-await-in-loop + .find(query) + .limit(250) + .sort({_id: 1}) + .select(fields) + .lean() + .exec(); + + if (users.length === 0) { + console.warn('All appropriate users found and modified.'); + console.warn(`\n${count} users processed\n`); + break; + } else { + query._id = { + $gt: users[users.length - 1], + }; } - } - process.exit(code); -} -module.exports = processUsers; + await Promise.all(users.map(updateUser)); // eslint-disable-line no-await-in-loop + } +}; diff --git a/scripts/gdpr-delete-users.js b/scripts/gdpr-delete-users.js index b8201697a1..f5e3166464 100644 --- a/scripts/gdpr-delete-users.js +++ b/scripts/gdpr-delete-users.js @@ -53,7 +53,7 @@ async function _deleteHabiticaData (user, email) { if (response) { console.log(`${response.status} ${response.statusText}`); - if (response.status === 200) console.log(`${user._id} removed. Last login: ${user.auth.timestamps.loggedin}`); + if (response.status === 200) console.log(`${user._id} (${email}) removed. Last login: ${user.auth.timestamps.loggedin}`); } }