Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
refactor send verification email
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou committed Apr 7, 2024
1 parent a9f2254 commit 71b68e3
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.studentcenter.weave.application.user.vo.UserUniversityVerificationNum
import com.studentcenter.weave.support.common.exception.CustomException
import com.studentcenter.weave.support.common.vo.Email
import org.springframework.stereotype.Service
import java.util.*
import kotlin.time.Duration.Companion.minutes

@Service
Expand All @@ -20,29 +21,39 @@ class SendVerificationEmailService(
): SendVerificationEmail {

override fun invoke(universityEmail: Email) {
checkAlreadyVerifiedUniversityEmail(universityEmail)
val verificationNumber = getOrGenerateVerificationNumber(
getCurrentUserAuthentication().userId,
universityEmail,
)

verificationNumberMailer.send(universityEmail, verificationNumber, DEFAULT_DURATION_MINUTE.minutes)
}

private fun checkAlreadyVerifiedUniversityEmail(universityEmail: Email) {
if (verificationInfoDomainService.existsByEmail(universityEmail)) {
throw CustomException(
type = UniversityVerificationExceptionType.VERIFICATED_EMAIL,
message = "이미 인증된 이메일입니다."
)
}
}

val verificationNumber = userVerificationNumberRepository.findByUserId(getCurrentUserAuthentication().userId)?.let {
if (universityEmail == it.first) {
it.second
} else {
null
}
} ?: UserUniversityVerificationNumber.generate().also {
private fun getOrGenerateVerificationNumber(userId: UUID, universityEmail: Email): UserUniversityVerificationNumber {
return userVerificationNumberRepository.findByUserId(userId)?.let {
if (universityEmail == it.first) it.second else null
} ?: generateVerificationNumber(userId, universityEmail)
}

private fun generateVerificationNumber(userId: UUID, universityEmail: Email): UserUniversityVerificationNumber {
return UserUniversityVerificationNumber.generate().also {
userVerificationNumberRepository.save(
getCurrentUserAuthentication().userId,
userId,
universityEmail,
it,
DEFAULT_DURATION_MINUTE.minutes.inWholeSeconds
)
}

verificationNumberMailer.send(universityEmail, verificationNumber, DEFAULT_DURATION_MINUTE.minutes)
}

companion object {
Expand Down

0 comments on commit 71b68e3

Please sign in to comment.