mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
start upgrading eslint
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import moment from 'moment';
|
||||
import _ from 'lodash';
|
||||
import * as Tasks from '../models/task';
|
||||
import {
|
||||
BadRequest,
|
||||
@@ -6,12 +7,11 @@ import {
|
||||
import {
|
||||
SHARED_COMPLETION,
|
||||
} from './groupTasks';
|
||||
import _ from 'lodash';
|
||||
import shared from '../../common';
|
||||
|
||||
async function _validateTaskAlias (tasks, res) {
|
||||
let tasksWithAliases = tasks.filter(task => task.alias);
|
||||
let aliases = tasksWithAliases.map(task => task.alias);
|
||||
const tasksWithAliases = tasks.filter(task => task.alias);
|
||||
const aliases = tasksWithAliases.map(task => task.alias);
|
||||
|
||||
// Compares the short names in tasks against
|
||||
// a Set, where values cannot repeat. If the
|
||||
@@ -20,9 +20,7 @@ async function _validateTaskAlias (tasks, res) {
|
||||
throw new BadRequest(res.t('taskAliasAlreadyUsed'));
|
||||
}
|
||||
|
||||
await Promise.all(tasksWithAliases.map((task) => {
|
||||
return task.validate();
|
||||
}));
|
||||
await Promise.all(tasksWithAliases.map(task => task.validate()));
|
||||
}
|
||||
|
||||
export function setNextDue (task, user, dueDateOption) {
|
||||
@@ -44,16 +42,14 @@ export function setNextDue (task, user, dueDateOption) {
|
||||
}
|
||||
|
||||
|
||||
let optionsForShouldDo = user.preferences.toObject();
|
||||
const optionsForShouldDo = user.preferences.toObject();
|
||||
optionsForShouldDo.now = now;
|
||||
task.isDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
|
||||
optionsForShouldDo.nextDue = true;
|
||||
let nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
const nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
if (nextDue && nextDue.length > 0) {
|
||||
task.nextDue = nextDue.map((dueDate) => {
|
||||
return dueDate.toISOString();
|
||||
});
|
||||
task.nextDue = nextDue.map(dueDate => dueDate.toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,27 +66,27 @@ export function setNextDue (task, user, dueDateOption) {
|
||||
* @return The created tasks
|
||||
*/
|
||||
export async function createTasks (req, res, options = {}) {
|
||||
let {
|
||||
const {
|
||||
user,
|
||||
challenge,
|
||||
group,
|
||||
} = options;
|
||||
|
||||
let owner = group || challenge || user;
|
||||
const owner = group || challenge || user;
|
||||
|
||||
let toSave = Array.isArray(req.body) ? req.body : [req.body];
|
||||
|
||||
// Return if no tasks are passed, avoids errors with mongo $push being empty
|
||||
if (toSave.length === 0) return [];
|
||||
|
||||
let taskOrderToAdd = {};
|
||||
const taskOrderToAdd = {};
|
||||
|
||||
toSave = toSave.map(taskData => {
|
||||
// Validate that task.type is valid
|
||||
if (!taskData || Tasks.tasksTypes.indexOf(taskData.type) === -1) throw new BadRequest(res.t('invalidTaskType'));
|
||||
|
||||
let taskType = taskData.type;
|
||||
let newTask = new Tasks[taskType](Tasks.Task.sanitize(taskData));
|
||||
const taskType = taskData.type;
|
||||
const newTask = new Tasks[taskType](Tasks.Task.sanitize(taskData));
|
||||
|
||||
if (challenge) {
|
||||
newTask.challenge.id = challenge.id;
|
||||
@@ -108,7 +104,7 @@ export async function createTasks (req, res, options = {}) {
|
||||
|
||||
// Validate that the task is valid and throw if it isn't
|
||||
// otherwise since we're saving user/challenge/group and task in parallel it could save the user/challenge/group with a tasksOrder that doens't match reality
|
||||
let validationErrors = newTask.validateSync();
|
||||
const validationErrors = newTask.validateSync();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
// Otherwise update the user/challenge/group
|
||||
@@ -119,8 +115,8 @@ export async function createTasks (req, res, options = {}) {
|
||||
});
|
||||
|
||||
// Push all task ids
|
||||
let taskOrderUpdateQuery = {$push: {}};
|
||||
for (let taskType in taskOrderToAdd) {
|
||||
const taskOrderUpdateQuery = { $push: {} };
|
||||
for (const taskType in taskOrderToAdd) {
|
||||
taskOrderUpdateQuery.$push[`tasksOrder.${taskType}`] = {
|
||||
$each: taskOrderToAdd[taskType],
|
||||
$position: 0,
|
||||
@@ -138,7 +134,7 @@ export async function createTasks (req, res, options = {}) {
|
||||
|
||||
toSave.unshift(owner.save());
|
||||
|
||||
let tasks = await Promise.all(toSave);
|
||||
const tasks = await Promise.all(toSave);
|
||||
tasks.splice(0, 1); // Remove user, challenge, or group promise
|
||||
return tasks;
|
||||
}
|
||||
@@ -156,25 +152,25 @@ export async function createTasks (req, res, options = {}) {
|
||||
* @return The tasks found
|
||||
*/
|
||||
export async function getTasks (req, res, options = {}) {
|
||||
let {
|
||||
const {
|
||||
user,
|
||||
challenge,
|
||||
group,
|
||||
dueDate,
|
||||
} = options;
|
||||
|
||||
let query = {userId: user._id};
|
||||
let query = { userId: user._id };
|
||||
let limit;
|
||||
let sort;
|
||||
let owner = group || challenge || user;
|
||||
const owner = group || challenge || user;
|
||||
|
||||
if (challenge) {
|
||||
query = {'challenge.id': challenge.id, userId: {$exists: false}};
|
||||
query = { 'challenge.id': challenge.id, userId: { $exists: false } };
|
||||
} else if (group) {
|
||||
query = {'group.id': group._id, userId: {$exists: false}};
|
||||
query = { 'group.id': group._id, userId: { $exists: false } };
|
||||
}
|
||||
|
||||
let type = req.query.type;
|
||||
const { type } = req.query;
|
||||
|
||||
if (type) {
|
||||
if (type === 'todos') {
|
||||
@@ -202,32 +198,32 @@ export async function getTasks (req, res, options = {}) {
|
||||
}
|
||||
} else {
|
||||
query.$or = [ // Exclude completed todos
|
||||
{type: 'todo', completed: false},
|
||||
{type: {$in: ['habit', 'daily', 'reward']}},
|
||||
{ type: 'todo', completed: false },
|
||||
{ type: { $in: ['habit', 'daily', 'reward'] } },
|
||||
];
|
||||
}
|
||||
|
||||
let mQuery = Tasks.Task.find(query);
|
||||
const mQuery = Tasks.Task.find(query);
|
||||
if (limit) mQuery.limit(limit);
|
||||
if (sort) mQuery.sort(sort);
|
||||
|
||||
let tasks = await mQuery.exec();
|
||||
const tasks = await mQuery.exec();
|
||||
|
||||
if (dueDate) {
|
||||
tasks.forEach((task) => {
|
||||
tasks.forEach(task => {
|
||||
setNextDue(task, user, dueDate);
|
||||
});
|
||||
}
|
||||
|
||||
// Order tasks based on tasksOrder
|
||||
if (type && type !== 'completedTodos' && type !== '_allCompletedTodos') {
|
||||
let order = owner.tasksOrder[type];
|
||||
const order = owner.tasksOrder[type];
|
||||
let orderedTasks = new Array(tasks.length);
|
||||
let unorderedTasks = []; // what we want to add later
|
||||
const unorderedTasks = []; // what we want to add later
|
||||
|
||||
tasks.forEach((task, index) => {
|
||||
let taskId = task._id;
|
||||
let i = order[index] === taskId ? index : order.indexOf(taskId);
|
||||
const taskId = task._id;
|
||||
const i = order[index] === taskId ? index : order.indexOf(taskId);
|
||||
if (i === -1) {
|
||||
unorderedTasks.push(task);
|
||||
} else {
|
||||
@@ -238,17 +234,16 @@ export async function getTasks (req, res, options = {}) {
|
||||
// Remove empty values from the array and add any unordered task
|
||||
orderedTasks = _.compact(orderedTasks).concat(unorderedTasks);
|
||||
return orderedTasks;
|
||||
} else {
|
||||
return tasks;
|
||||
}
|
||||
return tasks;
|
||||
}
|
||||
|
||||
// Takes a Task document and return a plain object of attributes that can be synced to the user
|
||||
|
||||
export function syncableAttrs (task) {
|
||||
let t = task.toObject(); // lodash doesn't seem to like _.omit on Document
|
||||
const t = task.toObject(); // lodash doesn't seem to like _.omit on Document
|
||||
// only sync/compare important attrs
|
||||
let omitAttrs = ['_id', 'userId', 'challenge', 'history', 'tags', 'completed', 'streak', 'notes', 'updatedAt', 'createdAt', 'group', 'checklist', 'attribute'];
|
||||
const omitAttrs = ['_id', 'userId', 'challenge', 'history', 'tags', 'completed', 'streak', 'notes', 'updatedAt', 'createdAt', 'group', 'checklist', 'attribute'];
|
||||
if (t.type !== 'reward') omitAttrs.push('value');
|
||||
return _.omit(t, omitAttrs);
|
||||
}
|
||||
@@ -263,7 +258,7 @@ export function syncableAttrs (task) {
|
||||
* @return Empty
|
||||
*/
|
||||
export function moveTask (order, taskId, to) {
|
||||
let currentIndex = order.indexOf(taskId);
|
||||
const currentIndex = order.indexOf(taskId);
|
||||
|
||||
// If for some reason the task isn't ordered (should never happen), push it in the new position
|
||||
// if the task is moved to a non existing position
|
||||
|
||||
Reference in New Issue
Block a user