|
1
|
|
|
package com.osomapps.pt.activecertificate; |
|
2
|
|
|
|
|
3
|
|
|
import com.osomapps.pt.ResourceNotFoundException; |
|
4
|
|
|
import com.osomapps.pt.admin.certificate.Certificate; |
|
5
|
|
|
import com.osomapps.pt.admin.certificate.CertificateRepository; |
|
6
|
|
|
import com.osomapps.pt.token.InUserLogin; |
|
7
|
|
|
import com.osomapps.pt.user.UserService; |
|
8
|
|
|
import java.time.LocalDate; |
|
9
|
|
|
import java.util.List; |
|
10
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
11
|
|
|
import org.springframework.stereotype.Service; |
|
12
|
|
|
|
|
13
|
|
|
@Service |
|
14
|
|
|
class ActiveCertificateService { |
|
15
|
|
|
private final InUserCertificateRepository inUserCertificateRepository; |
|
16
|
|
|
private final CertificateRepository certificateRepository; |
|
17
|
|
|
private final UserService userService; |
|
18
|
|
|
|
|
19
|
|
|
@Autowired |
|
20
|
|
|
ActiveCertificateService( |
|
21
|
|
|
InUserCertificateRepository inUserCertificateRepository, |
|
22
|
|
|
CertificateRepository certificateRepository, |
|
23
|
|
|
UserService userService) { |
|
24
|
|
|
this.inUserCertificateRepository = inUserCertificateRepository; |
|
25
|
|
|
this.certificateRepository = certificateRepository; |
|
26
|
|
|
this.userService = userService; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
ActiveCertificateResponseDTO firstActive(String token) { |
|
30
|
|
|
if (!token.isEmpty()) { |
|
31
|
|
|
final InUserLogin inUserLogin = userService.checkUserToken(token); |
|
32
|
|
|
return inUserLogin.getInUser().getInUserCertificates().stream() |
|
33
|
|
|
.filter( |
|
34
|
|
|
inUserCertificate -> |
|
35
|
|
|
inUserCertificate |
|
36
|
|
|
.getCreated() |
|
37
|
|
|
.toLocalDate() |
|
38
|
|
|
.plusDays(inUserCertificate.getAmount_of_days()) |
|
39
|
|
|
.isAfter(LocalDate.now())) |
|
40
|
|
|
.map( |
|
41
|
|
|
inUserCertificate -> |
|
42
|
|
|
ActiveCertificateResponseDTO.builder() |
|
43
|
|
|
.id(inUserCertificate.getId()) |
|
44
|
|
|
.code(inUserCertificate.getCode()) |
|
45
|
|
|
.expiration_date( |
|
46
|
|
|
inUserCertificate |
|
47
|
|
|
.getCreated() |
|
48
|
|
|
.toLocalDate() |
|
49
|
|
|
.plusDays( |
|
50
|
|
|
inUserCertificate |
|
51
|
|
|
.getAmount_of_days())) |
|
52
|
|
|
.build()) |
|
53
|
|
|
.findFirst() |
|
54
|
|
|
.orElse(new ActiveCertificateResponseDTO()); |
|
55
|
|
|
} |
|
56
|
|
|
return new ActiveCertificateResponseDTO(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
ActiveCertificateResponseDTO create( |
|
60
|
|
|
String token, ActiveCertificateRequestDTO certificateRequestDTO) { |
|
61
|
|
|
if (!token.isEmpty()) { |
|
62
|
|
|
final InUserLogin inUserLogin = userService.checkUserToken(token); |
|
63
|
|
|
final List<Certificate> certificates = |
|
64
|
|
|
certificateRepository.findByCode(certificateRequestDTO.getCode()); |
|
65
|
|
|
if (certificates.isEmpty()) { |
|
66
|
|
|
throw new ResourceNotFoundException( |
|
67
|
|
|
"Certificate with code " + certificateRequestDTO.getCode() + " not found."); |
|
68
|
|
|
} |
|
69
|
|
|
final Certificate notActiveCertificate = |
|
70
|
|
|
certificates.stream() |
|
71
|
|
|
.filter(certificate -> !certificate.getActivated()) |
|
72
|
|
|
.findAny() |
|
73
|
|
|
.orElseThrow( |
|
74
|
|
|
() -> |
|
75
|
|
|
new ResourceNotFoundException( |
|
76
|
|
|
"There is no active certificates with code " |
|
77
|
|
|
+ certificateRequestDTO.getCode() |
|
78
|
|
|
+ ".")); |
|
79
|
|
|
notActiveCertificate.setActivated(Boolean.TRUE); |
|
80
|
|
|
InUserCertificate inUserCertificate = new InUserCertificate(); |
|
81
|
|
|
inUserCertificate.setInUser(inUserLogin.getInUser()); |
|
82
|
|
|
inUserCertificate.setCode(notActiveCertificate.getCode()); |
|
83
|
|
|
inUserCertificate.setAmount_of_days(notActiveCertificate.getAmount_of_days()); |
|
84
|
|
|
InUserCertificate inUserCertificateSaved = |
|
85
|
|
|
inUserCertificateRepository.save(inUserCertificate); |
|
86
|
|
|
certificateRepository.save(notActiveCertificate); |
|
87
|
|
|
return ActiveCertificateResponseDTO.builder() |
|
88
|
|
|
.id(inUserCertificateSaved.getId()) |
|
89
|
|
|
.code(inUserCertificateSaved.getCode()) |
|
90
|
|
|
.expiration_date( |
|
91
|
|
|
LocalDate.now().plusDays(inUserCertificateSaved.getAmount_of_days())) |
|
92
|
|
|
.build(); |
|
93
|
|
|
} |
|
94
|
|
|
return new ActiveCertificateResponseDTO(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|