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

Commit

Permalink
exception 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Jan 24, 2024
1 parent 6a2ffcd commit 540d93c
Showing 1 changed file with 29 additions and 17 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.global.exception.MailSendFailedException;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -12,6 +14,7 @@
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.io.UnsupportedEncodingException;
import java.util.Random;

@Service
Expand All @@ -30,32 +33,41 @@ public MailSend(AuthCodeRepository authCodeRepository, JavaMailSender javaMailSe
private static final Random rand = new Random();

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

return sendMail(mail, authCode)
.doOnNext(mailSender::send)
.then(saveAuthCode(mail, authCode))
.then();
}

private Mono<MimeMessage> sendMail(String to) {
String authCode = getRandomCode();

private Mono<MimeMessage> sendMail(String to, String authCode) {
return Mono.fromCallable(() -> {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

helper.setTo(to);
helper.setSubject("DSM 메일 인증");

helper.setTo(to);
helper.setSubject("DSM 메일 인증");
String mail = getMailTemplate(authCode);

String mail = getMailTemplate(authCode);
helper.setText(mail);
helper.setFrom(new InternetAddress(admin, "DSM-MAIL-AUTH"));

helper.setText(mail);
helper.setFrom(new InternetAddress(admin, "DSM-MAIL-AUTH"));
return message;
} catch (MessagingException | UnsupportedEncodingException e) {
throw MailSendFailedException.EXCEPTION;
}
}).subscribeOn(Schedulers.boundedElastic());
}

return message;
}).subscribeOn(Schedulers.boundedElastic())
.flatMap(message -> codeRepository.save(AuthCode.builder()
.mail(to)
.code(authCode).build())
.thenReturn(message));
private Mono<AuthCode> saveAuthCode(String to, String authCode) {
return codeRepository.save(AuthCode.builder()
.mail(to)
.code(authCode)
.build());
}

private String getMailTemplate(String key) {
Expand Down

0 comments on commit 540d93c

Please sign in to comment.