UNPKG

2.65 kBJavaScriptView Raw
1const HttpStatus = require('http-status');
2const Promise = require('bluebird');
3const services = require('../../services');
4const config = require('../../../config');
5const utils = require('../../../config/utils');
6
7const PERCENTAGE = 100;
8
9/**
10 * Generate the status for a task given it's name
11 * @param taskId
12 * @returns {Promise.<TResult>}
13 */
14const getTaskStatus = (taskId) => {
15 return Promise.all([services.subtasks.getTotal(taskId), services.subtasks.countBacklog(taskId), services.subtasks.countCompleted(taskId), services.tasks.getProgress(taskId)])
16 .then((results) => {
17 const total = results[0];
18 const completed = results[2];
19 return {
20 percentComplete: ((completed / total) * PERCENTAGE).toFixed(config.numDigits),
21 total: total,
22 completed: completed,
23 backlog: results[1],
24 inProgress: results[3],
25 };
26 });
27};
28
29module.exports = {
30 /**
31 * Returns list of all tasks, and their status/progress
32 */
33 getAll: (req, res) =>
34 services.tasks.getAll()
35 .then((taskIds) =>
36 Promise.reduce(taskIds, (result, taskId) =>
37 getTaskStatus(taskId)
38 .then((status) => result[taskId] = status)
39 .then(() => result), {}))
40 .then((response) => res.status(HttpStatus.OK).json(response))
41 .catch((error) => utils.processError(error, res)),
42
43 /**
44 * Get a specific task by name
45 */
46 get: (req, res) =>
47 services.tasks.exists(req.params.id)
48 .then((exists) =>
49 exists
50 ? getTaskStatus(req.params.id).then((status) => res.status(HttpStatus.OK).json(status))
51 : res.status(HttpStatus.NOT_FOUND).json({error: `task '${req.params.id}' not found`})
52 )
53 .catch((e) => utils.processError(e, res)),
54
55 /**
56 * Add a new task by name
57 */
58 add: (req, res) =>
59 services.tasks.add(req.params.id, req.body)
60 .then(() => res.status(HttpStatus.OK).json())
61 .catch((e) => utils.processError(e, res)),
62
63 /**
64 * Delete a task by name
65 */
66 delete: (req, res) =>
67 services.tasks.remove(req.params.id)
68 .then(() => res.status(HttpStatus.NO_CONTENT).json())
69 .catch((e) => utils.processError(e, res)),
70
71 /**
72 * Get all errors for a given task
73 */
74 getErrors: (req, res) =>
75 services.tasks.exists(req.params.id)
76 .then((exists) =>
77 exists
78 ? services.tasks.errors(req.params.id).then((errors) => res.status(HttpStatus.OK).json(errors))
79 : res.status(HttpStatus.NOT_FOUND).json({error: `task '${req.params.id}' not found`})
80 )
81 .catch((e) => utils.processError(e, res))
82};
\No newline at end of file