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

Commit

Permalink
이메일 검사 및 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Feb 17, 2024
1 parent 9b9e1e0 commit 7a0a87d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}")
Expand All @@ -36,14 +40,19 @@ public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSe
private static final Random rand = new Random();

public Mono<Void> execute(AuthCodeRequest request) {
String authCode = getRandomCode();
String mail = request.mail();

Mono<Void> sendMailMono = sendMail(mail, authCode).subscribeOn(scheduler);
Mono<Void> 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<Void> sendMailMono = sendMail(mail, authCode).subscribeOn(scheduler);
Mono<Void> saveAuthCodeMono = saveAuthCode(mail, authCode).subscribeOn(scheduler);

return Mono.when(sendMailMono,
saveAuthCodeMono);
}))
.then();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public Mono<User> currentUser() {
}

public Mono<User> findByEmailNotNull(String email) {
return userRepository.findByEmail(email)
return findByEmail(email)
.switchIfEmpty(Mono.error(UserNotFoundException.EXCEPTION));
}

public Mono<User> findByEmail(String email) {
return userRepository.findByEmail(email);
}

}

0 comments on commit 7a0a87d

Please sign in to comment.