Files
habitica/migrations/tasks/tasks-set-everyX.js
Sabe Jones 7ea6c911cb Better group plan member counts (#10449)
* fix(group-plans): improved member count accuracy

* fix(migration): don't leave server running after completion

* fix(migration): don't update Stripe for non-Stripe methods
Also fixes a linting issue.

* fix(lint): no comma dangle here

* fix(async): put async token in relevant spot

* fix(lint): still more linting

* fix(async): better handling for async and promises
Also adds additional logging where discrepancies are found.

* feat(migration): provide CSV output

* fix(promises): better pause/resume

* fix(migration): don't update already canceled subs

* fix(groups): also address quantity/memberCount discrepancies

* fix(migration): also log quantity issues

* fix(migration): equation was reversed

* refactor(migration): condense logic, add error catch

* fix(migration): fix root cause of failed quantity update??

* fix(lint): gratuitous parens

* fix(test): expect group to be updated db-side

* fix(migration): actually update quantities?

* fix(groups): roll back unneeded Stripe lib change, refactor migration
2018-06-15 14:49:18 -05:00

94 lines
2.0 KiB
JavaScript

/* let migrationName = 'tasks-set-everyX'; */
let authorName = 'Sabe'; // in case script author needs to know when their ...
let authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; // ... own data is done
/*
* Iterates over all tasks and sets invalid everyX values (less than 0 or more than 9999 or not an int) field to 0
*/
let monk = require('monk');
let connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true';
let dbTasks = monk(connectionString).get('tasks', { castIds: false });
function processTasks (lastId) {
// specify a query to limit the affected tasks (empty for all tasks):
let query = {
type: 'daily',
everyX: {
$not: {
$gte: 0,
$lte: 9999,
$type: 'int',
},
},
};
if (lastId) {
query._id = {
$gt: lastId,
};
}
dbTasks.find(query, {
sort: {_id: 1},
limit: 250,
fields: [],
})
.then(updateTasks)
.catch((err) => {
console.log(err);
return exiting(1, `ERROR! ${ err}`);
});
}
let progressCount = 1000;
let count = 0;
function updateTasks (tasks) {
if (!tasks || tasks.length === 0) {
console.warn('All appropriate tasks found and modified.');
displayData();
return;
}
let taskPromises = tasks.map(updatetask);
let lasttask = tasks[tasks.length - 1];
return Promise.all(taskPromises)
.then(() => {
return processTasks(lasttask._id);
});
}
function updatetask (task) {
count++;
let set = {everyX: 0};
dbTasks.update({_id: task._id}, {$set: set});
if (count % progressCount === 0) console.warn(`${count } ${ task._id}`);
if (task._id === authorUuid) console.warn(`${authorName } processed`);
}
function displayData () {
console.warn(`\n${ count } tasks processed\n`);
return exiting(0);
}
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);
}
}
process.exit(code);
}
module.exports = processTasks;