mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
* Minor refactoring in scoreTask.js * Reward value validation added (should be >= 0)
This commit is contained in:
54
migrations/tasks/rewards-flip-negative-costs.js
Normal file
54
migrations/tasks/rewards-flip-negative-costs.js
Normal file
@@ -0,0 +1,54 @@
|
||||
// @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;
|
||||
Reference in New Issue
Block a user