habitica-self-host/migrations/users/pi-day.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-03-12 22:10:38 +00:00
/* eslint-disable no-console */
import { v4 as uuid } from 'uuid';
import { model as User } from '../../website/server/models/user';
const MIGRATION_NAME = '20240314_pi_day';
2019-10-08 14:57:10 +00:00
2019-03-12 22:10:38 +00:00
const progressCount = 1000;
let count = 0;
async function updateUser (user) {
2022-03-14 20:06:57 +00:00
count += 1;
2019-03-12 22:10:38 +00:00
const inc = {
'items.food.Pie_Skeleton': 1,
'items.food.Pie_Base': 1,
'items.food.Pie_CottonCandyBlue': 1,
'items.food.Pie_CottonCandyPink': 1,
'items.food.Pie_Shade': 1,
'items.food.Pie_White': 1,
'items.food.Pie_Golden': 1,
'items.food.Pie_Zombie': 1,
'items.food.Pie_Desert': 1,
'items.food.Pie_Red': 1,
};
const set = {};
2020-03-13 15:59:52 +00:00
let push;
2019-03-12 22:10:38 +00:00
set.migration = MIGRATION_NAME;
2020-03-13 15:59:52 +00:00
if (typeof user.items.gear.owned.head_special_piDay !== 'undefined') {
push = false;
} else {
set['items.gear.owned.head_special_piDay'] = false;
set['items.gear.owned.shield_special_piDay'] = false;
push = [
{ type: 'marketGear', path: 'gear.flat.head_special_piDay', _id: uuid() },
{ type: 'marketGear', path: 'gear.flat.shield_special_piDay', _id: uuid() },
];
}
2019-03-12 22:10:38 +00:00
if (count % progressCount === 0) console.warn(`${count} ${user._id}`);
2020-03-13 15:59:52 +00:00
if (push) {
return User
.update({ _id: user._id }, { $inc: inc, $set: set, $push: { pinnedItems: { $each: push } } })
.exec();
}
2019-10-08 16:36:55 +00:00
return User
2020-03-13 15:59:52 +00:00
.update({ _id: user._id }, { $inc: inc, $set: set })
2019-10-08 16:36:55 +00:00
.exec();
2019-03-12 22:10:38 +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 },
2023-03-14 14:12:39 +00:00
'auth.timestamps.loggedin': { $gt: new Date('2023-02-15') },
2019-03-12 22:10:38 +00:00
};
const fields = {
_id: 1,
items: 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 })
2019-03-12 22:10:38 +00:00
.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],
};
}
await Promise.all(users.map(updateUser)); // eslint-disable-line no-await-in-loop
}
2019-10-15 13:32:53 +00:00
}