[ce6ad22] | 1 | package parkup.configs;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 4 | import org.springframework.stereotype.Service;
|
---|
[9dd526f] | 5 | import parkup.configs.email.EmailService;
|
---|
[ce6ad22] | 6 | import parkup.configs.email.EmailValidator;
|
---|
[9dd526f] | 7 | import parkup.configs.email.Mail;
|
---|
[97fbc67] | 8 | import parkup.configs.token.ConfirmationTokenRP;
|
---|
[ce6ad22] | 9 | import parkup.configs.token.ConfirmationTokenService;
|
---|
[9dd526f] | 10 | import parkup.entities.RegisteredUser;
|
---|
| 11 | import parkup.services.RegisteredUserService;
|
---|
[ce6ad22] | 12 |
|
---|
| 13 | import javax.transaction.Transactional;
|
---|
| 14 | import java.time.LocalDateTime;
|
---|
| 15 |
|
---|
| 16 | @Service
|
---|
[97fbc67] | 17 | public class RegistrationServiceRP {
|
---|
[9dd526f] | 18 | private final RegisteredUserService registeredUserService;
|
---|
[ce6ad22] | 19 | private final EmailValidator emailValidator;
|
---|
| 20 | private final ConfirmationTokenService confirmationTokenService;
|
---|
[9dd526f] | 21 | private final EmailService emailService;
|
---|
[ce6ad22] | 22 |
|
---|
| 23 | @Autowired
|
---|
[9dd526f] | 24 | public RegistrationServiceRP(RegisteredUserService registeredUserService, EmailValidator emailValidator,
|
---|
| 25 | ConfirmationTokenService confirmationTokenService, EmailService emailService) {
|
---|
| 26 | this.registeredUserService = registeredUserService;
|
---|
[ce6ad22] | 27 | this.emailValidator = emailValidator;
|
---|
| 28 | this.confirmationTokenService = confirmationTokenService;
|
---|
[9dd526f] | 29 | this.emailService = emailService;
|
---|
[ce6ad22] | 30 | }
|
---|
| 31 |
|
---|
| 32 | public String register(RegistrationRequest request) {
|
---|
[9dd526f] | 33 | if (request.getEmail() == null || request.getEmail().isEmpty())
|
---|
| 34 | throw new IllegalArgumentException("Missing email");
|
---|
[ce6ad22] | 35 | boolean isValidEmail = emailValidator.
|
---|
| 36 | test(request.getEmail());
|
---|
| 37 |
|
---|
| 38 | if (!isValidEmail) {
|
---|
| 39 | throw new IllegalStateException("email not valid");
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[9dd526f] | 42 | String token = registeredUserService.signUpRegistriranParkirac(
|
---|
| 43 | new RegisteredUser(
|
---|
[ce6ad22] | 44 | request.getFirstName(),
|
---|
| 45 | request.getLastName(),
|
---|
| 46 | request.getEmail(),
|
---|
[97fbc67] | 47 | request.getPassword(),
|
---|
| 48 | request.getMobile()
|
---|
[ce6ad22] | 49 | )
|
---|
| 50 | );
|
---|
| 51 |
|
---|
[97fbc67] | 52 | String link = "http://localhost:8080/registriranParkirac/registration/confirm?token=" + token;
|
---|
[9dd526f] | 53 | Mail mail = new Mail();
|
---|
| 54 | mail.setMailTo(request.getEmail());
|
---|
[ad4da86] | 55 | mail.setMailFrom("noreply_parkup@gmail.com");
|
---|
[9dd526f] | 56 | mail.setMailSubject("Account confirmation");
|
---|
| 57 | mail.setMailContent(buildEmail(request.getEmail(), link));
|
---|
| 58 | emailService.sendEmail(mail);
|
---|
[ce6ad22] | 59 | return token;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | @Transactional
|
---|
| 63 | public String confirmToken(String token) {
|
---|
[97fbc67] | 64 | ConfirmationTokenRP confirmationTokenRP = confirmationTokenService
|
---|
| 65 | .getTokenRP(token)
|
---|
[ce6ad22] | 66 | .orElseThrow(() ->
|
---|
| 67 | new IllegalStateException("token not found"));
|
---|
| 68 |
|
---|
[97fbc67] | 69 | if (confirmationTokenRP.getConfirmedAt() != null) {
|
---|
[ce6ad22] | 70 | throw new IllegalStateException("email already confirmed");
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[97fbc67] | 73 | LocalDateTime expiredAt = confirmationTokenRP.getExpiresAt();
|
---|
[ce6ad22] | 74 |
|
---|
| 75 | if (expiredAt.isBefore(LocalDateTime.now())) {
|
---|
| 76 | throw new IllegalStateException("token expired");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[97fbc67] | 79 | confirmationTokenService.setConfirmedAtRP(token);
|
---|
[ce6ad22] | 80 |
|
---|
[9dd526f] | 81 | registeredUserService.enableRegistriranParkirac(confirmationTokenRP.getRegistriranParkirac().getEmail());
|
---|
[ce6ad22] | 82 | return "confirmed";
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private String buildEmail(String name, String link) {
|
---|
| 86 | return "<div style=\"font-family:Helvetica,Arial,sans-serif;font-size:16px;margin:0;color:#0b0c0c\">\n" +
|
---|
| 87 | "\n" +
|
---|
| 88 | "<span style=\"display:none;font-size:1px;color:#fff;max-height:0\"></span>\n" +
|
---|
| 89 | "\n" +
|
---|
| 90 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;min-width:100%;width:100%!important\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +
|
---|
| 91 | " <tbody><tr>\n" +
|
---|
| 92 | " <td width=\"100%\" height=\"53\" bgcolor=\"#0b0c0c\">\n" +
|
---|
| 93 | " \n" +
|
---|
| 94 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;max-width:580px\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\">\n" +
|
---|
| 95 | " <tbody><tr>\n" +
|
---|
| 96 | " <td width=\"70\" bgcolor=\"#0b0c0c\" valign=\"middle\">\n" +
|
---|
| 97 | " <table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
| 98 | " <tbody><tr>\n" +
|
---|
| 99 | " <td style=\"padding-left:10px\">\n" +
|
---|
| 100 | " \n" +
|
---|
| 101 | " </td>\n" +
|
---|
| 102 | " <td style=\"font-size:28px;line-height:1.315789474;Margin-top:4px;padding-left:10px\">\n" +
|
---|
| 103 | " <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" +
|
---|
| 104 | " </td>\n" +
|
---|
| 105 | " </tr>\n" +
|
---|
| 106 | " </tbody></table>\n" +
|
---|
| 107 | " </a>\n" +
|
---|
| 108 | " </td>\n" +
|
---|
| 109 | " </tr>\n" +
|
---|
| 110 | " </tbody></table>\n" +
|
---|
| 111 | " \n" +
|
---|
| 112 | " </td>\n" +
|
---|
| 113 | " </tr>\n" +
|
---|
| 114 | " </tbody></table>\n" +
|
---|
| 115 | " <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" +
|
---|
| 116 | " <tbody><tr>\n" +
|
---|
| 117 | " <td width=\"10\" height=\"10\" valign=\"middle\"></td>\n" +
|
---|
| 118 | " <td>\n" +
|
---|
| 119 | " \n" +
|
---|
| 120 | " <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
| 121 | " <tbody><tr>\n" +
|
---|
| 122 | " <td bgcolor=\"#1D70B8\" width=\"100%\" height=\"10\"></td>\n" +
|
---|
| 123 | " </tr>\n" +
|
---|
| 124 | " </tbody></table>\n" +
|
---|
| 125 | " \n" +
|
---|
| 126 | " </td>\n" +
|
---|
| 127 | " <td width=\"10\" valign=\"middle\" height=\"10\"></td>\n" +
|
---|
| 128 | " </tr>\n" +
|
---|
| 129 | " </tbody></table>\n" +
|
---|
| 130 | "\n" +
|
---|
| 131 | "\n" +
|
---|
| 132 | "\n" +
|
---|
| 133 | " <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" +
|
---|
| 134 | " <tbody><tr>\n" +
|
---|
| 135 | " <td height=\"30\"><br></td>\n" +
|
---|
| 136 | " </tr>\n" +
|
---|
| 137 | " <tr>\n" +
|
---|
| 138 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
| 139 | " <td style=\"font-family:Helvetica,Arial,sans-serif;font-size:19px;line-height:1.315789474;max-width:560px\">\n" +
|
---|
| 140 | " \n" +
|
---|
| 141 | " <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>" +
|
---|
| 142 | " \n" +
|
---|
| 143 | " </td>\n" +
|
---|
| 144 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
| 145 | " </tr>\n" +
|
---|
| 146 | " <tr>\n" +
|
---|
| 147 | " <td height=\"30\"><br></td>\n" +
|
---|
| 148 | " </tr>\n" +
|
---|
| 149 | " </tbody></table><div class=\"yj6qo\"></div><div class=\"adL\">\n" +
|
---|
| 150 | "\n" +
|
---|
| 151 | "</div></div>";
|
---|
| 152 | }
|
---|
| 153 | }
|
---|