Created
April 4, 2017 02:18
-
-
Save talyssonoc/a27de58e3a78cfafd25fabca584a4ae9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const EventEmitter = require('events'); | |
class CreateUser extends EventEmitter { | |
constructor({ usersRepository }) { | |
super(); | |
this.usersRepository = usersRepository; | |
} | |
execute(userData) { | |
const user = new User(userData); | |
this.usersRepository | |
.add(user) | |
.then((newUser) => { | |
this.emit('SUCCESS', newUser); | |
}) | |
.catch((error) => { | |
if(error.message === 'ValidationError') { | |
return this.emit('VALIDATION_ERROR', error); | |
} | |
this.emit('ERROR', error); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment