package parkup.configs; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import parkup.configs.email.EmailSender; import parkup.configs.email.EmailValidator; import parkup.configs.token.ConfirmationTokenW; import parkup.configs.token.ConfirmationTokenService; import parkup.entities.Vraboten; import parkup.services.VrabotenService; import javax.transaction.Transactional; import java.time.LocalDateTime; @Service public class RegistrationServiceW { private final VrabotenService vrabotenService; private final EmailValidator emailValidator; private final ConfirmationTokenService confirmationTokenService; private final EmailSender emailSender; @Autowired public RegistrationServiceW(VrabotenService vrabotenService, EmailValidator emailValidator, ConfirmationTokenService confirmationTokenService, EmailSender emailSender) { this.vrabotenService = vrabotenService; this.emailValidator = emailValidator; this.confirmationTokenService = confirmationTokenService; this.emailSender = emailSender; } public String register(RegistrationRequest request) { boolean isValidEmail = emailValidator. test(request.getEmail()); if (!isValidEmail) { throw new IllegalStateException("email not valid"); } String token = vrabotenService.signUpVraboten( new Vraboten( request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), request.getMobile() ) ); String link = "http://localhost:8080/vraboten/registration/confirm?token=" + token; emailSender.send( request.getEmail(), buildEmail(request.getFirstName(), link)); return token; } @Transactional public String confirmToken(String token) { ConfirmationTokenW confirmationTokenW = confirmationTokenService .getTokenW(token) .orElseThrow(() -> new IllegalStateException("token not found")); if (confirmationTokenW.getConfirmedAt() != null) { throw new IllegalStateException("email already confirmed"); } LocalDateTime expiredAt = confirmationTokenW.getExpiresAt(); if (expiredAt.isBefore(LocalDateTime.now())) { throw new IllegalStateException("token expired"); } confirmationTokenService.setConfirmedAtW(token); vrabotenService.enableVraboten(confirmationTokenW.getRegistriranParkirac().getEmail()); return "confirmed"; } private String buildEmail(String name, String link) { return "
\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " Confirm your email\n" + "
\n" + " \n" + "
\n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + "
\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "


\n" + " \n" + "

Hi " + name + ",

Thank you for registering. Please click on the below link to activate your account:

Activate Now

\n Link will expire in 15 minutes.

See you soon

" + " \n" + "


\n" + "\n" + "
"; } }