Files
habitica/migrations/tasks/rewards-flip-negative-costs.js
Denys Dorokhov 25e72ad907 Reward with negative cost can no longer be created, fixes #11855 (#11870)
* Minor refactoring in scoreTask.js

* Reward value validation added (should be >= 0)
2020-03-24 12:10:10 +01:00

55 lines
1.2 KiB
JavaScript

// @migrationName = 'RewardsMigrationFlipNegativeCostsValues';
// @authorName = 'hamboomger';
// @authorUuid = '80b61b73-2278-4947-b713-a10112cfe7f5';
/*
* For each reward with negative cost, make it positive
* by assigning it an absolute value of itself
*/
import { Task } from '../../website/server/models/task';
async function flipNegativeCostsValues () {
const query = {
type: 'reward',
value: { $lt: 0 },
};
const fields = {
_id: 1,
value: 1,
};
// eslint-disable-next-line no-constant-condition
while (true) {
// eslint-disable-next-line no-await-in-loop
const rewards = await Task
.find(query)
.limit(250)
.sort({ _id: 1 })
.select(fields)
.lean()
.exec();
if (rewards.length === 0) {
break;
}
const promises = rewards.map(reward => {
const positiveValue = Math.abs(reward.value);
return Task.update({ _id: reward._id }, { $set: { value: positiveValue } }).exec();
});
// eslint-disable-next-line no-await-in-loop
await Promise.all(promises);
query._id = {
$gt: rewards[rewards.length - 1]._id,
};
}
console.log('All rewards with negative values were updated, migration finished');
}
export default flipNegativeCostsValues;