From 7a0a87d69c0daec13f4d639037927006010cafc3 Mon Sep 17 00:00:00 2001 From: 101 Date: Sat, 17 Feb 2024 10:35:56 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EB=A9=94=EC=9D=BC=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EB=B0=8F=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/mail/service/MailSend.java | 27 ++++++++++++------- .../user/service/facade/UserFacade.java | 6 ++++- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java b/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java index 9c9f05be..b2a288d6 100644 --- a/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java +++ b/src/main/java/com/example/daemawiki/domain/mail/service/MailSend.java @@ -3,6 +3,8 @@ import com.example.daemawiki.domain.mail.dto.AuthCodeRequest; import com.example.daemawiki.domain.mail.model.AuthCode; import com.example.daemawiki.domain.mail.repository.AuthCodeRepository; +import com.example.daemawiki.domain.user.service.facade.UserFacade; +import com.example.daemawiki.global.exception.h409.EmailAlreadyExistsException; import com.example.daemawiki.global.exception.h500.MailSendFailedException; import jakarta.mail.MessagingException; import jakarta.mail.internet.InternetAddress; @@ -23,11 +25,13 @@ public class MailSend { private final AuthCodeRepository codeRepository; private final JavaMailSender mailSender; private final Scheduler scheduler; + private final UserFacade userFacade; - public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSender, Scheduler scheduler) { + public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSender, Scheduler scheduler, UserFacade userFacade) { this.codeRepository = authCodeRepository; this.mailSender = javaMailSender; this.scheduler = scheduler; + this.userFacade = userFacade; } @Value("${admin.mail}") @@ -36,14 +40,19 @@ public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSe private static final Random rand = new Random(); public Mono execute(AuthCodeRequest request) { - String authCode = getRandomCode(); - String mail = request.mail(); - - Mono sendMailMono = sendMail(mail, authCode).subscribeOn(scheduler); - Mono saveAuthCodeMono = saveAuthCode(mail, authCode).subscribeOn(scheduler); - - return Mono.when(sendMailMono, - saveAuthCodeMono); + return userFacade.findByEmail(request.mail()) + .flatMap(user -> Mono.error(EmailAlreadyExistsException.EXCEPTION)) + .switchIfEmpty(Mono.fromCallable(() -> { + String authCode = getRandomCode(); + String mail = request.mail(); + + Mono sendMailMono = sendMail(mail, authCode).subscribeOn(scheduler); + Mono saveAuthCodeMono = saveAuthCode(mail, authCode).subscribeOn(scheduler); + + return Mono.when(sendMailMono, + saveAuthCodeMono); + })) + .then(); } diff --git a/src/main/java/com/example/daemawiki/domain/user/service/facade/UserFacade.java b/src/main/java/com/example/daemawiki/domain/user/service/facade/UserFacade.java index 377906b1..f4c270d9 100644 --- a/src/main/java/com/example/daemawiki/domain/user/service/facade/UserFacade.java +++ b/src/main/java/com/example/daemawiki/domain/user/service/facade/UserFacade.java @@ -26,8 +26,12 @@ public Mono currentUser() { } public Mono findByEmailNotNull(String email) { - return userRepository.findByEmail(email) + return findByEmail(email) .switchIfEmpty(Mono.error(UserNotFoundException.EXCEPTION)); } + public Mono findByEmail(String email) { + return userRepository.findByEmail(email); + } + } \ No newline at end of file