11package ceos .backend .infra .ses ;
22
33
4+ import ceos .backend .infra .ses .domain .EmailSendHistory ;
5+ import ceos .backend .infra .ses .domain .EmailType ;
6+ import ceos .backend .infra .ses .repository .EmailSendHistoryRepository ;
47import lombok .RequiredArgsConstructor ;
8+ import lombok .extern .slf4j .Slf4j ;
59import org .springframework .stereotype .Component ;
610import org .thymeleaf .context .Context ;
711import org .thymeleaf .spring6 .SpringTemplateEngine ;
812import software .amazon .awssdk .services .ses .SesAsyncClient ;
913import software .amazon .awssdk .services .ses .model .*;
1014
15+ import java .util .concurrent .CompletableFuture ;
16+
17+ @ Slf4j
1118@ Component
1219@ RequiredArgsConstructor
1320public class AwsSESUtils {
1421 private final SesAsyncClient sesAsyncClient ;
1522 private final SpringTemplateEngine templateEngine ;
23+ private final EmailSendHistoryRepository emailSendHistoryRepository ;
1624
17- public void singleEmailRequest (String to , String subject , String template , Context context ) {
25+ public void singleEmailRequest (
26+ String to , String subject , String template , Context context , EmailType emailType ) {
1827 final String html = templateEngine .process (template , context );
1928
2029 final SendEmailRequest .Builder sendEmailRequestBuilder = SendEmailRequest .builder ();
2130 sendEmailRequestBuilder .destination (Destination .builder ().toAddresses (to ).build ());
22- sendEmailRequestBuilder
31+
32+ SendEmailRequest request = sendEmailRequestBuilder
2333 .message (newMessage (subject , html ))
2434 .source ("ceos@ceos-sinchon.com" )
2535 .build ();
2636
27- sesAsyncClient .sendEmail (sendEmailRequestBuilder .build ());
37+ CompletableFuture <SendEmailResponse > future = sesAsyncClient .sendEmail (request );
38+
39+ saveHistory (to , subject , template , emailType , future );
2840 }
2941
3042 private Message newMessage (String subject , String html ) {
@@ -34,4 +46,41 @@ private Message newMessage(String subject, String html) {
3446 .body (Body .builder ().html (builder -> builder .data (html )).build ())
3547 .build ();
3648 }
49+
50+ private void saveHistory (String to , String subject , String template , EmailType emailType , CompletableFuture <SendEmailResponse > future ) {
51+ future .whenComplete (
52+ (response , exception ) -> {
53+ if (exception != null ) {
54+ log .error ("Failed to send email to: {}" , to , exception );
55+ saveFailureHistory (to , subject , template , emailType , exception );
56+ } else {
57+ log .info ("Successfully sent email to: {}, messageId: {}" , to , response .messageId ());
58+ saveSuccessHistory (to , subject , template , emailType , response .messageId ());
59+ }
60+ });
61+ }
62+
63+ private void saveSuccessHistory (
64+ String to , String subject , String template , EmailType emailType , String messageId ) {
65+ try {
66+ EmailSendHistory history =
67+ EmailSendHistory .createSuccess (to , subject , template , emailType , messageId );
68+ emailSendHistoryRepository .save (history );
69+ } catch (Exception e ) {
70+ log .error ("Failed to save email send success history" , e );
71+ }
72+ }
73+
74+ private void saveFailureHistory (
75+ String to , String subject , String template , EmailType emailType , Throwable exception ) {
76+ try {
77+ EmailSendHistory history =
78+ EmailSendHistory .createFailure (
79+ to , subject , template , emailType , exception .getMessage ());
80+ emailSendHistoryRepository .save (history );
81+ } catch (Exception e ) {
82+ log .error ("Failed to save email send failure history" , e );
83+ }
84+ }
85+
3786}
0 commit comments