mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 21:27:23 +01:00
fix(scripts): update admin migrations
Refactore "full stable" to current format, add email to GDPR deletion output, fix path in bulk email script
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/* eslint-disable no-console */
|
/* 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 { model as User } from '../../website/server/models/user';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import nconf from 'nconf';
|
import nconf from 'nconf';
|
||||||
|
|||||||
@@ -1,67 +1,24 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
|
const MIGRATION_NAME = 'full-stable';
|
||||||
import each from 'lodash/each';
|
import each from 'lodash/each';
|
||||||
import keys from 'lodash/keys';
|
import keys from 'lodash/keys';
|
||||||
import content from '../../website/common/script/content/index';
|
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 ...
|
import { model as User } from '../../website/server/models/user';
|
||||||
const authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; // ... own data is done
|
|
||||||
|
const progressCount = 1000;
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Award users every extant pet and mount
|
* Award users every extant pet and mount
|
||||||
*/
|
*/
|
||||||
const connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE
|
|
||||||
|
|
||||||
let monk = require('monk');
|
async function updateUser (user) {
|
||||||
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) {
|
|
||||||
count++;
|
count++;
|
||||||
let set = {
|
|
||||||
migration: migrationName,
|
const set = {};
|
||||||
};
|
|
||||||
|
set.migration = MIGRATION_NAME;
|
||||||
|
|
||||||
each(keys(content.pets), (pet) => {
|
each(keys(content.pets), (pet) => {
|
||||||
set[`items.pets.${pet}`] = 5;
|
set[`items.pets.${pet}`] = 5;
|
||||||
@@ -88,30 +45,40 @@ function updateUser (user) {
|
|||||||
set[`items.mounts.${mount}`] = true;
|
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}`);
|
return await User.update({_id: user._id}, {$set: set}).exec();
|
||||||
if (user._id === authorUuid) console.warn(`${authorName } processed`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayData () {
|
module.exports = async function processUsers () {
|
||||||
console.warn(`\n${ count } users processed\n`);
|
let query = {
|
||||||
return exiting(0);
|
migration: {$ne: MIGRATION_NAME},
|
||||||
}
|
'auth.local.username': 'olson22',
|
||||||
|
};
|
||||||
|
|
||||||
function exiting (code, msg) {
|
const fields = {
|
||||||
code = code || 0; // 0 = success
|
_id: 1,
|
||||||
if (code && !msg) {
|
};
|
||||||
msg = 'ERROR!';
|
|
||||||
}
|
while (true) { // eslint-disable-line no-constant-condition
|
||||||
if (msg) {
|
const users = await User // eslint-disable-line no-await-in-loop
|
||||||
if (code) {
|
.find(query)
|
||||||
console.error(msg);
|
.limit(250)
|
||||||
} else {
|
.sort({_id: 1})
|
||||||
console.log(msg);
|
.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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ async function _deleteHabiticaData (user, email) {
|
|||||||
|
|
||||||
if (response) {
|
if (response) {
|
||||||
console.log(`${response.status} ${response.statusText}`);
|
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}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user