Guest User

fn

a guest
Jul 1st, 2020
157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // shell
  2. class AddUser {
  3.   execute (username, email, password) {
  4.     const user = new User(username, email)
  5.     const exists = UserRepository.findOneBy({ username, email })
  6.  
  7.     handleIfExists(user, exists)
  8.     verifyUser(user)
  9.     // hash password
  10.  
  11.     UserRepository.insert({ ...user, hashedPassword })
  12.   }
  13. }
  14.  
  15. // core
  16. function handleIfExists (userToBeCreated, userFromDatabase) {
  17.   if (!userFromDatabase) {
  18.     return true
  19.   }
  20.   if (user.username === userFromDatabase.username) {
  21.     throw new Error('Username is already taken!)
  22.  } else if (user.email === userFromDatabase.email) {
  23.    throw new Error('Username is already taken!)
  24.   }
  25. }
  26.  
  27. function verifyUser (user) {
  28.   // ...
  29. }
RAW Paste Data