adapt current tests and change urls to avoid conflicts

This commit is contained in:
Matteo Pagliazzi
2016-01-04 22:03:21 +01:00
parent 6680853078
commit 3bc8945bcc
14 changed files with 105 additions and 94 deletions

View File

@@ -49,5 +49,6 @@
"winnerNotFound": "Winner with id \"<%= userId %>\" not found or not part of the challenge.",
"noCompletedTodosChallenge": "\"includeComepletedTodos\" is not supported when fetching a challenge tasks.",
"userTasksNoChallengeId": "When \"tasksOwner\" is \"user\" \"challengeId\" can't be passed.",
"onlyChalLeaderEditTasks": "Tasks belonging to a challenge can only be edited by the leader."
"onlyChalLeaderEditTasks": "Tasks belonging to a challenge can only be edited by the leader.",
"invalidTasksOwner": "\"tasksOwner\" must be \"user\" or \"challenge\"."
}

View File

@@ -16,7 +16,7 @@ describe('DELETE /tasks/:id', () => {
let task;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
}).then((createdTask) => {
@@ -48,7 +48,7 @@ describe('DELETE /tasks/:id', () => {
it('cannot delete a task owned by someone else', () => {
return generateUser()
.then((anotherUser) => {
return anotherUser.post('/tasks', {
return anotherUser.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
});

View File

@@ -16,14 +16,14 @@ describe('GET /tasks', () => {
});
it('returns all user\'s tasks', async () => {
let createdTasks = await user.post('/tasks', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]);
let tasks = await user.get('/tasks/user');
let createdTasks = await user.post('/tasks?tasksOwner=user', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]);
let tasks = await user.get('/tasks?tasksOwner=user');
expect(tasks.length).to.equal(createdTasks.length + 1); // + 1 because 1 is a default task
});
it('returns only a type of user\'s tasks if req.query.type is specified', async () => {
let createdTasks = await user.post('/tasks', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]);
let tasks = await user.get('/tasks/user?type=habit');
let createdTasks = await user.post('/tasks?tasksOwner=user', [{text: 'test habit', type: 'habit'}, {text: 'test todo', type: 'todo'}]);
let tasks = await user.get('/tasks?tasksOwner=user&type=habit');
expect(tasks.length).to.equal(1);
expect(tasks[0]._id).to.equal(createdTasks[0]._id);
});

View File

@@ -17,7 +17,7 @@ describe('GET /tasks/:id', () => {
let task;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
}).then((createdTask) => {
@@ -54,7 +54,7 @@ describe('GET /tasks/:id', () => {
.then((user2) => {
anotherUser = user2;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
});

View File

@@ -14,7 +14,7 @@ describe('POST /tasks', () => {
context('validates params', () => {
it('returns an error if req.body.type is absent', async () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
notType: 'habit',
})).to.eventually.be.rejected.and.eql({
code: 400,
@@ -24,7 +24,7 @@ describe('POST /tasks', () => {
});
it('returns an error if req.body.type is not valid', async () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habitF',
})).to.eventually.be.rejected.and.eql({
code: 400,
@@ -34,7 +34,7 @@ describe('POST /tasks', () => {
});
it('returns an error if one object inside an array is invalid', async () => {
return expect(user.post('/tasks', [
return expect(user.post('/tasks?tasksOwner=user', [
{type: 'habitF'},
{type: 'habit'},
])).to.eventually.be.rejected.and.eql({
@@ -45,7 +45,7 @@ describe('POST /tasks', () => {
});
it('returns an error if req.body.text is absent', async () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
})).to.eventually.be.rejected.and.eql({
code: 400,
@@ -56,7 +56,7 @@ describe('POST /tasks', () => {
it('does not update user.tasksOrder.{taskType} when the task is not saved because invalid', async () => {
let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits;
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
})).to.eventually.be.rejected.and.eql({ // this block is necessary
code: 400,
@@ -71,7 +71,7 @@ describe('POST /tasks', () => {
it('does not update user.tasksOrder.{taskType} when a task inside an array is not saved because invalid', async () => {
let originalHabitsOrder = (await user.get('/user')).tasksOrder.habits;
return expect(user.post('/tasks', [
return expect(user.post('/tasks?tasksOwner=user', [
{type: 'habit'}, // Missing text
{type: 'habit', text: 'valid'}, // Valid
])).to.eventually.be.rejected.and.eql({ // this block is necessary
@@ -86,8 +86,8 @@ describe('POST /tasks', () => {
});
it('does not save any task sent in an array when 1 is invalid', async () => {
let originalTasks = await user.get('/tasks');
return expect(user.post('/tasks', [
let originalTasks = await user.get('/tasks?tasksOwner=user');
return expect(user.post('/tasks?tasksOwner=user', [
{type: 'habit'}, // Missing text
{type: 'habit', text: 'valid'}, // Valid
])).to.eventually.be.rejected.and.eql({ // this block is necessary
@@ -95,14 +95,14 @@ describe('POST /tasks', () => {
error: 'BadRequest',
message: 'habit validation failed',
}).then(async () => {
let updatedTasks = await user.get('/tasks');
let updatedTasks = await user.get('/tasks?tasksOwner=user');
expect(updatedTasks).to.eql(originalTasks);
});
});
it('automatically sets "task.userId" to user\'s uuid', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
});
@@ -113,7 +113,7 @@ describe('POST /tasks', () => {
it(`ignores setting userId, history, createdAt,
updatedAt, challenge, completed, streak,
dateCompleted fields`, async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
userId: 123,
@@ -137,7 +137,7 @@ describe('POST /tasks', () => {
});
it('ignores invalid fields', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
notValid: true,
@@ -149,7 +149,7 @@ describe('POST /tasks', () => {
context('habits', () => {
it('creates a habit', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
up: false,
@@ -167,7 +167,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.habits when a new habit is created', async () => {
let originalHabitsOrderLen = (await user.get('/user')).tasksOrder.habits.length;
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'an habit',
});
@@ -179,7 +179,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.habits when multiple habits are created', async () => {
let originalHabitsOrderLen = (await user.get('/user')).tasksOrder.habits.length;
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
type: 'habit',
text: 'an habit',
}, {
@@ -194,7 +194,7 @@ describe('POST /tasks', () => {
});
it('creates multiple habits', async () => {
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
text: 'test habit',
type: 'habit',
up: false,
@@ -224,7 +224,7 @@ describe('POST /tasks', () => {
});
it('defaults to setting up and down to true', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
notes: 1976,
@@ -235,7 +235,7 @@ describe('POST /tasks', () => {
});
it('cannot create checklists', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
checklist: [
@@ -249,7 +249,7 @@ describe('POST /tasks', () => {
context('todos', () => {
it('creates a todo', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test todo',
type: 'todo',
notes: 1976,
@@ -262,7 +262,7 @@ describe('POST /tasks', () => {
});
it('creates multiple todos', async () => {
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
text: 'test todo',
type: 'todo',
notes: 1976,
@@ -285,7 +285,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.todos when a new todo is created', async () => {
let originalTodosOrderLen = (await user.get('/user')).tasksOrder.todos.length;
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
type: 'todo',
text: 'a todo',
});
@@ -297,7 +297,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.todos when multiple todos are created', async () => {
let originalTodosOrderLen = (await user.get('/user')).tasksOrder.todos.length;
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
type: 'todo',
text: 'a todo',
}, {
@@ -312,7 +312,7 @@ describe('POST /tasks', () => {
});
it('can create checklists', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test todo',
type: 'todo',
checklist: [
@@ -333,7 +333,7 @@ describe('POST /tasks', () => {
it('creates a daily', async () => {
let now = new Date();
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
notes: 1976,
@@ -352,7 +352,7 @@ describe('POST /tasks', () => {
});
it('creates multiple dailys', async () => {
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
text: 'test daily',
type: 'daily',
notes: 1976,
@@ -375,7 +375,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.dailys when a new daily is created', async () => {
let originalDailysOrderLen = (await user.get('/user')).tasksOrder.dailys.length;
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'a daily',
});
@@ -387,7 +387,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.dailys when multiple dailys are created', async () => {
let originalDailysOrderLen = (await user.get('/user')).tasksOrder.dailys.length;
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
type: 'daily',
text: 'a daily',
}, {
@@ -402,7 +402,7 @@ describe('POST /tasks', () => {
});
it('defaults to a weekly frequency, with every day set', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
});
@@ -421,7 +421,7 @@ describe('POST /tasks', () => {
});
it('allows repeat field to be configured', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
repeat: {
@@ -445,7 +445,7 @@ describe('POST /tasks', () => {
it('defaults startDate to today', async () => {
let today = (new Date()).getDay();
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
});
@@ -454,7 +454,7 @@ describe('POST /tasks', () => {
});
it('can create checklists', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
checklist: [
@@ -473,7 +473,7 @@ describe('POST /tasks', () => {
context('rewards', () => {
it('creates a reward', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test reward',
type: 'reward',
notes: 1976,
@@ -488,7 +488,7 @@ describe('POST /tasks', () => {
});
it('creates multiple rewards', async () => {
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
text: 'test reward',
type: 'reward',
notes: 1976,
@@ -515,7 +515,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.rewards when a new reward is created', async () => {
let originalRewardsOrderLen = (await user.get('/user')).tasksOrder.rewards.length;
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
type: 'reward',
text: 'a reward',
});
@@ -527,7 +527,7 @@ describe('POST /tasks', () => {
it('updates user.tasksOrder.dreward when multiple rewards are created', async () => {
let originalRewardsOrderLen = (await user.get('/user')).tasksOrder.rewards.length;
let [task, task2] = await user.post('/tasks', [{
let [task, task2] = await user.post('/tasks?tasksOwner=user', [{
type: 'reward',
text: 'a reward',
}, {
@@ -542,7 +542,7 @@ describe('POST /tasks', () => {
});
it('defaults to a 0 value', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test reward',
type: 'reward',
});
@@ -551,7 +551,7 @@ describe('POST /tasks', () => {
});
it('requires value to be coerced into a number', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test reward',
type: 'reward',
value: '10',
@@ -561,7 +561,7 @@ describe('POST /tasks', () => {
});
it('cannot create checklists', async () => {
let task = await user.post('/tasks', {
let task = await user.post('/tasks?tasksOwner=user', {
text: 'test reward',
type: 'reward',
checklist: [

View File

@@ -37,7 +37,7 @@ describe('POST /tasks/:id/score/:direction', () => {
let todo;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test todo',
type: 'todo',
}).then((task) => {
@@ -149,7 +149,7 @@ describe('POST /tasks/:id/score/:direction', () => {
let daily;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
}).then((task) => {
@@ -226,26 +226,26 @@ describe('POST /tasks/:id/score/:direction', () => {
let habit, minusHabit, plusHabit, neitherHabit; // eslint-disable-line no-unused-vars
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
}).then((task) => {
habit = task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test min habit',
type: 'habit',
up: false,
});
}).then((task) => {
minusHabit = task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test plus habit',
type: 'habit',
down: false,
});
}).then((task) => {
plusHabit = task;
user.post('/tasks', {
user.post('/tasks?tasksOwner=user', {
text: 'test neither habit',
type: 'habit',
up: false,
@@ -297,7 +297,7 @@ describe('POST /tasks/:id/score/:direction', () => {
let reward;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test reward',
type: 'reward',
value: 5,

View File

@@ -16,7 +16,7 @@ describe('PUT /tasks/:id', () => {
let task;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
}).then((createdTask) => {
@@ -65,7 +65,7 @@ describe('PUT /tasks/:id', () => {
let habit;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test habit',
type: 'habit',
notes: 1976,
@@ -93,7 +93,7 @@ describe('PUT /tasks/:id', () => {
let todo;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test todo',
type: 'todo',
notes: 1976,
@@ -150,7 +150,7 @@ describe('PUT /tasks/:id', () => {
let daily;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test daily',
type: 'daily',
notes: 1976,
@@ -254,7 +254,7 @@ describe('PUT /tasks/:id', () => {
let reward;
beforeEach(() => {
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
text: 'test reward',
type: 'reward',
notes: 1976,

View File

@@ -16,7 +16,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
it('deletes a checklist item', () => {
let task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'Daily with checklist',
}).then(createdTask => {
@@ -33,7 +33,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
it('does not work with habits', () => {
let habit;
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'habit with checklist',
}).then(createdTask => {
@@ -47,7 +47,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
});
it('does not work with rewards', async () => {
let reward = await user.post('/tasks', {
let reward = await user.post('/tasks?tasksOwner=user', {
type: 'reward',
text: 'reward with checklist',
});
@@ -68,7 +68,7 @@ describe('DELETE /tasks/:taskId/checklist/:itemId', () => {
});
it('fails on checklist item not found', () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'daily with checklist',
}).then(createdTask => {

View File

@@ -16,7 +16,7 @@ describe('POST /tasks/:taskId/checklist/', () => {
it('adds a checklist item to a task', () => {
let task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'Daily with checklist',
}).then(createdTask => {
@@ -36,7 +36,7 @@ describe('POST /tasks/:taskId/checklist/', () => {
it('does not add a checklist to habits', () => {
let habit;
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'habit with checklist',
}).then(createdTask => {
@@ -51,7 +51,7 @@ describe('POST /tasks/:taskId/checklist/', () => {
it('does not add a checklist to rewards', () => {
let reward;
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'reward',
text: 'reward with checklist',
}).then(createdTask => {

View File

@@ -16,7 +16,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
it('scores a checklist item', () => {
let task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'Daily with checklist',
}).then(createdTask => {
@@ -32,7 +32,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
it('fails on habits', () => {
let habit;
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'habit with checklist',
}).then(createdTask => {
@@ -46,7 +46,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
});
it('fails on rewards', async () => {
let reward = await user.post('/tasks', {
let reward = await user.post('/tasks?tasksOwner=user', {
type: 'reward',
text: 'reward with checklist',
});
@@ -67,7 +67,7 @@ describe('POST /tasks/:taskId/checklist/:itemId/score', () => {
});
it('fails on checklist item not found', () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'daily with checklist',
}).then(createdTask => {

View File

@@ -16,7 +16,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => {
it('updates a checklist item', () => {
let task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'Daily with checklist',
}).then(createdTask => {
@@ -33,7 +33,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => {
});
it('fails on habits', async () => {
let habit = await user.post('/tasks', {
let habit = await user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'habit with checklist',
});
@@ -46,7 +46,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => {
});
it('fails on rewards', async () => {
let reward = await user.post('/tasks', {
let reward = await user.post('/tasks?tasksOwner=user', {
type: 'reward',
text: 'reward with checklist',
});
@@ -67,7 +67,7 @@ describe('PUT /tasks/:taskId/checklist/:itemId', () => {
});
it('fails on checklist item not found', () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'daily',
text: 'daily with checklist',
}).then(createdTask => {

View File

@@ -17,7 +17,7 @@ describe('DELETE /tasks/:taskId/tags/:tagId', () => {
let tag;
let task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
@@ -35,7 +35,7 @@ describe('DELETE /tasks/:taskId/tags/:tagId', () => {
});
it('only deletes existing tags', () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {

View File

@@ -17,7 +17,7 @@ describe('POST /tasks/:taskId/tags/:tagId', () => {
let tag;
let task;
return user.post('/tasks', {
return user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
@@ -35,7 +35,7 @@ describe('POST /tasks/:taskId/tags/:tagId', () => {
let tag;
let task;
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'Task with tag',
}).then(createdTask => {
@@ -54,7 +54,7 @@ describe('POST /tasks/:taskId/tags/:tagId', () => {
});
it('does not add a non existing tag to a task', () => {
return expect(user.post('/tasks', {
return expect(user.post('/tasks?tasksOwner=user', {
type: 'habit',
text: 'Task with tag',
}).then((task) => {

View File

@@ -22,20 +22,26 @@ let api = {};
* @apiName CreateTask
* @apiGroup Task
*
* @apiParam {string="user","challenge"} tasksOwner Define if tasks will belong to the auhenticated user or to a challenge (specifying the "challengeId" parameter).
* @apiParam {UUID} challengeId Optional. If "tasksOwner" is "challenge" then specify the challenge id.
* @apiParam {string="user","challenge"} tasksOwner Query parameter to define if tasks will belong to the auhenticated user or to a challenge (specifying the "challengeId" parameter).
* @apiParam {UUID} challengeId Optional. Query parameter. If "tasksOwner" is "challenge" then specify the challenge id.
*
* @apiSuccess {Object|Array} task The newly created task(s)
*/
api.createTask = {
method: 'POST',
url: '/tasks/:tasksOwner/:challengeId?',
url: '/tasks',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
req.checkQuery('tasksOwner', res.t('invalidTasksOwner')).isIn(['user', 'challenge']);
req.checkQuery('challengeId', res.t('challengeIdRequired')).optional().isUUID();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let tasksData = Array.isArray(req.body) ? req.body : [req.body];
let user = res.locals.user;
let tasksOwner = req.params.tasksOwner;
let challengeId = req.params.challengeId;
let tasksOwner = req.query.tasksOwner;
let challengeId = req.query.challengeId;
let challenge;
if (tasksOwner === 'user' && challengeId) throw new BadRequest(res.t('userTasksNoChallengeId'));
@@ -79,8 +85,13 @@ api.createTask = {
toSave.unshift((challenge || user).save());
let tasks = await Q.all(toSave);
if (tasks.length === 2) {
res.respond(201, tasks[1]);
} else {
tasks.splice(0, 1); // remove the user/challenge
res.respond(201, tasks);
}
// If adding tasks to a challenge -> sync users
if (challenge) challenge.addTasks(tasks); // TODO catch/log
@@ -93,8 +104,8 @@ api.createTask = {
* @apiName GetTasks
* @apiGroup Task
*
* @apiParam {string="user","challenge"} tasksOwner Url parameter to return tasks belonging to a challenge (specifying the "challengeId" parameter) or to the autheticated user.
* @apiParam {UUID} challengeId Optional. If "tasksOwner" is "challenge" then specify the challenge id.
* @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".
*
@@ -102,20 +113,19 @@ api.createTask = {
*/
api.getTasks = {
method: 'GET',
url: '/tasks/:tasksOwner/:challengeId?',
url: '/tasks',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
req.checkParams('tasksOwner', res.t('invalidTasksOwner')).isIn(['user', 'challenge']);
req.checkParams('challengeId', res.t('challengeIdRequired')).optional().isUUID();
req.checkQuery('tasksOwner', res.t('invalidTasksOwner')).isIn(['user', 'challenge']);
req.checkQuery('challengeId', res.t('challengeIdRequired')).optional().isUUID();
req.checkQuery('type', res.t('invalidTaskType')).optional().isIn(Tasks.tasksTypes);
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let user = res.locals.user;
let tasksOwner = req.params.tasksOwner;
let challengeId = req.params.challengeId;
let tasksOwner = req.query.tasksOwner;
let challengeId = req.query.challengeId;
let challenge;
if (tasksOwner === 'user' && challengeId) throw new BadRequest(res.t('userTasksNoChallengeId'));