stonecircle/express-autoroute-json

View on GitHub
lib/deleteOne.js

Summary

Maintainability
A
3 hrs
Test Coverage
A
100%
var mergeQueries = require('./helpers/mergeQueries');
 
Function `exports` has 41 lines of code (exceeds 25 allowed). Consider refactoring.
Function `exports` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
module.exports = function(options) {
return function(req, res, next) {
Identical blocks of code found in 3 locations. Consider refactoring.
return new Promise(function(resolve) {
if (options.translateId) {
return resolve(options.translateId(req.params.id, req));
}
 
return resolve(req.params.id);
})
 
.then(function(id) {
var query = {
_id: id,
};
 
if (req.autorouteQuery) {
query = mergeQueries(req.autorouteQuery, query);
}
 
return options.model.findOneAndRemove(query).then(function(object) {
if (!object) {
throw new Error('NotFound');
}
});
})
 
.then(function() {
res.status(204).send('');
next();
})
 
.then(null, function(err) {
if (err.name === 'CastError' || err.message === 'NotFound') {
res.status(404).send({
errors: [{
detail: 'Not Found',
}],
});
} else {
res.status(500).send({
errors: [{
detail: err.message,
}],
});
}
return next(err);
});
};
};