1+ package kattsyn .dev .rentplace .services .impl ;
2+
3+ import kattsyn .dev .rentplace .configs .EmailRetryProperties ;
4+ import kattsyn .dev .rentplace .exceptions .EmailException ;
5+ import kattsyn .dev .rentplace .exceptions .EmailServiceUnavailableException ;
6+ import kattsyn .dev .rentplace .services .EmailService ;
7+ import lombok .RequiredArgsConstructor ;
8+ import lombok .extern .slf4j .Slf4j ;
9+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
10+ import org .springframework .context .annotation .Primary ;
11+ import org .springframework .stereotype .Service ;
12+
13+ @ Service
14+ @ RequiredArgsConstructor
15+ @ Slf4j
16+ @ Primary
17+ @ EnableConfigurationProperties (EmailRetryProperties .class )
18+ public class CompositeEmailService implements EmailService {
19+
20+ private final PostmarkEmailServiceImpl postmarkEmailServiceImpl ;
21+ private final EmailServiceImpl emailServiceImpl ;
22+ private final EmailRetryProperties retryProperties ;
23+
24+ @ Override
25+ public void sendVerificationCode (String email , String code ) {
26+ try {
27+ postmarkEmailServiceImpl .sendVerificationCode (email , code );
28+ } catch (EmailException postmarkException ) {
29+ log .warn ("Postmark failed, trying JavaMail. Reason: {}" , postmarkException .getMessage ());
30+ try {
31+ emailServiceImpl .sendVerificationCode (email , code );
32+ } catch (EmailException javaMailEx ) {
33+ log .error ("All email services failed: {}" , javaMailEx .getMessage ());
34+ throw new EmailServiceUnavailableException ("All email providers failed" );
35+ }
36+ }
37+ }
38+ }
0 commit comments