-
Notifications
You must be signed in to change notification settings - Fork 0
복습 대상 URL 이메일 전송 스케줄러 구현 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
91d03d0
feat: Review 엔티티에 Member 연관 관계 추가
jhan0121 f472d6f
feat: 주기적 복습 이메일 전송 기능 추가
jhan0121 a8ebc18
test: ReviewCycleServiceTest 추가
jhan0121 21383cc
refactor: ReviewSendOutput collect 내 불변 리스트를 사용하도록 수정
jhan0121 e3b837c
refactor: html 태그에 lang 추가
jhan0121 2e3bb3e
feat: 이메일 전송 이력 관리 기능 추가
jhan0121 42952ed
style: 코드 구조 정리
jhan0121 32c0de5
refactor: ReviewEmailSender 타임존 설정 추가
jhan0121 eda7e93
test: 메일 발송 실패 처리 검증 추가
jhan0121 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/recyclestudy/email/DeviceAuthEmailSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.recyclestudy.email; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Service; | ||
| import org.thymeleaf.TemplateEngine; | ||
| import org.thymeleaf.context.Context; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class DeviceAuthEmailSender { | ||
|
|
||
| private final EmailSender emailSender; | ||
| private final TemplateEngine templateEngine; | ||
|
|
||
| @Value("${auth.base-url}") | ||
| private String baseUrl; | ||
|
|
||
| @Async | ||
| public void sendDeviceAuthMail(final String email, final String deviceId) { | ||
| final String authUrl = createAuthUrl(email, deviceId); | ||
| final String message = createMessage(authUrl); | ||
|
|
||
| emailSender.send(email, "[Recycle Study] 디바이스 인증을 완료해주세요.", message); | ||
|
|
||
| log.info("인증 메일 발송 성공: {}", email); | ||
| } | ||
|
|
||
| private String createAuthUrl(final String email, final String deviceId) { | ||
| return String.format("%s/api/v1/device/auth?email=%s&identifier=%s", baseUrl, email, deviceId); | ||
| } | ||
|
|
||
| private String createMessage(final String authUrl) { | ||
| final Context context = new Context(); | ||
| context.setVariable("authUrl", authUrl); | ||
| return templateEngine.process("auth_email", context); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.recyclestudy.email; | ||
|
|
||
| import com.recyclestudy.exception.EmailSendException; | ||
| import jakarta.mail.MessagingException; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.mail.javamail.MimeMessageHelper; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class EmailSender { | ||
|
|
||
| private final JavaMailSender javaMailSender; | ||
|
|
||
| public void send(final String targetEmail, final String subject, final String content) { | ||
| try { | ||
| final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); | ||
| final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "UTF-8"); | ||
|
|
||
| helper.setTo(targetEmail); | ||
| helper.setSubject(subject); | ||
| helper.setText(content, true); | ||
|
|
||
| javaMailSender.send(mimeMessage); | ||
|
|
||
| log.info("메일 발송 성공: email={}", targetEmail); | ||
|
|
||
| } catch (MessagingException e) { | ||
| log.error("메일 발송 실패: email={}", targetEmail, e); | ||
| throw new EmailSendException("메일 전송 중 오류가 발생했습니다.", e); | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
src/main/java/com/recyclestudy/email/ReviewEmailSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package com.recyclestudy.email; | ||
|
|
||
| import com.recyclestudy.member.domain.Email; | ||
| import com.recyclestudy.review.domain.NotificationStatus; | ||
| import com.recyclestudy.review.domain.ReviewURL; | ||
| import com.recyclestudy.review.service.NotificationHistoryService; | ||
| import com.recyclestudy.review.service.ReviewCycleService; | ||
| import com.recyclestudy.review.service.input.ReviewSendInput; | ||
| import com.recyclestudy.review.service.output.ReviewSendOutput; | ||
| import com.recyclestudy.review.service.output.ReviewSendOutput.ReviewSendElement; | ||
| import java.time.Clock; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Service; | ||
| import org.thymeleaf.TemplateEngine; | ||
| import org.thymeleaf.context.Context; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class ReviewEmailSender { | ||
|
|
||
| private final EmailSender emailSender; | ||
| private final TemplateEngine templateEngine; | ||
| private final ReviewCycleService reviewCycleService; | ||
| private final NotificationHistoryService notificationHistoryService; | ||
| private final Clock clock; | ||
|
|
||
| @Scheduled(cron = "0 0 8 * * *", zone = "Asia/Seoul") | ||
| public void sendReviewMail() { | ||
|
|
||
| final LocalDate targetDate = LocalDate.now(clock); | ||
| final LocalTime targetTime = LocalTime.of(8, 0); | ||
|
|
||
| final ReviewSendOutput targetReviewCycle = reviewCycleService.findTargetReviewCycle( | ||
| ReviewSendInput.from(targetDate, targetTime)); | ||
|
|
||
| final List<ReviewSendElement> elements = targetReviewCycle.elements(); | ||
| log.info("복습 메일 발송 시작: 대상 {}명", elements.size()); | ||
|
|
||
| int successCount = 0; | ||
| int failCount = 0; | ||
|
|
||
| for (final ReviewSendElement element : elements) { | ||
| final String message = createMessage(element.targetUrls()); | ||
| final Email targetEmail = element.email(); | ||
|
|
||
| final boolean success = sendToTargetEmail(targetEmail, message); | ||
|
|
||
| if (success) { | ||
| notificationHistoryService.saveAll(element.reviewCycleIds(), NotificationStatus.SENT); | ||
| successCount++; | ||
| } else { | ||
| notificationHistoryService.saveAll(element.reviewCycleIds(), NotificationStatus.FAILED); | ||
| failCount++; | ||
| } | ||
| } | ||
|
|
||
| log.info("복습 메일 발송 처리 완료: 성공 {}명, 실패 {}명", successCount, failCount); | ||
| } | ||
|
|
||
| private boolean sendToTargetEmail(final Email targetEmail, final String message) { | ||
| try { | ||
| emailSender.send(targetEmail.getValue(), "[Recycle Study] 오늘의 복습 목록이 도착했습니다", message); | ||
| return true; | ||
| } catch (final Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private String createMessage(final List<ReviewURL> targetUrls) { | ||
| final Context context = new Context(); | ||
| context.setVariable("targetUrls", targetUrls); | ||
| return templateEngine.process("review_email", context); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.