fix dateCompleted not remove from un-completed todos and fix tasks test

This commit is contained in:
Matteo Pagliazzi
2016-01-18 15:01:46 +01:00
parent d93cabafb9
commit ee854229c7
5 changed files with 5 additions and 65 deletions

View File

@@ -224,7 +224,7 @@ export default function scoreTask (options = {}, req = {}) {
if (cron) { // don't touch stats on cron if (cron) { // don't touch stats on cron
delta += _changeTaskValue(user, task, direction, times, cron); delta += _changeTaskValue(user, task, direction, times, cron);
} else { } else {
if (direction === 'up') task.dateCompleted = new Date(); task.dateCompleted = direction === 'up' ? new Date() : undefined;
delta += _changeTaskValue(user, task, direction, times, cron); delta += _changeTaskValue(user, task, direction, times, cron);
if (direction === 'down') delta = _calculateDelta(task, direction, delta); // recalculate delta for unchecking so the gp and exp come out correctly if (direction === 'down') delta = _calculateDelta(task, direction, delta); // recalculate delta for unchecking so the gp and exp come out correctly

View File

@@ -46,6 +46,7 @@ describe('POST /tasks/:id/score/:direction', () => {
let task = await user.get(`/tasks/${todo._id}`); let task = await user.get(`/tasks/${todo._id}`);
expect(task.completed).to.equal(true); expect(task.completed).to.equal(true);
expect(task.dateCompleted).to.be.a('string'); // date gets converted to a string as json doesn't have a Date type
}); });
it('moves completed todos out of user.tasksOrder.todos', async () => { it('moves completed todos out of user.tasksOrder.todos', async () => {
@@ -81,6 +82,7 @@ describe('POST /tasks/:id/score/:direction', () => {
let updatedTask = await user.get(`/tasks/${todo._id}`); let updatedTask = await user.get(`/tasks/${todo._id}`);
expect(updatedTask.completed).to.equal(false); expect(updatedTask.completed).to.equal(false);
expect(updatedTask.dateCompleted).to.be.a('undefined');
}); });
it('scores up todo even if it is already completed'); // Yes? it('scores up todo even if it is already completed'); // Yes?

View File

@@ -95,9 +95,6 @@ describe('POST /tasks/user', () => {
expect(updatedTasks).to.eql(originalTasks); expect(updatedTasks).to.eql(originalTasks);
}); });
let updatedTasks = await user.get('/tasks');
expect(updatedTasks).to.eql(originalTasks);
}); });
it('automatically sets "task.userId" to user\'s uuid', async () => { it('automatically sets "task.userId" to user\'s uuid', async () => {

View File

@@ -153,7 +153,7 @@ async function _getTasks (req, res, user, challenge) {
* @apiGroup Task * @apiGroup Task
* *
* @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks * @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks
* @apiParam {boolean} includeCompletedTodos Optional query parameter to include completed todos when "type" is "todo". Only valid whe "tasksOwner" is "user". * @apiParam {boolean} includeCompletedTodos Optional query parameter to include completed todos when "type" is "todo".
* *
* @apiSuccess {Array} tasks An array of task objects * @apiSuccess {Array} tasks An array of task objects
*/ */
@@ -178,7 +178,6 @@ api.getUserTasks = {
* @apiGroup Task * @apiGroup Task
* *
* @apiParam {UUID} challengeId The id of the challenge from which to retrieve the tasks. * @apiParam {UUID} challengeId The id of the challenge from which to retrieve the tasks.
*
* @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks * @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks
* *
* @apiSuccess {Array} tasks An array of task objects * @apiSuccess {Array} tasks An array of task objects
@@ -208,64 +207,6 @@ api.getChallengeTasks = {
}, },
}; };
/**
* @api {get} /tasks/:tasksOwner/:challengeId Get an user's tasks
* @apiVersion 3.0.0
* @apiName GetTasks
* @apiGroup Task
*
* @apiParam {string="user","challenge"} tasksOwner Query parameter to return tasks belonging to a challenge (specifying the "challengeId" parameter) or to the autheticated user.
* @apiParam {UUID} challengeId Optional query parameter. If "tasksOwner" is "challenge" then required to specify the challenge id.
* @apiParam {string="habit","daily","todo","reward"} type Optional query parameter to return just a type of tasks
* @apiParam {boolean} includeCompletedTodos Optional query parameter to include completed todos when "type" is "todo". Only valid whe "tasksOwner" is "user".
*
* @apiSuccess {Array} tasks An array of task objects
*/
api.getTasks = {
method: 'GET',
url: '/tasks',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
let user = res.locals.user;
let challengeId = req.query.challengeId;
let challenge;
let query = challenge ? {'challenge.id': challengeId, userId: {$exists: false}} : {userId: user._id};
let type = req.query.type;
if (type) {
query.type = type;
if (type === 'todo') query.completed = false; // Exclude completed todos
} else {
query.$or = [ // Exclude completed todos
{type: 'todo', completed: false},
{type: {$in: ['habit', 'daily', 'reward']}},
];
}
if (req.query.includeCompletedTodos === 'true' && (!type || type === 'todo')) {
if (challengeId) throw new BadRequest(res.t('noCompletedTodosChallenge'));
let queryCompleted = Tasks.Task.find({
type: 'todo',
completed: true,
}).limit(30).sort({ // TODO add ability to pick more than 30 completed todos
dateCompleted: 1,
});
let results = await Q.all([
queryCompleted.exec(),
Tasks.Task.find(query).exec(),
]);
res.respond(200, results[1].concat(results[0]));
} else {
let tasks = await Tasks.Task.find(query).exec();
res.respond(200, tasks);
}
},
};
/** /**
* @api {get} /task/:taskId Get a task given its id * @api {get} /task/:taskId Get a task given its id
* @apiVersion 3.0.0 * @apiVersion 3.0.0

View File

@@ -492,7 +492,7 @@ schema.methods.leave = function leaveGroup (user, keep) {
if (group.type === 'guild') { if (group.type === 'guild') {
_.pull(user.guilds, group._id); _.pull(user.guilds, group._id);
} else { } else {
user.party._id = undefined; user.party._id = undefined; // TODO remove quest information too?
} }
// If the leader is leaving (or if the leader previously left, and this wasn't accounted for) // If the leader is leaving (or if the leader previously left, and this wasn't accounted for)