Drag challenge tasks (#12204)
* Allow challenge tasks to be draggable by leaders and admins * Drag challenge tasks, ensure they're ordered * Ensure group tasks are ordered properly, make draggable * Add tests, fix broken tests * Resolve merge conflict * Remove console.log() * Address code review comments * Code review fixes * Fix lint * Fix importing * taskManager * Lint * Fix collapseChecklist test Co-authored-by: Sabe Jones <sabrecat@gmail.com>
This commit is contained in:
@@ -19,7 +19,7 @@ import * as Tasks from '../../models/task';
|
||||
import csvStringify from '../../libs/csvStringify';
|
||||
import {
|
||||
createTasks,
|
||||
} from '../../libs/taskManager';
|
||||
} from '../../libs/tasks';
|
||||
|
||||
import {
|
||||
addUserJoinChallengeNotification,
|
||||
|
||||
@@ -16,35 +16,19 @@ import {
|
||||
import {
|
||||
createTasks,
|
||||
getTasks,
|
||||
getGroupFromTaskAndUser,
|
||||
getChallengeFromTask,
|
||||
scoreTasks,
|
||||
verifyTaskModification,
|
||||
} from '../../libs/tasks';
|
||||
import {
|
||||
moveTask,
|
||||
setNextDue,
|
||||
scoreTasks,
|
||||
} from '../../libs/taskManager';
|
||||
requiredGroupFields,
|
||||
} from '../../libs/tasks/utils';
|
||||
import common from '../../../common';
|
||||
import apiError from '../../libs/apiError';
|
||||
|
||||
// @TODO abstract, see api-v3/tasks/groups.js
|
||||
function canNotEditTasks (group, user, assignedUserId, taskPayload = null) {
|
||||
const isNotGroupLeader = group.leader !== user._id;
|
||||
const isManager = Boolean(group.managers[user._id]);
|
||||
const userIsAssigningToSelf = Boolean(assignedUserId && user._id === assignedUserId);
|
||||
|
||||
const taskPayloadProps = taskPayload
|
||||
? Object.keys(taskPayload)
|
||||
: [];
|
||||
|
||||
// only allow collapseChecklist to be changed by everyone
|
||||
const allowedByTaskPayload = taskPayloadProps.length === 1
|
||||
&& taskPayloadProps.includes('collapseChecklist');
|
||||
|
||||
if (allowedByTaskPayload) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isNotGroupLeader && !isManager
|
||||
&& !userIsAssigningToSelf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @apiDefine TaskNotFound
|
||||
* @apiError (404) {NotFound} TaskNotFound The specified task could not be found.
|
||||
@@ -61,7 +45,6 @@ function canNotEditTasks (group, user, assignedUserId, taskPayload = null) {
|
||||
*/
|
||||
|
||||
const api = {};
|
||||
const requiredGroupFields = '_id leader tasksOrder name';
|
||||
|
||||
/**
|
||||
* @api {post} /api/v3/tasks/user Create a new task belonging to the user
|
||||
@@ -613,7 +596,6 @@ api.updateTask = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
let challenge;
|
||||
|
||||
req.checkParams('taskId', apiError('taskIdRequired')).notEmpty();
|
||||
|
||||
@@ -622,26 +604,30 @@ api.updateTask = {
|
||||
|
||||
const { taskId } = req.params;
|
||||
const task = await Tasks.Task.findByIdOrAlias(taskId, user._id);
|
||||
let group;
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
|
||||
const group = await getGroupFromTaskAndUser(task, user);
|
||||
const challenge = await getChallengeFromTask(task);
|
||||
// Verify that the user can modify the task.
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.group.id && !task.userId) {
|
||||
// @TODO: Abstract this access snippet
|
||||
const fields = requiredGroupFields.concat(' managers');
|
||||
group = await Group.getGroup({ user, groupId: task.group.id, fields });
|
||||
// If the task is in a group and only modifying `collapseChecklist`,
|
||||
// the modification should be allowed.
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (canNotEditTasks(group, user, null, req.body)) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
const taskPayloadProps = Object.keys(req.body);
|
||||
|
||||
// If the task belongs to a challenge make sure the user has rights
|
||||
} else if (task.challenge.id && !task.userId) {
|
||||
challenge = await Challenge.findOne({ _id: task.challenge.id }).exec();
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
if (!challenge.canModify(user)) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||
const allowedByTaskPayload = taskPayloadProps.length === 1
|
||||
&& taskPayloadProps.includes('collapseChecklist');
|
||||
|
||||
// If the task is owned by a user make it's the current one
|
||||
} else if (task.userId !== user._id) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
if (!allowedByTaskPayload) {
|
||||
// Otherwise, verify the task modification normally.
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
}
|
||||
} else {
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
}
|
||||
|
||||
const oldCheckList = task.checklist;
|
||||
@@ -832,20 +818,29 @@ api.moveTask = {
|
||||
const { taskId } = req.params;
|
||||
const to = Number(req.params.position);
|
||||
|
||||
const task = await Tasks.Task.findByIdOrAlias(taskId, user._id, { userId: user._id });
|
||||
const task = await Tasks.Task.findByIdOrAlias(taskId, user._id);
|
||||
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
|
||||
const group = await getGroupFromTaskAndUser(task, user);
|
||||
const challenge = await getChallengeFromTask(task);
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
|
||||
if (!task) throw new NotFound(res.t('taskNotFound'));
|
||||
if (task.type === 'todo' && task.completed) throw new BadRequest(res.t('cantMoveCompletedTodo'));
|
||||
|
||||
const owner = group || challenge || user;
|
||||
|
||||
// In memory updates
|
||||
const order = user.tasksOrder[`${task.type}s`];
|
||||
const order = owner.tasksOrder[`${task.type}s`];
|
||||
moveTask(order, task._id, to);
|
||||
|
||||
// Server updates
|
||||
// Cannot send $pull and $push on same field in one single op
|
||||
const pullQuery = { $pull: {} };
|
||||
pullQuery.$pull[`tasksOrder.${task.type}s`] = task.id;
|
||||
await user.update(pullQuery).exec();
|
||||
await owner.update(pullQuery).exec();
|
||||
|
||||
let position = to;
|
||||
if (to === -1) position = order.length - 1; // push to bottom
|
||||
@@ -855,12 +850,15 @@ api.moveTask = {
|
||||
$each: [task._id],
|
||||
$position: position,
|
||||
};
|
||||
await user.update(updateQuery).exec();
|
||||
await owner.update(updateQuery).exec();
|
||||
|
||||
// Update the user version field manually,
|
||||
// it cannot be updated in the pre update hook
|
||||
// See https://github.com/HabitRPG/habitica/pull/9321#issuecomment-354187666 for more info
|
||||
user._v += 1;
|
||||
// Only users have a version.
|
||||
if (!group && !challenge) {
|
||||
owner._v += 1;
|
||||
}
|
||||
|
||||
res.respond(200, order);
|
||||
},
|
||||
@@ -900,8 +898,6 @@ api.addChecklistItem = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
let challenge;
|
||||
let group;
|
||||
|
||||
req.checkParams('taskId', apiError('taskIdRequired')).notEmpty();
|
||||
|
||||
@@ -913,22 +909,12 @@ api.addChecklistItem = {
|
||||
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.group.id && !task.userId) {
|
||||
const fields = requiredGroupFields.concat(' managers');
|
||||
group = await Group.getGroup({ user, groupId: task.group.id, fields });
|
||||
if (canNotEditTasks(group, user)) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
|
||||
// If the task belongs to a challenge make sure the user has rights
|
||||
} else if (task.challenge.id && !task.userId) {
|
||||
challenge = await Challenge.findOne({ _id: task.challenge.id }).exec();
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
if (!challenge.canModify(user)) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||
|
||||
// If the task is owned by a user make it's the current one
|
||||
} else if (task.userId !== user._id) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
|
||||
const group = await getGroupFromTaskAndUser(task, user);
|
||||
const challenge = await getChallengeFromTask(task);
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
|
||||
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
||||
|
||||
const newCheckListItem = Tasks.Task.sanitizeChecklist(req.body);
|
||||
@@ -1018,8 +1004,6 @@ api.updateChecklistItem = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
let challenge;
|
||||
let group;
|
||||
|
||||
req.checkParams('taskId', apiError('taskIdRequired')).notEmpty();
|
||||
req.checkParams('itemId', res.t('itemIdRequired')).notEmpty().isUUID();
|
||||
@@ -1032,22 +1016,11 @@ api.updateChecklistItem = {
|
||||
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.group.id && !task.userId) {
|
||||
const fields = requiredGroupFields.concat(' managers');
|
||||
group = await Group.getGroup({ user, groupId: task.group.id, fields });
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (canNotEditTasks(group, user)) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
|
||||
// If the task belongs to a challenge make sure the user has rights
|
||||
} else if (task.challenge.id && !task.userId) {
|
||||
challenge = await Challenge.findOne({ _id: task.challenge.id }).exec();
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
if (!challenge.canModify(user)) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||
|
||||
// If the task is owned by a user make it's the current one
|
||||
} else if (task.userId !== user._id) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
|
||||
const group = await getGroupFromTaskAndUser(task, user);
|
||||
const challenge = await getChallengeFromTask(task);
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
||||
|
||||
const item = _.find(task.checklist, { id: req.params.itemId });
|
||||
@@ -1095,8 +1068,6 @@ api.removeChecklistItem = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
let challenge;
|
||||
let group;
|
||||
|
||||
req.checkParams('taskId', apiError('taskIdRequired')).notEmpty();
|
||||
req.checkParams('itemId', res.t('itemIdRequired')).notEmpty().isUUID();
|
||||
@@ -1109,22 +1080,11 @@ api.removeChecklistItem = {
|
||||
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.group.id && !task.userId) {
|
||||
const fields = requiredGroupFields.concat(' managers');
|
||||
group = await Group.getGroup({ user, groupId: task.group.id, fields });
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (canNotEditTasks(group, user)) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
|
||||
// If the task belongs to a challenge make sure the user has rights
|
||||
} else if (task.challenge.id && !task.userId) {
|
||||
challenge = await Challenge.findOne({ _id: task.challenge.id }).exec();
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
if (!challenge.canModify(user)) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||
|
||||
// If the task is owned by a user make it's the current one
|
||||
} else if (task.userId !== user._id) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
|
||||
const group = await getGroupFromTaskAndUser(task, user);
|
||||
const challenge = await getChallengeFromTask(task);
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
||||
|
||||
const hasItem = removeFromArray(task.checklist, { id: req.params.itemId });
|
||||
@@ -1447,30 +1407,19 @@ api.deleteTask = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
let challenge;
|
||||
|
||||
const { taskId } = req.params;
|
||||
const task = await Tasks.Task.findByIdOrAlias(taskId, user._id);
|
||||
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.group.id && !task.userId) {
|
||||
// @TODO: Abstract this access snippet
|
||||
const fields = requiredGroupFields.concat(' managers');
|
||||
const group = await Group.getGroup({ user, groupId: task.group.id, fields });
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (canNotEditTasks(group, user)) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
}
|
||||
const group = await getGroupFromTaskAndUser(task, user);
|
||||
const challenge = await getChallengeFromTask(task);
|
||||
verifyTaskModification(task, user, group, challenge, res);
|
||||
|
||||
if (task.group.id && !task.userId) {
|
||||
await group.removeTask(task);
|
||||
|
||||
// If the task belongs to a challenge make sure the user has rights
|
||||
} else if (task.challenge.id && !task.userId) {
|
||||
challenge = await Challenge.findOne({ _id: task.challenge.id }).exec();
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
if (!challenge.canModify(user)) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||
|
||||
// If the task is owned by a user make it's the current one
|
||||
} else if (task.userId !== user._id) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.userId && task.challenge.id && !task.challenge.broken) {
|
||||
throw new NotAuthorized(res.t('cantDeleteChallengeTasks'));
|
||||
} else if (
|
||||
|
||||
@@ -8,10 +8,13 @@ import {
|
||||
NotAuthorized,
|
||||
} from '../../../libs/errors';
|
||||
import {
|
||||
canNotEditTasks,
|
||||
createTasks,
|
||||
getTasks,
|
||||
} from '../../../libs/tasks';
|
||||
import {
|
||||
moveTask,
|
||||
} from '../../../libs/taskManager';
|
||||
} from '../../../libs/tasks/utils';
|
||||
import { handleSharedCompletion } from '../../../libs/groupTasks';
|
||||
import apiError from '../../../libs/apiError';
|
||||
import logger from '../../../libs/logger';
|
||||
@@ -22,14 +25,6 @@ const types = Tasks.tasksTypes.map(type => `${type}s`);
|
||||
// _allCompletedTodos is currently in BETA and is likely to be removed in future
|
||||
types.push('completedTodos', '_allCompletedTodos');
|
||||
|
||||
// @TODO abstract this snipped (also see api-v3/tasks.js)
|
||||
function canNotEditTasks (group, user, assignedUserId) {
|
||||
const isNotGroupLeader = group.leader !== user._id;
|
||||
const isManager = Boolean(group.managers[user._id]);
|
||||
const userIsAssigningToSelf = Boolean(assignedUserId && user._id === assignedUserId);
|
||||
return isNotGroupLeader && !isManager && !userIsAssigningToSelf;
|
||||
}
|
||||
|
||||
const api = {};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import _ from 'lodash';
|
||||
import { authWithHeaders } from '../../middlewares/auth';
|
||||
import { scoreTasks } from '../../libs/taskManager';
|
||||
import { scoreTasks } from '../../libs/tasks';
|
||||
|
||||
const api = {};
|
||||
|
||||
|
||||
@@ -1,73 +1,29 @@
|
||||
import moment from 'moment';
|
||||
import _ from 'lodash';
|
||||
import validator from 'validator';
|
||||
import * as Tasks from '../models/task';
|
||||
import apiError from './apiError';
|
||||
import {
|
||||
setNextDue,
|
||||
validateTaskAlias,
|
||||
requiredGroupFields,
|
||||
} from './utils';
|
||||
import { model as Challenge } from '../../models/challenge';
|
||||
import { model as Group } from '../../models/group';
|
||||
import { model as User } from '../../models/user';
|
||||
import * as Tasks from '../../models/task';
|
||||
import apiError from '../apiError';
|
||||
import {
|
||||
BadRequest,
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
} from './errors';
|
||||
NotAuthorized,
|
||||
} from '../errors';
|
||||
import {
|
||||
SHARED_COMPLETION,
|
||||
handleSharedCompletion,
|
||||
} from './groupTasks';
|
||||
import shared from '../../common';
|
||||
import { model as Group } from '../models/group'; // eslint-disable-line import/no-cycle
|
||||
import { model as User } from '../models/user'; // eslint-disable-line import/no-cycle
|
||||
import { taskScoredWebhook } from './webhook'; // eslint-disable-line import/no-cycle
|
||||
} from '../groupTasks';
|
||||
import shared from '../../../common';
|
||||
import { taskScoredWebhook } from '../webhook';
|
||||
|
||||
import logger from './logger';
|
||||
|
||||
const requiredGroupFields = '_id leader tasksOrder name';
|
||||
|
||||
async function _validateTaskAlias (tasks, res) {
|
||||
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
|
||||
// lengths are different, some name was duplicated
|
||||
if (aliases.length !== [...new Set(aliases)].length) {
|
||||
throw new BadRequest(res.t('taskAliasAlreadyUsed'));
|
||||
}
|
||||
|
||||
await Promise.all(tasksWithAliases.map(task => task.validate()));
|
||||
}
|
||||
|
||||
export function setNextDue (task, user, dueDateOption) {
|
||||
if (task.type !== 'daily') return;
|
||||
|
||||
let now = moment().toDate();
|
||||
let dateTaskIsDue = Date.now();
|
||||
if (dueDateOption) {
|
||||
// @TODO Add required ISO format
|
||||
dateTaskIsDue = moment(dueDateOption);
|
||||
|
||||
// If not time is supplied. Let's assume we want start of Custom Day Start day.
|
||||
if (
|
||||
dateTaskIsDue.hour() === 0
|
||||
&& dateTaskIsDue.minute() === 0
|
||||
&& dateTaskIsDue.second() === 0
|
||||
&& dateTaskIsDue.millisecond() === 0
|
||||
) {
|
||||
dateTaskIsDue.add(user.preferences.timezoneOffset, 'minutes');
|
||||
dateTaskIsDue.add(user.preferences.dayStart, 'hours');
|
||||
}
|
||||
|
||||
now = dateTaskIsDue;
|
||||
}
|
||||
|
||||
const optionsForShouldDo = user.preferences.toObject();
|
||||
optionsForShouldDo.now = now;
|
||||
task.isDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
|
||||
optionsForShouldDo.nextDue = true;
|
||||
const nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
if (nextDue && nextDue.length > 0) {
|
||||
task.nextDue = nextDue.map(dueDate => dueDate.toISOString());
|
||||
}
|
||||
}
|
||||
import logger from '../logger';
|
||||
|
||||
/**
|
||||
* Creates tasks for a user, challenge or group.
|
||||
@@ -81,7 +37,7 @@ export function setNextDue (task, user, dueDateOption) {
|
||||
* @param options.requiresApproval A boolean stating if the task will require approval
|
||||
* @return The created tasks
|
||||
*/
|
||||
export async function createTasks (req, res, options = {}) {
|
||||
async function createTasks (req, res, options = {}) {
|
||||
const {
|
||||
user,
|
||||
challenge,
|
||||
@@ -161,7 +117,7 @@ export async function createTasks (req, res, options = {}) {
|
||||
await owner.update(taskOrderUpdateQuery).exec();
|
||||
|
||||
// tasks with aliases need to be validated asynchronously
|
||||
await _validateTaskAlias(toSave, res);
|
||||
await validateTaskAlias(toSave, res);
|
||||
|
||||
// If all tasks are valid (this is why it's not in the previous .map()),
|
||||
// save everything, withough running validation again
|
||||
@@ -188,7 +144,7 @@ export async function createTasks (req, res, options = {}) {
|
||||
* @param options.dueDate The date to use for computing the nextDue field for each returned task
|
||||
* @return The tasks found
|
||||
*/
|
||||
export async function getTasks (req, res, options = {}) {
|
||||
async function getTasks (req, res, options = {}) {
|
||||
const {
|
||||
user,
|
||||
challenge,
|
||||
@@ -253,63 +209,72 @@ export async function getTasks (req, res, options = {}) {
|
||||
}
|
||||
|
||||
// Order tasks based on tasksOrder
|
||||
let order = [];
|
||||
if (type && type !== 'completedTodos' && type !== '_allCompletedTodos') {
|
||||
const order = owner.tasksOrder[type];
|
||||
let orderedTasks = new Array(tasks.length);
|
||||
const unorderedTasks = []; // what we want to add later
|
||||
|
||||
tasks.forEach((task, index) => {
|
||||
const taskId = task._id;
|
||||
const i = order[index] === taskId ? index : order.indexOf(taskId);
|
||||
if (i === -1) {
|
||||
unorderedTasks.push(task);
|
||||
} else {
|
||||
orderedTasks[i] = task;
|
||||
}
|
||||
order = owner.tasksOrder[type];
|
||||
} else if (!type) {
|
||||
Object.values(owner.tasksOrder).forEach(taskOrder => {
|
||||
order = order.concat(taskOrder);
|
||||
});
|
||||
|
||||
// Remove empty values from the array and add any unordered task
|
||||
orderedTasks = _.compact(orderedTasks).concat(unorderedTasks);
|
||||
return orderedTasks;
|
||||
}
|
||||
return tasks;
|
||||
}
|
||||
|
||||
// Takes a Task document and return a plain object of attributes that can be synced to the user
|
||||
export function syncableAttrs (task) {
|
||||
const t = task.toObject(); // lodash doesn't seem to like _.omit on Document
|
||||
// only sync/compare important attrs
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves a task to a specified position.
|
||||
*
|
||||
* @param order The list of ordered tasks
|
||||
* @param taskId The Task._id of the task to move
|
||||
* @param to A integer specifying the index to move the task to
|
||||
*
|
||||
* @return Empty
|
||||
*/
|
||||
export function moveTask (order, taskId, to) {
|
||||
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
|
||||
// or if the task is moved to position -1 (push to bottom)
|
||||
// -> push task at end of list
|
||||
if (!order[to] && to !== -1) {
|
||||
order.push(taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentIndex !== -1) order.splice(currentIndex, 1);
|
||||
if (to === -1) {
|
||||
order.push(taskId);
|
||||
} else {
|
||||
order.splice(to, 0, taskId);
|
||||
return tasks;
|
||||
}
|
||||
|
||||
let orderedTasks = new Array(tasks.length);
|
||||
const unorderedTasks = []; // what we want to add later
|
||||
|
||||
tasks.forEach((task, index) => {
|
||||
const taskId = task._id;
|
||||
const i = order[index] === taskId ? index : order.indexOf(taskId);
|
||||
if (i === -1) {
|
||||
unorderedTasks.push(task);
|
||||
} else {
|
||||
orderedTasks[i] = task;
|
||||
}
|
||||
});
|
||||
|
||||
// Remove empty values from the array and add any unordered task
|
||||
orderedTasks = _.compact(orderedTasks).concat(unorderedTasks);
|
||||
return orderedTasks;
|
||||
}
|
||||
|
||||
function canNotEditTasks (group, user, assignedUserId) {
|
||||
const isNotGroupLeader = group.leader !== user._id;
|
||||
const isManager = Boolean(group.managers[user._id]);
|
||||
const userIsAssigningToSelf = Boolean(assignedUserId && user._id === assignedUserId);
|
||||
return isNotGroupLeader && !isManager && !userIsAssigningToSelf;
|
||||
}
|
||||
|
||||
async function getGroupFromTaskAndUser (task, user) {
|
||||
if (task.group.id && !task.userId) {
|
||||
const fields = requiredGroupFields.concat(' managers');
|
||||
return Group.getGroup({ user, groupId: task.group.id, fields });
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getChallengeFromTask (task) {
|
||||
if (task.challenge.id && !task.userId) {
|
||||
return Challenge.findOne({ _id: task.challenge.id }).exec();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function verifyTaskModification (task, user, group, challenge, res) {
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
} else if (task.group.id && !task.userId) {
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (canNotEditTasks(group, user)) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
|
||||
// If the task belongs to a challenge make sure the user has rights
|
||||
} else if (task.challenge.id && !task.userId) {
|
||||
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
|
||||
if (!challenge.canModify(user)) throw new NotAuthorized(res.t('onlyChalLeaderEditTasks'));
|
||||
|
||||
// If the task is owned by a user make it's the current one
|
||||
} else if (task.userId !== user._id) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -589,3 +554,13 @@ export async function scoreTasks (user, taskScorings, req, res) {
|
||||
return { id: data.task._id, delta: data.delta, _tmp: data._tmp };
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
createTasks,
|
||||
getTasks,
|
||||
scoreTask,
|
||||
canNotEditTasks,
|
||||
getGroupFromTaskAndUser,
|
||||
getChallengeFromTask,
|
||||
verifyTaskModification,
|
||||
};
|
||||
@@ -0,0 +1,94 @@
|
||||
import moment from 'moment';
|
||||
import _ from 'lodash';
|
||||
import {
|
||||
BadRequest,
|
||||
} from '../errors';
|
||||
import shared from '../../../common';
|
||||
|
||||
export const requiredGroupFields = '_id leader tasksOrder name';
|
||||
|
||||
export async function validateTaskAlias (tasks, res) {
|
||||
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
|
||||
// lengths are different, some name was duplicated
|
||||
if (aliases.length !== [...new Set(aliases)].length) {
|
||||
throw new BadRequest(res.t('taskAliasAlreadyUsed'));
|
||||
}
|
||||
|
||||
await Promise.all(tasksWithAliases.map(task => task.validate()));
|
||||
}
|
||||
|
||||
// Takes a Task document and return a plain object of attributes that can be synced to the user
|
||||
export function syncableAttrs (task) {
|
||||
const t = task.toObject(); // lodash doesn't seem to like _.omit on Document
|
||||
// only sync/compare important attrs
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves a task to a specified position.
|
||||
*
|
||||
* @param order The list of ordered tasks
|
||||
* @param taskId The Task._id of the task to move
|
||||
* @param to A integer specifying the index to move the task to
|
||||
*
|
||||
* @return Empty
|
||||
*/
|
||||
export function moveTask (order, taskId, to) {
|
||||
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
|
||||
// or if the task is moved to position -1 (push to bottom)
|
||||
// -> push task at end of list
|
||||
if (!order[to] && to !== -1) {
|
||||
order.push(taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentIndex !== -1) order.splice(currentIndex, 1);
|
||||
if (to === -1) {
|
||||
order.push(taskId);
|
||||
} else {
|
||||
order.splice(to, 0, taskId);
|
||||
}
|
||||
}
|
||||
|
||||
export function setNextDue (task, user, dueDateOption) {
|
||||
if (task.type !== 'daily') return;
|
||||
|
||||
let now = moment().toDate();
|
||||
let dateTaskIsDue = Date.now();
|
||||
if (dueDateOption) {
|
||||
// @TODO Add required ISO format
|
||||
dateTaskIsDue = moment(dueDateOption);
|
||||
|
||||
// If not time is supplied. Let's assume we want start of Custom Day Start day.
|
||||
if (
|
||||
dateTaskIsDue.hour() === 0
|
||||
&& dateTaskIsDue.minute() === 0
|
||||
&& dateTaskIsDue.second() === 0
|
||||
&& dateTaskIsDue.millisecond() === 0
|
||||
) {
|
||||
dateTaskIsDue.add(user.preferences.timezoneOffset, 'minutes');
|
||||
dateTaskIsDue.add(user.preferences.dayStart, 'hours');
|
||||
}
|
||||
|
||||
now = dateTaskIsDue;
|
||||
}
|
||||
|
||||
const optionsForShouldDo = user.preferences.toObject();
|
||||
optionsForShouldDo.now = now;
|
||||
task.isDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
|
||||
optionsForShouldDo.nextDue = true;
|
||||
const nextDue = shared.shouldDo(dateTaskIsDue, task, optionsForShouldDo);
|
||||
if (nextDue && nextDue.length > 0) {
|
||||
task.nextDue = nextDue.map(dueDate => dueDate.toISOString());
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,7 @@ import { removeFromArray } from '../libs/collectionManipulators';
|
||||
import shared from '../../common';
|
||||
import { sendTxn as txnEmail } from '../libs/email'; // eslint-disable-line import/no-cycle
|
||||
import { sendNotification as sendPushNotification } from '../libs/pushNotifications'; // eslint-disable-line import/no-cycle
|
||||
import { // eslint-disable-line import/no-cycle
|
||||
syncableAttrs,
|
||||
setNextDue,
|
||||
} from '../libs/taskManager';
|
||||
import { syncableAttrs, setNextDue } from '../libs/tasks/utils';
|
||||
|
||||
const { Schema } = mongoose;
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import { sendTxn as sendTxnEmail } from '../libs/email'; // eslint-disable-line
|
||||
import { sendNotification as sendPushNotification } from '../libs/pushNotifications'; // eslint-disable-line import/no-cycle
|
||||
import { // eslint-disable-line import/no-cycle
|
||||
syncableAttrs,
|
||||
} from '../libs/taskManager';
|
||||
} from '../libs/tasks/utils';
|
||||
import {
|
||||
schema as SubscriptionPlanSchema,
|
||||
} from './subscriptionPlan';
|
||||
|
||||
Reference in New Issue
Block a user