mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Group plans reorder tasks (#8358)
* Added move route for group tasks * Added group task reorder to front end * Added syncing with group task order * Fixed linting issues * Added missing exec and abstracted move code * Added unit test for moveTask
This commit is contained in:
committed by
Matteo Pagliazzi
parent
2690caed35
commit
1590d955cd
@@ -4,12 +4,14 @@ import * as Tasks from '../../../models/task';
|
||||
import { model as Group } from '../../../models/group';
|
||||
import { model as User } from '../../../models/user';
|
||||
import {
|
||||
BadRequest,
|
||||
NotFound,
|
||||
NotAuthorized,
|
||||
} from '../../../libs/errors';
|
||||
import {
|
||||
createTasks,
|
||||
getTasks,
|
||||
moveTask,
|
||||
} from '../../../libs/taskManager';
|
||||
|
||||
let requiredGroupFields = '_id leader tasksOrder name';
|
||||
@@ -80,6 +82,58 @@ api.getGroupTasks = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /api/v3/group/:groupId/tasks/:taskId/move/to/:position Move a group task to a specified position
|
||||
* @apiDescription Moves a group task to a specified position
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName GroupMoveTask
|
||||
* @apiGroup Task
|
||||
*
|
||||
* @apiParam {String} taskId The task _id
|
||||
* @apiParam {Number} position Query parameter - Where to move the task (-1 means push to bottom). First position is 0
|
||||
*
|
||||
* @apiSuccess {Array} data The new tasks order (group.tasksOrder.{task.type}s)
|
||||
*/
|
||||
api.groupMoveTask = {
|
||||
method: 'POST',
|
||||
url: '/group-tasks/:taskId/move/to/:position',
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty();
|
||||
req.checkParams('position', res.t('positionRequired')).notEmpty().isNumeric();
|
||||
|
||||
let reqValidationErrors = req.validationErrors();
|
||||
if (reqValidationErrors) throw reqValidationErrors;
|
||||
|
||||
let user = res.locals.user;
|
||||
|
||||
let taskId = req.params.taskId;
|
||||
let task = await Tasks.Task.findOne({
|
||||
_id: taskId,
|
||||
}).exec();
|
||||
|
||||
let to = Number(req.params.position);
|
||||
|
||||
if (!task) {
|
||||
throw new NotFound(res.t('taskNotFound'));
|
||||
}
|
||||
|
||||
if (task.type === 'todo' && task.completed) throw new BadRequest(res.t('cantMoveCompletedTodo'));
|
||||
|
||||
let group = await Group.getGroup({user, groupId: task.group.id, fields: requiredGroupFields});
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
|
||||
if (group.leader !== user._id) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||
|
||||
let order = group.tasksOrder[`${task.type}s`];
|
||||
|
||||
moveTask(order, task._id, to);
|
||||
|
||||
await group.save();
|
||||
res.respond(200, order);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /api/v3/tasks/:taskId/assign/:assignedUserId Assign a group task to a user
|
||||
* @apiDescription Assigns a user to a group task
|
||||
|
||||
Reference in New Issue
Block a user