diff --git a/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/SendVerificationEmailService.kt b/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/SendVerificationEmailService.kt index 1d2ac2de..deba986c 100644 --- a/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/SendVerificationEmailService.kt +++ b/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/SendVerificationEmailService.kt @@ -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 @@ -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 {