2015-11-28 16:47:32 +00:00
|
|
|
import { authWithHeaders } from '../../middlewares/api-v3/auth';
|
2015-11-28 17:05:07 +00:00
|
|
|
import * as Tasks from '../../models/task';
|
2015-11-29 17:29:35 +00:00
|
|
|
import {
|
|
|
|
|
NotFound,
|
|
|
|
|
NotAuthorized,
|
2015-11-30 16:25:40 +00:00
|
|
|
BadRequest,
|
2015-11-29 17:29:35 +00:00
|
|
|
} from '../../libs/api-v3/errors';
|
2015-11-28 17:18:28 +00:00
|
|
|
import Q from 'q';
|
2015-11-29 18:05:24 +00:00
|
|
|
import _ from 'lodash';
|
2015-11-28 16:47:32 +00:00
|
|
|
|
|
|
|
|
let api = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {post} /tasks Create a new task
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName CreateTask
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {Object} task The newly created task
|
|
|
|
|
*/
|
|
|
|
|
api.createTask = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/tasks',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
2015-11-30 18:38:53 +00:00
|
|
|
req.checkBody('type', res.t('invalidTaskType')).notEmpty().isIn(Tasks.tasksTypes);
|
2015-11-28 16:47:32 +00:00
|
|
|
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
let taskType = req.body.type;
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
let newTask = new Tasks[taskType](Tasks.Task.sanitize(req.body));
|
2015-11-28 16:47:32 +00:00
|
|
|
newTask.userId = user._id;
|
|
|
|
|
|
2015-11-28 17:18:28 +00:00
|
|
|
user.tasksOrder[taskType].unshift(newTask._id);
|
|
|
|
|
|
|
|
|
|
Q.all([
|
|
|
|
|
newTask.save(),
|
|
|
|
|
user.save(),
|
|
|
|
|
])
|
2015-11-29 18:05:24 +00:00
|
|
|
.then(([task]) => res.respond(201, task))
|
|
|
|
|
.catch(next);
|
2015-11-28 16:47:32 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {get} /tasks Get an user's tasks
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName GetTasks
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {string="habit","daily","todo","reward"} type Optional queyr parameter to return just a type of tasks
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {Array} tasks An array of task objects
|
|
|
|
|
*/
|
|
|
|
|
api.getTasks = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: '/tasks',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
2015-11-30 18:38:53 +00:00
|
|
|
req.checkQuery('type', res.t('invalidTaskType')).isIn(Tasks.tasksTypes);
|
2015-11-28 16:47:32 +00:00
|
|
|
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
let query = {userId: user._id};
|
|
|
|
|
let type = req.query.type;
|
2015-11-30 18:38:53 +00:00
|
|
|
if (type) query.type = type;
|
2015-11-28 16:47:32 +00:00
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.find(query).exec()
|
2015-11-28 16:47:32 +00:00
|
|
|
.then((tasks) => res.respond(200, tasks))
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {get} /task/:taskId Get a task given its id
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName GetTask
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} task The task object
|
|
|
|
|
*/
|
|
|
|
|
api.getTask = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: '/tasks/:taskId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-28 16:47:32 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
|
|
|
|
res.respond(200, task);
|
|
|
|
|
})
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
2015-11-29 16:47:10 +00:00
|
|
|
|
2015-11-29 18:05:24 +00:00
|
|
|
/**
|
|
|
|
|
* @api {put} /task/:taskId Update a task
|
|
|
|
|
* @apiVersion 3.0.0
|
2015-11-30 16:25:40 +00:00
|
|
|
* @apiName UpdateTask
|
2015-11-29 18:05:24 +00:00
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} task The updated task
|
|
|
|
|
*/
|
|
|
|
|
api.updateTask = {
|
2015-11-30 16:25:40 +00:00
|
|
|
method: 'PUT',
|
2015-11-29 18:05:24 +00:00
|
|
|
url: '/tasks/:taskId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
2015-11-30 16:25:40 +00:00
|
|
|
|
2015-11-29 18:05:24 +00:00
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
2015-11-30 16:25:40 +00:00
|
|
|
// TODO check that req.body isn't empty
|
2015-11-29 18:05:24 +00:00
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-29 18:05:24 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
2015-11-30 16:25:40 +00:00
|
|
|
|
|
|
|
|
// If checklist is updated -> replace the original one
|
|
|
|
|
if (req.body.checklist) {
|
|
|
|
|
delete req.body.checklist;
|
|
|
|
|
task.checklist = req.body.checklist;
|
|
|
|
|
}
|
2015-11-29 18:05:24 +00:00
|
|
|
// TODO merge goes deep into objects, it's ok?
|
|
|
|
|
// TODO also check that array fields are updated correctly without marking modified
|
2015-11-30 18:38:53 +00:00
|
|
|
_.merge(task, Tasks.Task.sanitizeUpdate(req.body));
|
2015-11-29 18:05:24 +00:00
|
|
|
return task.save();
|
|
|
|
|
})
|
|
|
|
|
.then((savedTask) => res.respond(200, savedTask))
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-30 16:25:40 +00:00
|
|
|
/**
|
|
|
|
|
* @api {post} /tasks/:taskId/checklist/addItem Add an item to a checklist, creating the checklist if it doesn't exist
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName AddChecklistItem
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} task The updated task
|
|
|
|
|
*/
|
|
|
|
|
api.addChecklistItem = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/tasks/:taskId/checklist/addItem',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
|
|
|
|
// TODO check that req.body isn't empty and is an array
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-30 16:25:40 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
2015-11-30 18:38:53 +00:00
|
|
|
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
2015-11-30 16:25:40 +00:00
|
|
|
|
|
|
|
|
task.checklist.push(req.body);
|
|
|
|
|
return task.save();
|
|
|
|
|
})
|
|
|
|
|
.then((savedTask) => res.respond(200, savedTask)) // TODO what to return
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {post} /tasks/:taskId/checklist/:itemId/score Score a checklist item
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName ScoreChecklistItem
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
* @apiParam {UUID} itemId The checklist item _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} task The updated task
|
|
|
|
|
*/
|
|
|
|
|
api.scoreCheckListItem = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/tasks/:taskId/checklist/:itemId/score',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
|
|
|
|
req.checkParams('itemId', res.t('itemIdRequired')).notEmpty().isUUID();
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-30 16:25:40 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
2015-11-30 18:38:53 +00:00
|
|
|
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
2015-11-30 16:25:40 +00:00
|
|
|
|
|
|
|
|
let item = _.find(task.checklist, {_id: req.params.itemId});
|
|
|
|
|
|
|
|
|
|
if (!item) throw new NotFound(res.t('checklistItemNotFound'));
|
|
|
|
|
item.completed = !item.completed;
|
|
|
|
|
return task.save();
|
|
|
|
|
})
|
|
|
|
|
.then((savedTask) => res.respond(200, savedTask)) // TODO what to return
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {put} /tasks/:taskId/checklist/:itemId Update a checklist item
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName UpdateChecklistItem
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
* @apiParam {UUID} itemId The checklist item _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} task The updated task
|
|
|
|
|
*/
|
|
|
|
|
api.updateChecklistItem = {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
url: '/tasks/:taskId/checklist/:itemId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
|
|
|
|
req.checkParams('itemId', res.t('itemIdRequired')).notEmpty().isUUID();
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-30 16:25:40 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
2015-11-30 18:38:53 +00:00
|
|
|
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
2015-11-30 16:25:40 +00:00
|
|
|
|
|
|
|
|
let item = _.find(task.checklist, {_id: req.params.itemId});
|
|
|
|
|
if (!item) throw new NotFound(res.t('checklistItemNotFound'));
|
|
|
|
|
|
|
|
|
|
delete req.body.id; // Simple sanitization to prevent the ID to be changed
|
|
|
|
|
_.merge(item, req.body);
|
|
|
|
|
return task.save();
|
|
|
|
|
})
|
|
|
|
|
.then((savedTask) => res.respond(200, savedTask)) // TODO what to return
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api {delete} /tasks/:taskId/checklist/:itemId Remove a checklist item
|
|
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName RemoveChecklistItem
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
* @apiParam {UUID} itemId The checklist item _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} empty An empty object
|
|
|
|
|
*/
|
|
|
|
|
api.removeChecklistItem = {
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
url: '/tasks/:taskId/checklist/:itemId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
|
|
|
|
req.checkParams('itemId', res.t('itemIdRequired')).notEmpty().isUUID();
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-30 16:25:40 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
2015-11-30 18:38:53 +00:00
|
|
|
if (task.type !== 'daily' && task.type !== 'todo') throw new BadRequest(res.t('checklistOnlyDailyTodo'));
|
2015-11-30 16:25:40 +00:00
|
|
|
|
|
|
|
|
let itemI = _.findIndex(task.checklist, {_id: req.params.itemId});
|
|
|
|
|
if (itemI === -1) throw new NotFound(res.t('checklistItemNotFound'));
|
|
|
|
|
|
|
|
|
|
task.checklist.splice(itemI, 1);
|
|
|
|
|
return task.save();
|
|
|
|
|
})
|
|
|
|
|
.then(() => res.respond(200, {})) // TODO what to return
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-29 16:47:10 +00:00
|
|
|
// Remove a task from user.tasksOrder
|
|
|
|
|
function _removeTaskTasksOrder (user, taskId) {
|
|
|
|
|
// Loop through all lists and when the task is found, remove it and return
|
2015-11-30 18:38:53 +00:00
|
|
|
for (let i = 0; i < Tasks.tasksTypes.length; i++) {
|
|
|
|
|
let list = user.tasksOrder[Tasks.tasksTypes[i]];
|
2015-11-29 16:47:10 +00:00
|
|
|
let index = list.indexOf(taskId);
|
|
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
list.splice(index, 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-29 17:29:35 +00:00
|
|
|
* @api {delete} /task/:taskId Delete a user task given its id
|
2015-11-29 16:47:10 +00:00
|
|
|
* @apiVersion 3.0.0
|
|
|
|
|
* @apiName DeleteTask
|
|
|
|
|
* @apiGroup Task
|
|
|
|
|
*
|
|
|
|
|
* @apiParam {UUID} taskId The task _id
|
|
|
|
|
*
|
|
|
|
|
* @apiSuccess {object} empty An empty object
|
|
|
|
|
*/
|
|
|
|
|
api.deleteTask = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: '/tasks/:taskId',
|
|
|
|
|
middlewares: [authWithHeaders()],
|
|
|
|
|
handler (req, res, next) {
|
|
|
|
|
let user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
|
|
|
|
|
2015-11-30 18:38:53 +00:00
|
|
|
Tasks.Task.findOne({
|
2015-11-29 17:29:35 +00:00
|
|
|
_id: req.params.taskId,
|
|
|
|
|
userId: user._id,
|
|
|
|
|
}).exec()
|
|
|
|
|
.then((task) => {
|
|
|
|
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
|
|
|
|
if (task.challenge.id) throw new NotAuthorized(res.t('cantDeleteChallengeTasks'));
|
|
|
|
|
|
|
|
|
|
_removeTaskTasksOrder(user, req.params.taskId);
|
|
|
|
|
return Q.all([
|
|
|
|
|
user.save(),
|
|
|
|
|
task.remove(),
|
|
|
|
|
]);
|
|
|
|
|
})
|
2015-11-29 16:47:10 +00:00
|
|
|
.then(() => res.respond(200, {}))
|
|
|
|
|
.catch(next);
|
|
|
|
|
},
|
|
|
|
|
};
|
2015-11-28 16:47:32 +00:00
|
|
|
|
|
|
|
|
export default api;
|