-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.js
More file actions
45 lines (44 loc) · 1.14 KB
/
delete.js
File metadata and controls
45 lines (44 loc) · 1.14 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
var async = require('async');
var keystone = require('../../../');
module.exports = function(req, res) {
if (!keystone.security.csrf.validate(req)) {
return res.apiError('invalid csrf');
}
if (req.list.get('nodelete')) {
return res.apiError('nodelete');
}
var ids = req.body.ids || req.body.id;
if (typeof ids === 'string') {
ids = ids.split(',');
}
if (!Array.isArray(ids)) {
return res.apiError('invalid ids', 'ids must be an Array or comma-delimited list of ids');
}
if (req.user) {
var userId = String(req.user.id);
if (ids.some(function(id) {
return id === userId;
})) {
return res.apiError('not allowed', 'You can not delete yourself');
}
}
var deletedCount = 0;
var deletedIds = [];
req.list.model.find().where('_id').in(ids).exec(function (err, results) {
if (err) return res.apiError('database error', err);
async.forEachLimit(results, 10, function(item, next) {
item.remove(function (err) {
if (err) return next(err);
deletedCount++;
deletedIds.push(item.id);
next();
});
}, function() {
return res.json({
success: true,
ids: deletedIds,
count: deletedCount
});
});
});
};