-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyTasks.js
77 lines (71 loc) · 2.5 KB
/
myTasks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const mysql = require('./dbcon.js');
const { requireAuth } = require('./middleware.js');
const Utils = require('./utils.js');
const router = express.Router();
router
.get('/', requireAuth, function(req, res) {
const context = {};
const { search_text, status } = req.query;
context.userId = req.user.id;
context.name = req.user.name;
if (search_text) context.search_text = search_text;
if (status && status !== 'All') context.status = status;
getMyTasks(res, context, complete);
function complete() {
context.page = 'mytasks';
res.render('mytasks', context);
}
});
/* Get all tasks assigned to me */
function getMyTasks(res, context, complete){
const sql = `SELECT t.*, p.Project_Name, u.name AS assignee_name
FROM (
SELECT
*, id AS task_id, FALSE AS is_sub
FROM tasks t
WHERE assignee_id = ?
UNION ALL
SELECT
st.id,
st.project_id,
CONCAT(t.name, " - ", st.name) AS name,
st.assignee_id,
st.due_date,
st.status,
st.description,
st.task_id,
TRUE AS is_sub
FROM subtasks st
INNER JOIN tasks t ON st.task_id = t.id
WHERE st.assignee_id = ?
) t
INNER JOIN Projects p ON t.project_id = p.Project_ID
INNER JOIN users u ON t.assignee_id = u.id
WHERE 1
${(context.search_text) ? 'AND (t.name LIKE ? OR t.description LIKE ?)' : ''}
${(context.status) ? 'AND t.status = ?' : ''}
ORDER BY t.due_date ASC`;
const inserts = [context.userId, context.userId];
if (context.search_text) {
inserts.push(`%${context.search_text}%`);
inserts.push(`%${context.search_text}%`);
}
if (context.status) inserts.push(context.status);
mysql.pool.query(sql, inserts, function(error, results) {
if(error){
res.write(JSON.stringify(error));
res.end();
}
else {
results.forEach(t => {
t.isOverdue = Utils.isOverdue(t.due_date);
[t.due_date, t.format_date] = Utils.formatDueDate(t.due_date);
});
context.currenttasks = results.filter(t => t.status != 'Completed');
context.pasttasks = results.filter(t => t.status == 'Completed');
complete();
}
});
}
module.exports = router;