22
33import com .example .cs25batch .adapter .RedisStreamsClient ;
44import com .example .cs25batch .batch .dto .MailDto ;
5+ import com .example .cs25batch .batch .service .MailLogBatchService ;
56import com .example .cs25entity .domain .mail .entity .MailLog ;
67import com .example .cs25entity .domain .mail .enums .MailStatus ;
78import com .example .cs25entity .domain .mail .exception .CustomMailException ;
2627@ RequiredArgsConstructor
2728public class MailLogAspect {
2829
30+ private final MailLogBatchService mailLogBatchService ;
2931 private final MailLogRepository mailLogRepository ;
3032 private final RedisStreamsClient redisClient ;
3133
@@ -47,19 +49,19 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable {
4749 } catch (Exception e ){
4850 status = MailStatus .FAILED ;
4951 caused = e .getMessage ();
52+ mailLogBatchService .saveFailLog (subscription , quiz , LocalDateTime .now (), caused );
5053 throw new CustomMailException (MailExceptionCode .EMAIL_SEND_FAILED_ERROR );
5154 } finally {
52- MailLog mailLog = MailLog .builder ()
53- .subscription (subscription )
54- .quiz (quiz )
55- .sendDate (LocalDateTime .now ())
56- .status (status )
57- .caused (caused )
58- .build ();
59-
60- mailLogRepository .save (mailLog );
61- mailLogRepository .flush ();
62-
55+ // MailLog mailLog = MailLog.builder()
56+ // .subscription(subscription)
57+ // .quiz(quiz)
58+ // .sendDate(LocalDateTime.now())
59+ // .status(status)
60+ // .caused(caused)
61+ // .build();
62+ //
63+ // mailLogRepository.save(mailLog);
64+ // mailLogRepository.flush();
6365 if (status == MailStatus .FAILED ) {
6466 log .info ("메일 발송 실패 : subscriptionId - {}, cause - {}" , subscription .getId (), caused );
6567 Map <String , String > retryMessage = Map .of (
@@ -69,6 +71,14 @@ public Object logMailSend(ProceedingJoinPoint joinPoint) throws Throwable {
6971 );
7072 redisClient .addDlq ("quiz-email-retry-stream" , retryMessage );
7173 }
74+ else if (status == MailStatus .SENT ){
75+ try {
76+ mailLogBatchService .saveSuccessLog (subscription , quiz , LocalDateTime .now ());
77+ } catch (Exception logEx ) {
78+ log .warn ("메일 성공 로그 저장 실패: subId={}, quizId={}" ,
79+ subscription .getId (), quiz .getId (), logEx );
80+ }
81+ }
7282 }
7383 }
7484}
0 commit comments