habitica-self-host/migrations/users/full-stable.js

85 lines
2 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-console */
const MIGRATION_NAME = 'full-stable';
2018-04-01 02:59:54 +00:00
import each from 'lodash/each';
import keys from 'lodash/keys';
import content from '../../website/common/script/content/index';
import { model as User } from '../../website/server/models/user';
2018-04-01 02:59:54 +00:00
const progressCount = 1000;
2018-04-01 02:59:54 +00:00
let count = 0;
/*
* Award users every extant pet and mount
*/
2018-04-01 02:59:54 +00:00
async function updateUser (user) {
count++;
2018-04-01 02:59:54 +00:00
const set = {};
2018-04-01 02:59:54 +00:00
set.migration = MIGRATION_NAME;
2018-04-01 02:59:54 +00:00
each(keys(content.pets), (pet) => {
set[`items.pets.${pet}`] = 5;
});
each(keys(content.premiumPets), (pet) => {
set[`items.pets.${pet}`] = 5;
});
each(keys(content.questPets), (pet) => {
set[`items.pets.${pet}`] = 5;
});
each(keys(content.specialPets), (pet) => {
set[`items.pets.${pet}`] = 5;
});
each(keys(content.mounts), (mount) => {
set[`items.mounts.${mount}`] = true;
});
each(keys(content.premiumMounts), (mount) => {
set[`items.mounts.${mount}`] = true;
});
each(keys(content.questMounts), (mount) => {
set[`items.mounts.${mount}`] = true;
});
each(keys(content.specialMounts), (mount) => {
set[`items.mounts.${mount}`] = true;
});
if (count % progressCount === 0) console.warn(`${count} ${user._id}`);
2018-04-01 02:59:54 +00:00
return await User.update({_id: user._id}, {$set: set}).exec();
2018-04-01 02:59:54 +00:00
}
module.exports = async function processUsers () {
let query = {
migration: {$ne: MIGRATION_NAME},
'auth.local.username': 'olson22',
};
2018-04-01 02:59:54 +00:00
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],
};
2018-04-01 02:59:54 +00:00
}
await Promise.all(users.map(updateUser)); // eslint-disable-line no-await-in-loop
}
};