Reward with negative cost can no longer be created, fixes #11855 (#11870)

* Minor refactoring in scoreTask.js

* Reward value validation added (should be >= 0)
This commit is contained in:
Denys Dorokhov
2020-03-24 13:10:10 +02:00
committed by GitHub
parent 5cf6a67a36
commit 25e72ad907
7 changed files with 128 additions and 37 deletions

View File

@@ -189,6 +189,37 @@ function _updateCounter (task, direction, times) {
}
}
function _lastHistoryEntryWasToday (lastHistoryEntry, user) {
if (!lastHistoryEntry || !lastHistoryEntry.date) {
return false;
}
const { timezoneOffset } = user.preferences;
const { dayStart } = user.preferences;
// Adjust the last entry date according to the user's timezone and CDS
const dateWithTimeZone = moment(lastHistoryEntry.date).zone(timezoneOffset);
if (dateWithTimeZone.hour() < dayStart) dateWithTimeZone.subtract(1, 'day');
return moment().zone(timezoneOffset).isSame(dateWithTimeZone, 'day');
}
function _updateLastHistoryEntry (lastHistoryEntry, task, direction, times) {
lastHistoryEntry.value = task.value;
lastHistoryEntry.date = Number(new Date());
// @TODO remove this extra check after migration
// has run to set scoredUp and scoredDown in every task
lastHistoryEntry.scoredUp = lastHistoryEntry.scoredUp || 0;
lastHistoryEntry.scoredDown = lastHistoryEntry.scoredDown || 0;
if (direction === 'up') {
lastHistoryEntry.scoredUp += times;
} else {
lastHistoryEntry.scoredDown += times;
}
}
export default function scoreTask (options = {}, req = {}, analytics) {
const {
user, task, direction, times = 1, cron = false,
@@ -226,38 +257,14 @@ export default function scoreTask (options = {}, req = {}, analytics) {
// Save history entry for habit
task.history = task.history || [];
const { timezoneOffset } = user.preferences;
const { dayStart } = user.preferences;
const historyLength = task.history.length;
const lastHistoryEntry = task.history[historyLength - 1];
// Adjust the last entry date according to the user's timezone and CDS
let lastHistoryEntryDate;
if (lastHistoryEntry && lastHistoryEntry.date) {
lastHistoryEntryDate = moment(lastHistoryEntry.date).zone(timezoneOffset);
if (lastHistoryEntryDate.hour() < dayStart) lastHistoryEntryDate.subtract(1, 'day');
}
if (
lastHistoryEntryDate
&& moment().zone(timezoneOffset).isSame(lastHistoryEntryDate, 'day')
) {
lastHistoryEntry.value = task.value;
lastHistoryEntry.date = Number(new Date());
// @TODO remove this extra check after migration
// has run to set scoredUp and scoredDown in every task
lastHistoryEntry.scoredUp = lastHistoryEntry.scoredUp || 0;
lastHistoryEntry.scoredDown = lastHistoryEntry.scoredDown || 0;
if (direction === 'up') {
lastHistoryEntry.scoredUp += times;
} else {
lastHistoryEntry.scoredDown += times;
if (_lastHistoryEntryWasToday(lastHistoryEntry, user)) {
_updateLastHistoryEntry(lastHistoryEntry, task, direction, times);
if (task.markModified) {
task.markModified(`history.${historyLength - 1}`);
}
if (task.markModified) task.markModified(`history.${historyLength - 1}`);
} else {
task.history.push({
date: Number(new Date()),
@@ -334,12 +341,7 @@ export default function scoreTask (options = {}, req = {}, analytics) {
// Don't adjust values for rewards
delta += _changeTaskValue(user, task, direction, times, cron);
// purchase item
stats.gp -= Math.abs(task.value);
// hp - gp difference
if (stats.gp < 0) {
stats.hp += stats.gp;
stats.gp = 0;
}
stats.gp -= task.value;
}
req.yesterDailyScored = task.yesterDailyScored;