sending emails

This commit is contained in:
alex 2024-08-06 22:46:31 -04:00
parent cd7ea79e95
commit 58490ded0f
6 changed files with 59 additions and 4 deletions

View File

@ -68,6 +68,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>

View File

@ -5,7 +5,10 @@ import com.primefactorsolutions.repositories.AssessmentRepository;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A; import org.checkerframework.checker.units.qual.A;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.swing.text.html.Option; import javax.swing.text.html.Option;
@ -18,10 +21,12 @@ import java.util.stream.Collectors;
@Service @Service
@Data @Data
@Slf4j
public class AssessmentService { public class AssessmentService {
private final AssessmentRepository assessmentRepository; private final AssessmentRepository assessmentRepository;
private final EntityManager entityManager; private final EntityManager entityManager;
private final JavaMailSender emailSender;
public Assessment createOrUpdate(final Assessment assessment) { public Assessment createOrUpdate(final Assessment assessment) {
final Assessment saved = assessmentRepository.save(assessment); final Assessment saved = assessmentRepository.save(assessment);
@ -29,6 +34,26 @@ public class AssessmentService {
return saved; return saved;
} }
public void sendEmail(final Assessment assessment) {
try {
final String evaluationLink = String.format("https://careers.primefactorsolutions.com/evaluation/%s", assessment.getId());
final SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("no-reply@primefactorsolutions.com");
message.setTo(assessment.getAppUser().getEmail());
message.setSubject("PFS - Evaluacion Tecnica");
message.setText(String.format("Estimado candidato,\n\nGracias por su candidatura. En esta etapa del proceso de seleccion, usted debe completar "
+ "una evaluacion tecnica de programacion en JAVA. La prueba tiene una duracion de 30 minutos y puede completarla cuando tenga una buena "
+ "conexion de internet.\n\n"
+ "Haga click aca: " + evaluationLink + "\n\n"
+ "Exito!"));
emailSender.send(message);
} catch (Exception e) {
log.error("Error sending email", e);
throw e;
}
}
public List<Assessment> getAssessments() { public List<Assessment> getAssessments() {
return assessmentRepository.findAll(); return assessmentRepository.findAll();
} }

View File

@ -5,10 +5,10 @@ import com.primefactorsolutions.service.AssessmentService;
import com.vaadin.flow.component.Component; import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Main; import com.vaadin.flow.component.html.Main;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.data.provider.DataProvider; import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.DataProviderListener; import com.vaadin.flow.data.provider.DataProviderListener;
import com.vaadin.flow.data.provider.Query; import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.data.renderer.ComponentRenderer;
import com.vaadin.flow.function.ValueProvider; import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
@ -34,6 +34,13 @@ public class AssessmentsListView extends Main {
grid.addComponentColumn((ValueProvider<Assessment, Component>) assessment -> new Button("Copy", event -> grid.addComponentColumn((ValueProvider<Assessment, Component>) assessment -> new Button("Copy", event ->
ClientsideClipboard.writeToClipboard(String.format("https://localhost:8080/evaluation/%s", assessment.getId())) ClientsideClipboard.writeToClipboard(String.format("https://localhost:8080/evaluation/%s", assessment.getId()))
)); ));
grid.addComponentColumn((ValueProvider<Assessment, Component>) assessment -> new Button("Send", event -> {
try {
assessmentService.sendEmail(assessment);
} catch (Exception e) {
Notification.show("Error sending email: " + e.getMessage(), 10_000, Notification.Position.TOP_CENTER);
}
}));
grid.setDataProvider(new DataProvider<>() { grid.setDataProvider(new DataProvider<>() {
@Override @Override

View File

@ -8,17 +8,23 @@ import com.vaadin.flow.component.html.Header;
import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.Scroller; import com.vaadin.flow.component.orderedlayout.Scroller;
import com.vaadin.flow.component.sidenav.SideNav; import com.vaadin.flow.component.sidenav.SideNav;
import com.vaadin.flow.component.sidenav.SideNavItem;
import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.spring.security.AuthenticationContext;
import com.vaadin.flow.theme.lumo.LumoUtility; import com.vaadin.flow.theme.lumo.LumoUtility;
import org.springframework.security.core.userdetails.UserDetails;
import org.vaadin.lineawesome.LineAwesomeIcon;
/** /**
* The main view is a top-level placeholder for other views. * The main view is a top-level placeholder for other views.
*/ */
public class MainLayout extends AppLayout { public class MainLayout extends AppLayout {
private final transient AuthenticationContext authContext;
private H1 viewTitle; private H1 viewTitle;
public MainLayout() { public MainLayout(AuthenticationContext authContext) {
this.authContext = authContext;
setPrimarySection(Section.DRAWER); setPrimarySection(Section.DRAWER);
addDrawerContent(); addDrawerContent();
addHeaderContent(); addHeaderContent();
@ -47,7 +53,12 @@ public class MainLayout extends AppLayout {
private SideNav createNavigation() { private SideNav createNavigation() {
SideNav nav = new SideNav(); SideNav nav = new SideNav();
//nav.addItem(new SideNavItem("CodeEditor", CodeEditorView.class, LineAwesomeIcon.EDIT.create())); authContext.getAuthenticatedUser(UserDetails.class).ifPresent(u -> {
nav.addItem(new SideNavItem("Home", MainView.class, LineAwesomeIcon.HOME_SOLID.create()));
nav.addItem(new SideNavItem("Assessments", AssessmentsListView.class, LineAwesomeIcon.RIBBON_SOLID.create()));
nav.addItem(new SideNavItem("Users", UsersListView.class, LineAwesomeIcon.USER.create()));
nav.addItem(new SideNavItem("Questions", QuestionsListView.class, LineAwesomeIcon.QUESTION_SOLID.create()));
});
return nav; return nav;
} }

View File

@ -6,7 +6,7 @@ import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.PermitAll;
@PageTitle("Assessment") @PageTitle("Home")
@Route(value = "", layout = MainLayout.class) @Route(value = "", layout = MainLayout.class)
@PermitAll @PermitAll
public class MainView extends Main { public class MainView extends Main {

View File

@ -8,6 +8,14 @@ vaadin.launch-browser=true
# For more information https://vaadin.com/docs/latest/integrations/spring/configuration#special-configuration-parameters # For more information https://vaadin.com/docs/latest/integrations/spring/configuration#special-configuration-parameters
vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin,org.vaadin.firitin,org.vaadin.addons.stefan vaadin.allowed-packages = com.vaadin,org.vaadin,com.primefactorsolutions,com.hilerio.ace,com.flowingcode.vaadin,org.vaadin.firitin,org.vaadin.addons.stefan
spring.mail.host=smtp.primefactorsolutions.com
spring.mail.username=${SMTP_USER}
spring.mail.password=${SMTP_PASS}
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# spring.datasource.url=jdbc:h2:mem:testdb # spring.datasource.url=jdbc:h2:mem:testdb
# spring.datasource.url=jdbc:h2:file:./db # spring.datasource.url=jdbc:h2:file:./db