mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
misc fixes, add createTask route and ability to get tasks by type to getTasks route
This commit is contained in:
@@ -83,7 +83,7 @@
|
|||||||
"max-nested-callbacks": [2, 3],
|
"max-nested-callbacks": [2, 3],
|
||||||
"new-cap": 2,
|
"new-cap": 2,
|
||||||
"new-parens": 2,
|
"new-parens": 2,
|
||||||
"newline-after-var": 2,
|
"newline-after-var": 0,
|
||||||
"no-array-constructor": 2,
|
"no-array-constructor": 2,
|
||||||
"no-continue": 2,
|
"no-continue": 2,
|
||||||
"no-lonely-if": 2,
|
"no-lonely-if": 2,
|
||||||
|
|||||||
@@ -14,5 +14,8 @@
|
|||||||
"onlyFbSupported": "Only Facebook supported currently.",
|
"onlyFbSupported": "Only Facebook supported currently.",
|
||||||
"cantDetachFb": "Account lacks another authentication method, can't detach Facebook.",
|
"cantDetachFb": "Account lacks another authentication method, can't detach Facebook.",
|
||||||
"onlySocialAttachLocal": "Local auth can only be added to a social account.",
|
"onlySocialAttachLocal": "Local auth can only be added to a social account.",
|
||||||
"invalidReqParams": "Invalid request parameters."
|
"invalidReqParams": "Invalid request parameters.",
|
||||||
|
"taskIdRequired": "\"taskId\" must be a valid UUID",
|
||||||
|
"taskNotFound": "Task not found.",
|
||||||
|
"invalidTaskType": "Task type must be one of \"habit\", \"daily\", \"todo\", \"reward\"."
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ api.loginSocial = {
|
|||||||
* @apiName UserDeleteSocial
|
* @apiName UserDeleteSocial
|
||||||
* @apiGroup User
|
* @apiGroup User
|
||||||
*
|
*
|
||||||
* @apiSuccess {Boolean=true} success Always true
|
* @apiSuccess {Object} response Empty object
|
||||||
*/
|
*/
|
||||||
api.deleteSocial = {
|
api.deleteSocial = {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
import { authWithHeaders } from '../../middlewares/api-v3/auth';
|
||||||
|
import * as Tasks from '../../models/tasks';
|
||||||
|
import { NotFound } from '../../libs/errors';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
req.checkBody('type', res.t('invalidTaskType')).notEmpty().isIn(['habit', 'daily', 'todo', 'reward']);
|
||||||
|
|
||||||
|
let user = res.locals.user;
|
||||||
|
let taskType = req.body.type;
|
||||||
|
|
||||||
|
let newTask = new Tasks[`${taskType.charAt(0).toUpperCase() + taskType.slice(1)}Model`](Tasks.Task.sanitize(req.body));
|
||||||
|
newTask.userId = user._id;
|
||||||
|
|
||||||
|
newTask.save()
|
||||||
|
.then((task) => res.respond(201, task))
|
||||||
|
.catch(next);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) {
|
||||||
|
req.checkQuery('type', res.t('invalidTaskType')).isIn(['habit', 'daily', 'todo', 'reward']);
|
||||||
|
|
||||||
|
let user = res.locals.user;
|
||||||
|
let query = {userId: user._id};
|
||||||
|
let type = req.query.type;
|
||||||
|
if (type) query.type = type.charAt(0).toUpperCase() + type.slice(1); // task.ype is stored with firt uppercase letter
|
||||||
|
|
||||||
|
Tasks.TaskModel.find(query).exec()
|
||||||
|
.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();
|
||||||
|
|
||||||
|
Tasks.TaskModel.findOne({
|
||||||
|
_id: req.params.taskId,
|
||||||
|
userId: user._id,
|
||||||
|
}).exec()
|
||||||
|
.then((task) => {
|
||||||
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
||||||
|
res.respond(200, task);
|
||||||
|
})
|
||||||
|
.catch(next);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// api.updateTask
|
||||||
|
// api.deleteTask
|
||||||
|
// api.score
|
||||||
|
// api.scoreChecklist
|
||||||
|
|
||||||
|
export default api;
|
||||||
|
|||||||
@@ -539,7 +539,7 @@ function _populateDefaultTasks (user, taskTypes) {
|
|||||||
|
|
||||||
function _populateDefaultsForNewUser (user) {
|
function _populateDefaultsForNewUser (user) {
|
||||||
let taskTypes;
|
let taskTypes;
|
||||||
let iterableFlags = user.toObject().flags;
|
let iterableFlags = user.flags.toObject();
|
||||||
|
|
||||||
if (user.registeredThrough === 'habitica-web') {
|
if (user.registeredThrough === 'habitica-web') {
|
||||||
taskTypes = ['habits', 'dailys', 'todos', 'rewards', 'tags'];
|
taskTypes = ['habits', 'dailys', 'todos', 'rewards', 'tags'];
|
||||||
|
|||||||
Reference in New Issue
Block a user