Last active
December 23, 2017 18:30
-
-
Save vedranjukic/f455e8fa0016df355cdf0d13d17d815c to your computer and use it in GitHub Desktop.
Isolated update user logic from express route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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