diff --git a/src/routes/tasks.js b/src/routes/tasks.js index d6c6841f6..55ab36a39 100644 --- a/src/routes/tasks.js +++ b/src/routes/tasks.js @@ -43,9 +43,9 @@ async function list(req, res, next) { const perPage = typeof req.query.per_page === 'string'? parseInt(req.query.per_page) : 25; if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number')); - if (req.query.type && typeof req.query.type !== 'string') return next(new HttpError(400, 'type must be a string')); + if ('type' in req.query && typeof req.query.type !== 'string') return next(new HttpError(400, 'type must be a string')); - const [error, result] = await safe(tasks.listByTypePaged(req.query.type || null, page, perPage)); + const [error, result] = await safe(tasks.list(page, perPage, { type: req.query.type || null })); if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(200, { tasks: result.map(tasks.removePrivateFields) })); diff --git a/src/tasks.js b/src/tasks.js index 7ce07a37c..dbb4f7795 100644 --- a/src/tasks.js +++ b/src/tasks.js @@ -6,7 +6,7 @@ exports = module.exports = { update, setCompleted, setCompletedByType, - listByTypePaged, + list, getLogs, @@ -143,7 +143,7 @@ async function setCompletedByType(type, task) { assert.strictEqual(typeof type, 'string'); assert.strictEqual(typeof task, 'object'); - const results = await listByTypePaged(type, 1, 1); + const results = await list(1, 1, { type }); if (results.length !== 1) throw new BoxError(BoxError.NOT_FOUND, 'No such task'); await setCompleted(results[0].id, task); @@ -232,17 +232,17 @@ async function stopAllTasks() { if (error) debug(`stopAllTasks: error stopping stasks: ${error.message}`); } -async function listByTypePaged(type, page, perPage) { - assert(typeof type === 'string' || type === null); +async function list(page, perPage, options) { assert.strictEqual(typeof page, 'number'); assert.strictEqual(typeof perPage, 'number'); + assert.strictEqual(typeof options, 'object'); const data = []; let query = `SELECT ${TASKS_FIELDS} FROM tasks`; - if (type) { + if (options.type) { query += ' WHERE TYPE=?'; - data.push(type); + data.push(options.type); } query += ' ORDER BY creationTime DESC, id DESC LIMIT ?,?'; // put latest task first diff --git a/src/test/tasks-test.js b/src/test/tasks-test.js index 030829614..4f8b74756 100644 --- a/src/test/tasks-test.js +++ b/src/test/tasks-test.js @@ -55,18 +55,18 @@ describe('task', function () { }); it('list succeeds - does not exist', async function () { - const result = await tasks.listByTypePaged('randomtask', 1, 1); + const result = await tasks.list(1, 1, { type: 'randomtask' }); expect(result.length).to.be(0); }); it('list succeeds - by type', async function () { - const result = await tasks.listByTypePaged(TASK.type, 1, 1); + const result = await tasks.list(1, 1, { type: TASK.type }); expect(result.length).to.be(1); expect(_.pick(result[0], Object.keys(TASK))).to.eql(TASK); }); it('list succeeds - all', async function () { - const result = await tasks.listByTypePaged(null, 1, 1); + const result = await tasks.list(1, 1, { type: null }); expect(result.length).to.be(1); expect(_.pick(result[0], Object.keys(TASK))).to.eql(TASK); });