I have an express handler which i thought was pretty simple, but CodeClimate flagged this method as having a Cognitive complexity of 6 (5 is the max by default without flagging something)
Curious how I could simplify this method to reduce Cognitive complexity.
function updateUser (req, res) {
// Update any fields that were passed in.
// Explicitly checking for undefined b/c passing null values should set them to null in the db
if (req.body.firstName !== undefined) {
req.user.firstName = req.body.firstName
}
if (req.body.lastName !== undefined) {
req.user.lastName = req.body.lastName
}
if (req.body.email !== undefined) {
req.user.email = req.body.email
}
if (req.body.phone !== undefined) {
req.user.phone = req.body.phone
}
if (req.body.fax !== undefined) {
req.user.fax = req.body.fax
}
if (req.body.notes !== undefined) {
req.user.notes = req.body.notes
}
req.user.save()
.then(user => {
return res.status(HttpStatus.OK).send(user)
})
.catch(err => {
req.log.error(err)
return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err.message)
})
}