<commit_before>package com.softwaremill.codebrag.service.user

import com.typesafe.scalalogging.slf4j.Logging
import com.softwaremill.codebrag.domain.{LastUserNotificationDispatch, UserSettings, User, Authentication}
import java.util.UUID
import com.softwaremill.codebrag.service.invitations.InvitationService
import com.softwaremill.codebrag.service.notification.NotificationService
import com.softwaremill.codebrag.dao.user.UserDAO
import org.bson.types.ObjectId

class RegisterService(userDao: UserDAO, newUserAdder: NewUserAdder, invitationService: InvitationService, notificationService: NotificationService) extends Logging {

  def firstRegistration: Boolean = userDao.findAll().isEmpty

  def register(login: String, email: String, password: String, invitationCode: String): Either[String, Unit] = {
    logger.info(s"Trying to register $login")
    val emailLowerCase = email.toLowerCase
    if (firstRegistration) {
      val firstUser = User(new ObjectId, Authentication.basic(login, password), login, emailLowerCase, UUID.randomUUID().toString)
      registerUser(firstUser.makeAdmin)
    } else {
      registerUserWithInvitation(login, emailLowerCase, password, invitationCode)
    }
  }

  private def registerUserWithInvitation(login: String, emailLowerCase: String, password: String, invitationCode: String): Either[String, Unit] = {
    for {
      _ <- leftIfTrue(invitationCode.isEmpty, "To register in Codebrag you need a registration link. Ask another Codebrag user to send you one.").right
      _ <- leftIfTrue(!invitationService.verify(invitationCode), "The registration link is not valid or was already used. Ask your friend for another one.").right
      _ <- leftIfSome(userDao.findByLowerCasedLogin(login), "User with the given login already exists").right
      _ <- leftIfSome(userDao.findByEmail(emailLowerCase), "User with the given email already exists").right
    } yield {
      val user = User(new ObjectId, Authentication.basic(login, password), login, emailLowerCase, UUID.randomUUID().toString)
      registerUser(user)
    }
  }

  private def registerUser(user: User): Either[String, Unit] = {
    val addedUser = newUserAdder.add(user)
    notificationService.sendWelcomeNotification(addedUser)
    Right()
  }<commit_msg>A bit of DRY applied<commit_after>package com.softwaremill.codebrag.service.user

import com.typesafe.scalalogging.slf4j.Logging
import com.softwaremill.codebrag.domain.{LastUserNotificationDispatch, UserSettings, User, Authentication}
import java.util.UUID
import com.softwaremill.codebrag.service.invitations.InvitationService
import com.softwaremill.codebrag.service.notification.NotificationService
import com.softwaremill.codebrag.dao.user.UserDAO
import org.bson.types.ObjectId

class RegisterService(userDao: UserDAO, newUserAdder: NewUserAdder, invitationService: InvitationService, notificationService: NotificationService) extends Logging {

  def firstRegistration: Boolean = userDao.findAll().isEmpty

  def register(login: String, email: String, password: String, invitationCode: String): Either[String, Unit] = {
    logger.info(s"Trying to register $login")
    val newUser = User(new ObjectId, Authentication.basic(login, password), login, email.toLowerCase, UUID.randomUUID().toString)
    if (firstRegistration) {
      registerUser(newUser.makeAdmin)
    } else {
      registerUserWithInvitation(newUser, invitationCode)
    }
  }

  private def registerUserWithInvitation(newUser: User, invitationCode: String): Either[String, Unit] = {
    for {
      _ <- leftIfTrue(invitationCode.isEmpty, "To register in Codebrag you need a registration link. Ask another Codebrag user to send you one.").right
      _ <- leftIfTrue(!invitationService.verify(invitationCode), "The registration link is not valid or was already used. Ask your friend for another one.").right
      _ <- leftIfSome(userDao.findByLowerCasedLogin(newUser.name), "User with the given login already exists").right
      _ <- leftIfSome(userDao.findByEmail(newUser.emailLowerCase), "User with the given email already exists").right
    } yield registerUser(newUser)
  }

  private def registerUser(user: User): Either[String, Unit] = {
    val addedUser = newUserAdder.add(user)
    notificationService.sendWelcomeNotification(addedUser)
    Right()
  }
{'fraction_non_alphanumeric': 0.09342319312107832, 'fraction_numerical': 0.00046479200557750407, 'mean_word_length': 5.255813953488372, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4049551', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1198, 'n_words': 296}