allow multiple tasks to be created at once

This commit is contained in:
Matteo Pagliazzi
2016-01-02 15:43:33 +01:00
parent 91e7b9eb32
commit 775766b30f
2 changed files with 133 additions and 20 deletions

View File

@@ -19,7 +19,7 @@ describe('POST /tasks', () => {
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('invalidReqParams'),
message: t('invalidTaskType'),
});
});
@@ -29,7 +29,18 @@ describe('POST /tasks', () => {
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('invalidReqParams'),
message: t('invalidTaskType'),
});
});
it('returns an error if one object inside an array is invalid', () => {
return expect(user.post('/tasks', [
{type: 'habitF'},
{type: 'habit'},
])).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('invalidTaskType'),
});
});
@@ -107,6 +118,36 @@ describe('POST /tasks', () => {
});
});
it('creates multiple habits', () => {
return user.post('/tasks', [{
text: 'test habit',
type: 'habit',
up: false,
down: true,
notes: 1976,
}, {
text: 'test habit 2',
type: 'habit',
up: true,
down: false,
notes: 1977,
}]).then(([task, task2]) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test habit');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('habit');
expect(task.up).to.eql(false);
expect(task.down).to.eql(true);
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test habit 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('habit');
expect(task2.up).to.eql(true);
expect(task2.down).to.eql(false);
});
});
it('defaults to setting up and down to true', () => {
return user.post('/tasks', {
text: 'test habit',
@@ -145,6 +186,28 @@ describe('POST /tasks', () => {
});
});
it('creates multiple todos', () => {
return user.post('/tasks', [{
text: 'test todo',
type: 'todo',
notes: 1976,
}, {
text: 'test todo 2',
type: 'todo',
notes: 1977,
}]).then(([task, task2]) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test todo');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('todo');
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test todo 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('todo');
});
});
it('can create checklists', () => {
return user.post('/tasks', {
text: 'test todo',
@@ -185,6 +248,28 @@ describe('POST /tasks', () => {
});
});
it('creates multiple dailys', () => {
return user.post('/tasks', [{
text: 'test daily',
type: 'daily',
notes: 1976,
}, {
text: 'test daily 2',
type: 'daily',
notes: 1977,
}]).then(([task, task2]) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test daily');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('daily');
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test daily 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('daily');
});
});
it('defaults to a weekly frequency, with every day set', () => {
return user.post('/tasks', {
text: 'test daily',
@@ -271,6 +356,32 @@ describe('POST /tasks', () => {
});
});
it('creates multiple rewards', () => {
return user.post('/tasks', [{
text: 'test reward',
type: 'reward',
notes: 1976,
value: 11,
}, {
text: 'test reward 2',
type: 'reward',
notes: 1977,
value: 12,
}]).then(([task, task2]) => {
expect(task.userId).to.equal(user._id);
expect(task.text).to.eql('test reward');
expect(task.notes).to.eql('1976');
expect(task.type).to.eql('reward');
expect(task.value).to.eql(11);
expect(task2.userId).to.equal(user._id);
expect(task2.text).to.eql('test reward 2');
expect(task2.notes).to.eql('1977');
expect(task2.type).to.eql('reward');
expect(task2.value).to.eql(12);
});
});
it('defaults to a 0 value', () => {
return user.post('/tasks', {
text: 'test reward',

View File

@@ -16,39 +16,41 @@ import { preenHistory } from '../../../../common/script/api-v3/preenHistory';
let api = {};
/**
* @api {post} /tasks Create a new task
* @api {post} /tasks Create a new task. Can be passed an object to create a single task or an array of objects to create multiple tasks.
* @apiVersion 3.0.0
* @apiName CreateTask
* @apiGroup Task
*
* @apiSuccess {Object} task The newly created task
* @apiSuccess {Object|Array} task The newly created task(s)
*/
// TODO should allow to create multiple tasks at once
// TODO gives problems when creating tasks concurrently because of how mongoose treats arrays (VersionErrors - treated as 500s)
api.createTask = {
method: 'POST',
url: '/tasks',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
req.checkBody('type', res.t('invalidTaskType')).notEmpty().isIn(Tasks.tasksTypes);
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let tasksData = Array.isArray(req.body) ? req.body : [req.body];
let user = res.locals.user;
let taskType = req.body.type;
let newTask = new Tasks[taskType](Tasks.Task.sanitizeCreate(req.body));
newTask.userId = user._id;
let toSave = tasksData.map(taskData => {
if (!taskData || Tasks.tasksTypes.indexOf(taskData.type) === -1) throw new BadRequest(res.t('invalidTaskType'));
user.tasksOrder[`${taskType}s`].unshift(newTask._id);
let taskType = taskData.type;
let newTask = new Tasks[taskType](Tasks.Task.sanitizeCreate(taskData));
newTask.userId = user._id;
user.tasksOrder[`${taskType}s`].unshift(newTask._id);
let results = await Q.all([
newTask.save(),
user.save(),
]);
return newTask.save();
});
res.respond(201, results[0]);
toSave.unshift(user.save());
let results = await Q.all(toSave);
if (results.length === 2) { // Just one task created
res.respond(201, results[1]);
} else {
results.splice(0, 1); // remove the user
res.respond(201, results);
}
},
};