-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskController.js
More file actions
166 lines (142 loc) · 4.67 KB
/
taskController.js
File metadata and controls
166 lines (142 loc) · 4.67 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const Task = require('../models/Task');
const mongoose = require('mongoose');
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 30 }); // 30 sec cache
const clearUserCache = (userId, role) => {
const keys = cache.keys();
keys.forEach(key => {
if (key.startsWith(userId) || key.startsWith('admin')) cache.del(key);
});
};
// @desc Get all tasks (with Filtering, Sorting, Pagination, Caching)
// @route GET /api/v1/tasks
// @access Private
exports.getTasks = async (req, res, next) => {
try {
const cacheKey = `${req.user.role === 'admin' ? 'admin' : req.user.id}:${req.originalUrl}`;
if (cache.has(cacheKey)) return res.status(200).json(cache.get(cacheKey));
let query;
const reqQuery = { ...req.query };
const page = Number(req.query.page) || 1;
const limit = Number(req.query.limit) || 10;
const skip = (page - 1) * limit;
const removeFields = ['sort', 'page', 'limit'];
removeFields.forEach(param => delete reqQuery[param]);
// Role check
if (req.user.role !== 'admin') {
reqQuery.user = req.user.id;
}
query = Task.find(reqQuery);
// Sorting
if (req.query.sort) {
const sortBy = req.query.sort.split(',').join(' ');
query = query.sort(sortBy);
} else {
query = query.sort('-createdAt'); // Default sort newest
}
query = query.skip(skip).limit(limit);
// Populate for admin
if (req.user.role === 'admin') {
query = query.populate('user', 'name email');
}
const total = await Task.countDocuments(reqQuery);
const tasks = await query;
const response = {
success: true,
count: tasks.length,
pagination: {
page,
limit,
total,
pages: Math.ceil(total / limit),
},
data: tasks
};
cache.set(cacheKey, response);
res.status(200).json(response);
} catch (err) {
next(err);
}
};
// @desc Get single task
// @route GET /api/v1/tasks/:id
// @access Private
exports.getTask = async (req, res, next) => {
try {
const task = await Task.findById(req.params.id);
if (!task) return res.status(404).json({ success: false, error: 'Task not found' });
if (task.user.toString() !== req.user.id && req.user.role !== 'admin') {
return res.status(403).json({ success: false, error: 'Not authorized' });
}
res.status(200).json({ success: true, data: task });
} catch (err) {
next(err);
}
};
// @desc Create new task
// @route POST /api/v1/tasks
// @access Private
exports.createTask = async (req, res, next) => {
try {
req.body.user = req.user.id;
const task = await Task.create(req.body);
clearUserCache(req.user.id, req.user.role);
res.status(201).json({ success: true, data: task });
} catch (err) {
next(err);
}
};
// @desc Update task
// @route PUT /api/v1/tasks/:id
// @access Private
exports.updateTask = async (req, res, next) => {
try {
let task = await Task.findById(req.params.id);
if (!task) return res.status(404).json({ success: false, error: 'Task not found' });
if (task.user.toString() !== req.user.id && req.user.role !== 'admin') {
return res.status(403).json({ success: false, error: 'Not authorized' });
}
task = await Task.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true });
clearUserCache(req.user.id, req.user.role);
res.status(200).json({ success: true, data: task });
} catch (err) {
next(err);
}
};
// @desc Delete task
// @route DELETE /api/v1/tasks/:id
// @access Private
exports.deleteTask = async (req, res, next) => {
try {
const task = await Task.findById(req.params.id);
if (!task) return res.status(404).json({ success: false, error: 'Task not found' });
if (task.user.toString() !== req.user.id && req.user.role !== 'admin') {
return res.status(403).json({ success: false, error: 'Not authorized' });
}
await Task.findByIdAndDelete(req.params.id);
clearUserCache(req.user.id, req.user.role);
res.status(200).json({ success: true, data: {} });
} catch (err) {
next(err);
}
};
// @desc Get task stats (Aggregation)
// @route GET /api/v1/tasks/stats
// @access Private
exports.getTaskStats = async (req, res, next) => {
try {
const matchCriteria = req.user.role === 'admin' ? {} : { user: new mongoose.Types.ObjectId(req.user.id) };
const stats = await Task.aggregate([
{ $match: matchCriteria },
{
$group: {
_id: { status: '$status', priority: '$priority' },
count: { $sum: 1 }
}
}
]);
res.status(200).json({ success: true, data: stats });
} catch (err) {
next(err);
}
};