1 | const HttpStatus = require('http-status');
|
2 | const Promise = require('bluebird');
|
3 | const services = require('../../services');
|
4 | const config = require('../../../config');
|
5 | const utils = require('../../../config/utils');
|
6 |
|
7 | const PERCENTAGE = 100;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const 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 |
|
29 | module.exports = {
|
30 | |
31 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |