Skip to content

Instantly share code, notes, and snippets.

@vedranjukic
Last active December 23, 2017 18:30
Show Gist options
  • Save vedranjukic/f455e8fa0016df355cdf0d13d17d815c to your computer and use it in GitHub Desktop.
Save vedranjukic/f455e8fa0016df355cdf0d13d17d815c to your computer and use it in GitHub Desktop.
Isolated update user logic from express route
//
// Isolated update user logic from express route
// Still coupled with mongo db client
//
async function updateUser ({db, updateParams}) {
const { userId, userName, userEmail } = updateParams
// validate request params
if (!userId || !userName || !userEmail) {
throw new InvalidParamsException('Missing required params')
}
if (!isValidUserName(userName)) {
throw new InvalidParamsException('Invalid username')
}
if (!isValidEmail(userEmail)) {
throw new InvalidParamsException('Invalid email')
}
const users = db.get('users')
const user = await users.findOne({
userId
})
if (!user) {
throw new UserNotFoundException()
}
const updatedUser = {
...user,
userName,
userEmail
}
await users.update({
userId
}, updatedUser)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment