|
1 | 1 | package com.github.throyer.common.springboot.domain.recovery.service; |
2 | 2 |
|
3 | | -import com.github.throyer.common.springboot.domain.recovery.model.RecoveryEmail; |
| 3 | +import com.github.throyer.common.springboot.domain.mail.service.MailService; |
4 | 4 | import com.github.throyer.common.springboot.domain.recovery.entity.Recovery; |
| 5 | +import com.github.throyer.common.springboot.domain.recovery.model.RecoveryEmail; |
5 | 6 | import com.github.throyer.common.springboot.domain.recovery.repository.RecoveryRepository; |
6 | 7 | import com.github.throyer.common.springboot.domain.user.repository.UserRepository; |
7 | | -import com.github.throyer.common.springboot.domain.mail.service.MailService; |
8 | 8 | import org.springframework.beans.factory.annotation.Autowired; |
9 | 9 | import org.springframework.stereotype.Service; |
10 | 10 |
|
| 11 | +import java.util.logging.Logger; |
| 12 | + |
| 13 | +import static com.github.throyer.common.springboot.utils.Constants.MAIL.*; |
| 14 | +import static java.lang.String.format; |
| 15 | +import static java.util.logging.Level.INFO; |
| 16 | +import static java.util.logging.Level.WARNING; |
| 17 | + |
11 | 18 | @Service |
12 | 19 | public class RecoveryService { |
13 | | - |
14 | | - private static final Integer MINUTES_TO_EXPIRE = 20; |
15 | 20 |
|
16 | | - @Autowired |
17 | | - private UserRepository users; |
| 21 | + private static final Logger LOGGER = Logger.getLogger(RecoveryService.class.getName()); |
18 | 22 |
|
19 | | - @Autowired |
20 | | - private RecoveryRepository recoveries; |
| 23 | + private final UserRepository users; |
| 24 | + private final RecoveryRepository recoveries; |
| 25 | + private final MailService service; |
21 | 26 |
|
22 | 27 | @Autowired |
23 | | - private MailService service; |
24 | | - |
| 28 | + public RecoveryService( |
| 29 | + UserRepository users, |
| 30 | + RecoveryRepository recoveries, |
| 31 | + MailService service |
| 32 | + ) { |
| 33 | + this.users = users; |
| 34 | + this.recoveries = recoveries; |
| 35 | + this.service = service; |
| 36 | + } |
| 37 | + |
25 | 38 | public void recovery(String email) { |
| 39 | + |
26 | 40 | var user = users.findOptionalByEmail(email); |
27 | 41 |
|
28 | 42 | if (user.isEmpty()) { |
29 | 43 | return; |
30 | 44 | } |
31 | 45 |
|
32 | | - var recovery = new Recovery(user.get(), MINUTES_TO_EXPIRE); |
| 46 | + var recovery = new Recovery(user.get(), MINUTES_TO_EXPIRE_RECOVERY_CODE); |
33 | 47 |
|
34 | 48 | recoveries.save(recovery); |
35 | 49 |
|
36 | | - try { |
37 | | - var recoveryEmail = new RecoveryEmail( |
38 | | - email, |
39 | | - "password recovery code", |
40 | | - user.get().getName(), |
41 | | - recovery.getCode() |
42 | | - ); |
43 | | - |
44 | | - service.send(recoveryEmail); |
45 | | - } catch (Exception exception) { } |
| 50 | + var sendEmailBackground = new Thread(() -> { |
| 51 | + try { |
| 52 | + var recoveryEmail = new RecoveryEmail( |
| 53 | + email, |
| 54 | + SUBJECT_PASSWORD_RECOVERY_CODE, |
| 55 | + user.get().getName(), |
| 56 | + recovery.getCode() |
| 57 | + ); |
| 58 | + |
| 59 | + service.send(recoveryEmail); |
| 60 | + |
| 61 | + LOGGER.log(INFO, format(EMAIL_SENT_SUCCESSFULLY_MESSAGE_LOG_TEMPLATE, email)); |
| 62 | + } catch (Exception exception) { |
| 63 | + LOGGER.log(WARNING, format(UNABLE_TO_SEND_EMAIL_MESSAGE_TEMPLATE, email), exception); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + sendEmailBackground.start(); |
46 | 68 | } |
47 | 69 | } |
0 commit comments