mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 06:37:23 +01:00
* Clean version of PR 8175 The original PR for this was here: https://github.com/HabitRPG/habitica/pull/8175 Unfortunately while fixing a conflict in tasks.json, I messed up the rebase and wound up pulling in too many commits and making a giant mess. Sorry. :P * Fixing test failure This test seems to occasionally start failing (another coder reported the same thing happening to them in the blacksmiths’ guild) because the order in which the tasks are created can sometimes not match the order in the array. So I have sorted the tasks array after creation by the task name to ensure a consistent ordering, and slightly reordered the expect statements to match.
78 lines
1.6 KiB
JavaScript
78 lines
1.6 KiB
JavaScript
import { v4 as uuid } from 'uuid';
|
|
import _ from 'lodash';
|
|
import moment from 'moment';
|
|
|
|
// Even though Mongoose handles task defaults, we want to make sure defaults are set on the client-side before
|
|
// sending up to the server for performance
|
|
|
|
// TODO move to client code?
|
|
|
|
const tasksTypes = ['habit', 'daily', 'todo', 'reward'];
|
|
|
|
module.exports = function taskDefaults (task = {}) {
|
|
if (!task.type || tasksTypes.indexOf(task.type) === -1) {
|
|
task.type = 'habit';
|
|
}
|
|
|
|
let defaultId = uuid();
|
|
let defaults = {
|
|
_id: defaultId,
|
|
text: task._id || defaultId,
|
|
notes: '',
|
|
tags: [],
|
|
value: task.type === 'reward' ? 10 : 0,
|
|
priority: 1,
|
|
challenge: {},
|
|
reminders: [],
|
|
attribute: 'str',
|
|
createdAt: new Date(), // TODO these are going to be overwritten by the server...
|
|
updatedAt: new Date(),
|
|
};
|
|
|
|
_.defaults(task, defaults);
|
|
|
|
if (task.type === 'habit' || task.type === 'daily') {
|
|
_.defaults(task, {
|
|
history: [],
|
|
});
|
|
}
|
|
|
|
if (task.type === 'todo' || task.type === 'daily') {
|
|
_.defaults(task, {
|
|
completed: false,
|
|
collapseChecklist: false,
|
|
checklist: [],
|
|
});
|
|
}
|
|
|
|
if (task.type === 'habit') {
|
|
_.defaults(task, {
|
|
up: true,
|
|
down: true,
|
|
frequency: 'daily',
|
|
counterUp: 0,
|
|
counterDown: 0,
|
|
});
|
|
}
|
|
|
|
if (task.type === 'daily') {
|
|
_.defaults(task, {
|
|
streak: 0,
|
|
repeat: {
|
|
m: true,
|
|
t: true,
|
|
w: true,
|
|
th: true,
|
|
f: true,
|
|
s: true,
|
|
su: true,
|
|
},
|
|
startDate: moment().startOf('day').toDate(),
|
|
everyX: 1,
|
|
frequency: 'weekly',
|
|
});
|
|
}
|
|
|
|
return task;
|
|
};
|