Created
July 13, 2017 06:32
-
-
Save anonymous/d0b0ff714707046da550812602b3d307 to your computer and use it in GitHub Desktop.
the description for this gist
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
/** | |
* Register new user with credentials access. | |
* Given email needs to be unique. | |
* | |
* @param c - user's email and password hash | |
* @return `RegistrationResponse`. | |
*/ | |
def registerUser(c: Credentials): FS[RegistrationResponse] = { | |
import c.email | |
val hash = c.password.bcrypt | |
def emailAlreadyTaken: FS[RegistrationResponse] = | |
for { | |
_ <- log.info(s"Cannot create user, email '$email' is already used") | |
} yield AlreadyRegistered: RegistrationResponse | |
def userRegistered(id: UserId): FS[RegistrationResponse] = | |
for { | |
_ <- log.info("User account with credentials access created") | |
} yield UserRegistered(id): RegistrationResponse | |
for { | |
_ <- log.info(s"Trying to register new user with email: '$email'") | |
insertResult <- persistence.saveCredentialsUser(email, hash) | |
registrationResult <- insertResult match { | |
case UserInserted(id) => | |
userRegistered(id) | |
case AlreadyExists => | |
emailAlreadyTaken | |
case DBFailure(err) => | |
FreeS.pure[F, RegistrationResponse](InternalFailure(err)) | |
} | |
} yield registrationResult | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment