habitica/migrations/users/full-stable.js

86 lines
2 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-console */
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
2019-10-08 14:57:10 +00:00
const MIGRATION_NAME = 'full-stable';
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) {
2019-10-08 16:36:55 +00:00
count += 1;
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
2019-10-08 14:57:10 +00:00
each(keys(content.pets), pet => {
2018-04-01 02:59:54 +00:00
set[`items.pets.${pet}`] = 5;
});
2019-10-08 14:57:10 +00:00
each(keys(content.premiumPets), pet => {
2018-04-01 02:59:54 +00:00
set[`items.pets.${pet}`] = 5;
});
2019-10-08 14:57:10 +00:00
each(keys(content.questPets), pet => {
2018-04-01 02:59:54 +00:00
set[`items.pets.${pet}`] = 5;
});
2019-10-08 14:57:10 +00:00
each(keys(content.specialPets), pet => {
2018-04-01 02:59:54 +00:00
set[`items.pets.${pet}`] = 5;
});
2019-10-08 14:57:10 +00:00
each(keys(content.mounts), mount => {
2018-04-01 02:59:54 +00:00
set[`items.mounts.${mount}`] = true;
});
2019-10-08 14:57:10 +00:00
each(keys(content.premiumMounts), mount => {
2018-04-01 02:59:54 +00:00
set[`items.mounts.${mount}`] = true;
});
2019-10-08 14:57:10 +00:00
each(keys(content.questMounts), mount => {
2018-04-01 02:59:54 +00:00
set[`items.mounts.${mount}`] = true;
});
2019-10-08 14:57:10 +00:00
each(keys(content.specialMounts), mount => {
2018-04-01 02:59:54 +00:00
set[`items.mounts.${mount}`] = true;
});
if (count % progressCount === 0) console.warn(`${count} ${user._id}`);
2018-04-01 02:59:54 +00:00
2019-10-08 16:36:55 +00:00
return User.update({ _id: user._id }, { $set: set }).exec();
2018-04-01 02:59:54 +00:00
}
2019-10-15 13:32:53 +00:00
export default async function processUsers () {
2019-10-08 14:57:10 +00:00
const 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)
2019-10-08 14:57:10 +00:00
.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
}
2019-10-15 13:32:53 +00:00
}