1 | package parkup.configs;
|
---|
2 |
|
---|
3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
4 | import org.springframework.stereotype.Service;
|
---|
5 | import parkup.configs.email.EmailSender;
|
---|
6 | import parkup.configs.email.EmailValidator;
|
---|
7 | import parkup.configs.token.ConfirmationTokenW;
|
---|
8 | import parkup.configs.token.ConfirmationTokenService;
|
---|
9 | import parkup.entities.Vraboten;
|
---|
10 | import parkup.services.VrabotenService;
|
---|
11 |
|
---|
12 | import javax.transaction.Transactional;
|
---|
13 | import java.time.LocalDateTime;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class RegistrationServiceW {
|
---|
17 | private final VrabotenService vrabotenService;
|
---|
18 | private final EmailValidator emailValidator;
|
---|
19 | private final ConfirmationTokenService confirmationTokenService;
|
---|
20 | private final EmailSender emailSender;
|
---|
21 |
|
---|
22 | @Autowired
|
---|
23 | public RegistrationServiceW(VrabotenService vrabotenService, EmailValidator emailValidator,
|
---|
24 | ConfirmationTokenService confirmationTokenService, EmailSender emailSender) {
|
---|
25 | this.vrabotenService = vrabotenService;
|
---|
26 | this.emailValidator = emailValidator;
|
---|
27 | this.confirmationTokenService = confirmationTokenService;
|
---|
28 | this.emailSender = emailSender;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public String register(RegistrationRequest request) {
|
---|
32 | boolean isValidEmail = emailValidator.
|
---|
33 | test(request.getEmail());
|
---|
34 |
|
---|
35 | if (!isValidEmail) {
|
---|
36 | throw new IllegalStateException("email not valid");
|
---|
37 | }
|
---|
38 |
|
---|
39 | String token = vrabotenService.signUpVraboten(
|
---|
40 | new Vraboten(
|
---|
41 | request.getFirstName(),
|
---|
42 | request.getLastName(),
|
---|
43 | request.getEmail(),
|
---|
44 | request.getPassword(),
|
---|
45 | request.getMobile()
|
---|
46 | )
|
---|
47 | );
|
---|
48 |
|
---|
49 | String link = "http://localhost:8080/vraboten/registration/confirm?token=" + token;
|
---|
50 | emailSender.send(
|
---|
51 | request.getEmail(),
|
---|
52 | buildEmail(request.getFirstName(), link));
|
---|
53 |
|
---|
54 | return token;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Transactional
|
---|
58 | public String confirmToken(String token) {
|
---|
59 | ConfirmationTokenW confirmationTokenW = confirmationTokenService
|
---|
60 | .getTokenW(token)
|
---|
61 | .orElseThrow(() ->
|
---|
62 | new IllegalStateException("token not found"));
|
---|
63 |
|
---|
64 | if (confirmationTokenW.getConfirmedAt() != null) {
|
---|
65 | throw new IllegalStateException("email already confirmed");
|
---|
66 | }
|
---|
67 |
|
---|
68 | LocalDateTime expiredAt = confirmationTokenW.getExpiresAt();
|
---|
69 |
|
---|
70 | if (expiredAt.isBefore(LocalDateTime.now())) {
|
---|
71 | throw new IllegalStateException("token expired");
|
---|
72 | }
|
---|
73 |
|
---|
74 | confirmationTokenService.setConfirmedAtW(token);
|
---|
75 |
|
---|
76 | vrabotenService.enableVraboten(confirmationTokenW.getRegistriranParkirac().getEmail());
|
---|
77 | return "confirmed";
|
---|
78 | }
|
---|
79 |
|
---|
80 | private String buildEmail(String name, String link) {
|
---|
81 | return "<div style=\"font-family:Helvetica,Arial,sans-serif;font-size:16px;margin:0;color:#0b0c0c\">\n" +
|
---|
82 | "\n" +
|
---|
83 | "<span style=\"display:none;font-size:1px;color:#fff;max-height:0\"></span>\n" +
|
---|
84 | "\n" +
|
---|
85 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;min-width:100%;width:100%!important\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +
|
---|
86 | " <tbody><tr>\n" +
|
---|
87 | " <td width=\"100%\" height=\"53\" bgcolor=\"#0b0c0c\">\n" +
|
---|
88 | " \n" +
|
---|
89 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;max-width:580px\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\">\n" +
|
---|
90 | " <tbody><tr>\n" +
|
---|
91 | " <td width=\"70\" bgcolor=\"#0b0c0c\" valign=\"middle\">\n" +
|
---|
92 | " <table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
93 | " <tbody><tr>\n" +
|
---|
94 | " <td style=\"padding-left:10px\">\n" +
|
---|
95 | " \n" +
|
---|
96 | " </td>\n" +
|
---|
97 | " <td style=\"font-size:28px;line-height:1.315789474;Margin-top:4px;padding-left:10px\">\n" +
|
---|
98 | " <span style=\"font-family:Helvetica,Arial,sans-serif;font-weight:700;color:#ffffff;text-decoration:none;vertical-align:top;display:inline-block\">Confirm your email</span>\n" +
|
---|
99 | " </td>\n" +
|
---|
100 | " </tr>\n" +
|
---|
101 | " </tbody></table>\n" +
|
---|
102 | " </a>\n" +
|
---|
103 | " </td>\n" +
|
---|
104 | " </tr>\n" +
|
---|
105 | " </tbody></table>\n" +
|
---|
106 | " \n" +
|
---|
107 | " </td>\n" +
|
---|
108 | " </tr>\n" +
|
---|
109 | " </tbody></table>\n" +
|
---|
110 | " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" +
|
---|
111 | " <tbody><tr>\n" +
|
---|
112 | " <td width=\"10\" height=\"10\" valign=\"middle\"></td>\n" +
|
---|
113 | " <td>\n" +
|
---|
114 | " \n" +
|
---|
115 | " <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
116 | " <tbody><tr>\n" +
|
---|
117 | " <td bgcolor=\"#1D70B8\" width=\"100%\" height=\"10\"></td>\n" +
|
---|
118 | " </tr>\n" +
|
---|
119 | " </tbody></table>\n" +
|
---|
120 | " \n" +
|
---|
121 | " </td>\n" +
|
---|
122 | " <td width=\"10\" valign=\"middle\" height=\"10\"></td>\n" +
|
---|
123 | " </tr>\n" +
|
---|
124 | " </tbody></table>\n" +
|
---|
125 | "\n" +
|
---|
126 | "\n" +
|
---|
127 | "\n" +
|
---|
128 | " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" +
|
---|
129 | " <tbody><tr>\n" +
|
---|
130 | " <td height=\"30\"><br></td>\n" +
|
---|
131 | " </tr>\n" +
|
---|
132 | " <tr>\n" +
|
---|
133 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
134 | " <td style=\"font-family:Helvetica,Arial,sans-serif;font-size:19px;line-height:1.315789474;max-width:560px\">\n" +
|
---|
135 | " \n" +
|
---|
136 | " <p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\">Hi " + name + ",</p><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> Thank you for registering. Please click on the below link to activate your account: </p><blockquote style=\"Margin:0 0 20px 0;border-left:10px solid #b1b4b6;padding:15px 0 0.1px 15px;font-size:19px;line-height:25px\"><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> <a href=\"" + link + "\">Activate Now</a> </p></blockquote>\n Link will expire in 15 minutes. <p>See you soon</p>" +
|
---|
137 | " \n" +
|
---|
138 | " </td>\n" +
|
---|
139 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
140 | " </tr>\n" +
|
---|
141 | " <tr>\n" +
|
---|
142 | " <td height=\"30\"><br></td>\n" +
|
---|
143 | " </tr>\n" +
|
---|
144 | " </tbody></table><div class=\"yj6qo\"></div><div class=\"adL\">\n" +
|
---|
145 | "\n" +
|
---|
146 | "</div></div>";
|
---|
147 | }
|
---|
148 | }
|
---|