add clearCompletedTodos route with tests, add test for getting completed todos, misc fixes

This commit is contained in:
Matteo Pagliazzi
2016-01-25 17:23:01 +01:00
parent 4149cbf381
commit d5751837ed
5 changed files with 51 additions and 7 deletions

View File

@@ -22,6 +22,21 @@ describe('GET /tasks/user', () => {
expect(tasks[0]._id).to.equal(createdTasks[0]._id);
});
// TODO complete after task scoring is done
it('returns completed todos sorted by creation date if req.query.includeCompletedTodos is specified');
it('returns completed todos sorted by completion date if req.query.includeCompletedTodos is specified', async () => {
let todo1 = await user.post('/tasks/user', {text: 'todo to complete 1', type: 'todo'});
let todo2 = await user.post('/tasks/user', {text: 'todo to complete 2', type: 'todo'});
await user.sync();
let initialTodoCount = user.tasksOrder.todos.length;
await user.post(`/tasks/${todo2._id}/score/up`);
await user.post(`/tasks/${todo1._id}/score/up`);
await user.sync();
expect(user.tasksOrder.todos.length).to.equal(initialTodoCount - 2);
let allTodos = await user.get('/tasks/user?type=todo&includeCompletedTodos=true');
expect(allTodos.length).to.equal(initialTodoCount);
expect(allTodos[allTodos.length - 1].text).to.equal('todo to complete 1'); // last is the todo that was completed later
});
});

View File

@@ -129,6 +129,7 @@ async function _getTasks (req, res, user, challenge) {
if (challenge) throw new BadRequest(res.t('noCompletedTodosChallenge')); // no completed todos for challenges
let queryCompleted = Tasks.Task.find({
userId: user._id,
type: 'todo',
completed: true,
}).limit(30).sort({ // TODO add ability to pick more than 30 completed todos
@@ -837,7 +838,35 @@ api.unlinkTask = {
};
/**
* @api {delete} /task/:taskId Delete a user task given its id
* @api {post} /tasks/clearCompletedTodos Delete user's completed todos
* @apiVersion 3.0.0
* @apiName ClearCompletedTodos
* @apiGroup Task
*
* @apiSuccess {object} empty An empty object
*/
api.clearCompletedTodos = {
method: 'POST',
url: '/tasks/clearCompletedTodos',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
let user = res.locals.user;
// Clear completed todos
// Do not delete challenges completed todos TODO unless the task is broken?
await Tasks.Task.remove({
userId: user._id,
type: 'todo',
completed: true,
'challenge.id': {$exists: false},
}).exec();
res.respond(200, {});
},
};
/**
* @api {delete} /tasks/:taskId Delete a user task given its id
* @apiVersion 3.0.0
* @apiName DeleteTask
* @apiGroup Task

View File

@@ -34,7 +34,7 @@ export default function cronMiddleware (req, res, next) {
// Run cron
cron({user, tasksByType, now, daysMissed, analytics});
// Clean completed todos - 30 days for free users, 90 for subscribers
// Clear old completed todos - 30 days for free users, 90 for subscribers
// Do not delete challenges completed todos TODO unless the task is broken?
Task.remove({
userId: user._id,

View File

@@ -79,7 +79,7 @@ schema.plugin(baseModel, {
// A list of additional fields that cannot be updated (but can be set on creation)
let noUpdate = ['privacy', 'type'];
schema.statics.sanitizeUpdate = function sanitizeUpdate (updateObj) {
return model.sanitize(updateObj, noUpdate); // eslint-disable-line no-use-before-define
return this.sanitize(updateObj, noUpdate);
};
// TODO migration

View File

@@ -48,13 +48,13 @@ TaskSchema.plugin(baseModel, {
// A list of additional fields that cannot be set on creation (but can be set on updare)
let noCreate = ['completed']; // TODO completed should be removed for updates too?
TaskSchema.statics.sanitizeCreate = function sanitizeCreate (createObj) {
return Task.sanitize(createObj, noCreate); // eslint-disable-line no-use-before-define
return this.sanitize(createObj, noCreate);
};
// A list of additional fields that cannot be updated (but can be set on creation)
let noUpdate = ['_id', 'type'];
TaskSchema.statics.sanitizeUpdate = function sanitizeUpdate (updateObj) {
return Task.sanitize(updateObj, noUpdate); // eslint-disable-line no-use-before-define
return this.sanitize(updateObj, noUpdate);
};
// Sanitize checklist objects (disallowing _id)