-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 알림 단계 설정과 자동 알림 발송 구현 #77
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
4 commits
Select commit
Hold shift + click to select a range
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
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
132 changes: 132 additions & 0 deletions
132
src/main/java/com/gachi/be/domain/newsletter/pipeline/NewsletterPipelineStatusService.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,132 @@ | ||
| package com.gachi.be.domain.newsletter.pipeline; | ||
|
|
||
| import com.gachi.be.domain.child.repository.ChildRepository; | ||
| import com.gachi.be.domain.newsletter.entity.Newsletter; | ||
| import com.gachi.be.domain.newsletter.repository.NewsletterRepository; | ||
| import com.gachi.be.domain.notification.entity.enums.NotificationLevel; | ||
| import com.gachi.be.domain.notification.entity.enums.NotificationType; | ||
| import com.gachi.be.domain.notification.service.NotificationCreateCommand; | ||
| import com.gachi.be.domain.notification.service.NotificationService; | ||
| import java.util.Map; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Propagation; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.transaction.support.TransactionSynchronization; | ||
| import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class NewsletterPipelineStatusService { | ||
|
|
||
| private final NewsletterRepository newsletterRepository; | ||
| private final NotificationService notificationService; | ||
| private final ChildRepository childRepository; | ||
|
|
||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void markProcessing(Long newsletterId) { | ||
| newsletterRepository | ||
| .findById(newsletterId) | ||
| .ifPresent( | ||
| newsletter -> { | ||
| newsletter.startProcessing(); | ||
| newsletterRepository.save(newsletter); | ||
| }); | ||
| } | ||
|
|
||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void markCompleted( | ||
| Long newsletterId, | ||
| String ocrText, | ||
| String originalText, | ||
| String translatedText, | ||
| String title, | ||
| String summary) { | ||
| newsletterRepository | ||
| .findById(newsletterId) | ||
| .ifPresent( | ||
| newsletter -> { | ||
| newsletter.complete(ocrText, originalText, translatedText, title, summary); | ||
| Newsletter saved = newsletterRepository.save(newsletter); | ||
| scheduleAnalysisCompletedNotification(saved); | ||
| }); | ||
| } | ||
|
|
||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void markFailedWithSnapshot( | ||
| Long newsletterId, | ||
| String ocrText, | ||
| String originalText, | ||
| String translatedText, | ||
| String failureStage, | ||
| String failureReason) { | ||
| newsletterRepository | ||
| .findById(newsletterId) | ||
| .ifPresent( | ||
| newsletter -> { | ||
| newsletter.failWithSnapshot( | ||
| ocrText, originalText, translatedText, failureStage, failureReason); | ||
| newsletterRepository.save(newsletter); | ||
| }); | ||
| } | ||
|
|
||
| private void scheduleAnalysisCompletedNotification(Newsletter newsletter) { | ||
| if (TransactionSynchronizationManager.isSynchronizationActive()) { | ||
| TransactionSynchronizationManager.registerSynchronization( | ||
| new TransactionSynchronization() { | ||
| @Override | ||
| public void afterCommit() { | ||
| createAnalysisCompletedNotificationSafely(newsletter); | ||
| } | ||
| }); | ||
| return; | ||
| } | ||
| createAnalysisCompletedNotificationSafely(newsletter); | ||
| } | ||
|
|
||
| private void createAnalysisCompletedNotificationSafely(Newsletter newsletter) { | ||
| try { | ||
| createAnalysisCompletedNotification(newsletter); | ||
| } catch (Exception ex) { | ||
| log.warn( | ||
| "[Pipeline] 분석 완료 알림 생성 실패. newsletterId={}, error={}", | ||
| newsletter.getId(), | ||
| ex.getMessage(), | ||
| ex); | ||
| } | ||
| } | ||
|
|
||
| private void createAnalysisCompletedNotification(Newsletter newsletter) { | ||
| Long childId = resolveChildId(newsletter); | ||
| notificationService.createNotification( | ||
| newsletter.getUserId(), | ||
| new NotificationCreateCommand( | ||
| NotificationType.NEWSLETTER_ANALYSIS, | ||
| "새 가정통신문 분석 완료", | ||
| newsletter.getTitle() != null && !newsletter.getTitle().isBlank() | ||
| ? newsletter.getTitle() + " 분석이 완료되었어요" | ||
| : "가정통신문 분석이 완료되었어요", | ||
| Map.of( | ||
| "newsletterId", | ||
| newsletter.getId(), | ||
| "childName", | ||
| newsletter.getChildName() != null ? newsletter.getChildName() : ""), | ||
| "newsletter-analysis:" + newsletter.getId(), | ||
| NotificationLevel.IMPORTANT, | ||
| childId, | ||
| newsletter.getChildName())); | ||
| } | ||
|
|
||
| private Long resolveChildId(Newsletter newsletter) { | ||
| if (newsletter.getChildName() == null || newsletter.getChildName().isBlank()) { | ||
| return null; | ||
| } | ||
| return childRepository | ||
| .findFirstByUserIdAndNameAndDeletedAtIsNull( | ||
| newsletter.getUserId(), newsletter.getChildName()) | ||
| .map(child -> child.getId()) | ||
| .orElse(null); | ||
| } | ||
| } |
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.
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.