diff --git a/test/api/v3/integration/tasks/POST-tasks_clearCompletedTodos.test.js b/test/api/v3/integration/tasks/POST-tasks_clearCompletedTodos.test.js index 7dfce6d6cc..7cdd7753d2 100644 --- a/test/api/v3/integration/tasks/POST-tasks_clearCompletedTodos.test.js +++ b/test/api/v3/integration/tasks/POST-tasks_clearCompletedTodos.test.js @@ -43,7 +43,7 @@ describe('POST /tasks/clearCompletedTodos', () => { let completedTodos = await user.get('/tasks/user?type=completedTodos'); let todos = await user.get('/tasks/user?type=todos'); let allTodos = todos.concat(completedTodos); - expect(allTodos.length).to.equal(initialTodoCount + 4); // + 6 - 3 completed (but one is from challenge) - expect(allTodos[allTodos.length - 1].text).to.equal('todo 7'); + expect(allTodos.length).to.equal(initialTodoCount + 5); // + 7 - 3 completed (but one is from challenge) + expect(allTodos[allTodos.length - 1].text).to.equal('todo 6'); // last completed todo }); }); diff --git a/website/server/controllers/api-v3/tasks.js b/website/server/controllers/api-v3/tasks.js index a3732fd733..7116bc2387 100644 --- a/website/server/controllers/api-v3/tasks.js +++ b/website/server/controllers/api-v3/tasks.js @@ -896,16 +896,24 @@ api.clearCompletedTodos = { let user = res.locals.user; // Clear completed todos - // Do not delete challenges completed todos unless the task is broken + // Do not delete completed todos from challenges or groups, unless the task is broken await Tasks.Task.remove({ userId: user._id, type: 'todo', completed: true, - $or: [ - {'challenge.id': {$exists: false}}, - {'challenge.broken': {$exists: true}}, - {'group.id': {$exists: false}}, - {'group.broken': {$exists: true}}, + $and: [ // exclude challenge and group tasks + { + $or: [ + {'challenge.id': {$exists: false}}, + {'challenge.broken': {$exists: true}}, + ], + }, + { + $or: [ + {'group.id': {$exists: false}}, + {'group.broken': {$exists: true}}, + ], + }, ], }).exec(); diff --git a/website/server/controllers/api-v3/user.js b/website/server/controllers/api-v3/user.js index 3d048a0e59..a11ccbb052 100644 --- a/website/server/controllers/api-v3/user.js +++ b/website/server/controllers/api-v3/user.js @@ -392,11 +392,19 @@ api.castSpell = { } else if (targetType === 'tasks') { // new target type in v3: when all the user's tasks are necessary let tasks = await Tasks.Task.find({ userId: user._id, - $or: [ // exclude challenge tasks - {'challenge.id': {$exists: false}}, - {'challenge.broken': {$exists: true}}, - {'group.id': {$exists: false}}, - {'group.broken': {$exists: true}}, + $and: [ // exclude challenge and group tasks + { + $or: [ + {'challenge.id': {$exists: false}}, + {'challenge.broken': {$exists: true}}, + ], + }, + { + $or: [ + {'group.id': {$exists: false}}, + {'group.broken': {$exists: true}}, + ], + }, ], }).exec(); @@ -1055,11 +1063,19 @@ api.userRebirth = { let tasks = await Tasks.Task.find({ userId: user._id, type: {$in: ['daily', 'habit', 'todo']}, - $or: [ // exclude challenge tasks - {'challenge.id': {$exists: false}}, - {'challenge.broken': {$exists: true}}, - {'group.id': {$exists: false}}, - {'group.broken': {$exists: true}}, + $and: [ // exclude challenge and group tasks + { + $or: [ + {'challenge.id': {$exists: false}}, + {'challenge.broken': {$exists: true}}, + ], + }, + { + $or: [ + {'group.id': {$exists: false}}, + {'group.broken': {$exists: true}}, + ], + }, ], }).exec(); @@ -1173,11 +1189,19 @@ api.userReroll = { let query = { userId: user._id, type: {$in: ['daily', 'habit', 'todo']}, - $or: [ // exclude challenge tasks - {'challenge.id': {$exists: false}}, - {'challenge.broken': {$exists: true}}, - {'group.id': {$exists: false}}, - {'group.broken': {$exists: true}}, + $and: [ // exclude challenge and group tasks + { + $or: [ + {'challenge.id': {$exists: false}}, + {'challenge.broken': {$exists: true}}, + ], + }, + { + $or: [ + {'group.id': {$exists: false}}, + {'group.broken': {$exists: true}}, + ], + }, ], }; let tasks = await Tasks.Task.find(query).exec(); @@ -1210,11 +1234,19 @@ api.userReset = { let tasks = await Tasks.Task.find({ userId: user._id, - $or: [ // exclude challenge tasks - {'challenge.id': {$exists: false}}, - {'challenge.broken': {$exists: true}}, - {'group.id': {$exists: false}}, - {'group.broken': {$exists: true}}, + $and: [ // exclude challenge and group tasks + { + $or: [ + {'challenge.id': {$exists: false}}, + {'challenge.broken': {$exists: true}}, + ], + }, + { + $or: [ + {'group.id': {$exists: false}}, + {'group.broken': {$exists: true}}, + ], + }, ], }).select('_id type challenge group').exec();