Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket event handlers #52

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.flowinquiry.config;

import java.util.concurrent.Executor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
Expand Down Expand Up @@ -28,25 +27,13 @@ public AsyncConfiguration(TaskExecutionProperties taskExecutionProperties) {
this.taskExecutionProperties = taskExecutionProperties;
}

@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
LOG.debug("Creating Async Task Executor");
@Bean(name = "asyncTaskExecutor")
public TaskExecutor auditLogTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(taskExecutionProperties.getPool().getCoreSize());
executor.setMaxPoolSize(taskExecutionProperties.getPool().getMaxSize());
executor.setQueueCapacity(taskExecutionProperties.getPool().getQueueCapacity());
executor.setThreadNamePrefix(taskExecutionProperties.getThreadNamePrefix());
return executor;
}

@Bean(name = "auditLogExecutor")
public TaskExecutor auditLogTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("SecurityContextAsync-");
executor.initialize();

// Wrap the executor with DelegatingSecurityContextTaskExecutor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.flowinquiry.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/** Provide the door to let some non-spring class can retrieve or call a spring component */
@Component
public class SpringContextProvider implements ApplicationContextAware {

private static ApplicationContext context;

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}

public static ApplicationContext getContext() {
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AuditLogUpdateEventListener(
this.applicationContext = applicationContext;
}

@Async("auditLogExecutor")
@Async("asyncTaskExecutor")
@Transactional
@EventListener
public void onNewTeamRequestCreated(AuditLogUpdateEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.flowinquiry.modules.collab;

import io.flowinquiry.config.FlowInquiryProperties;
import io.flowinquiry.config.SpringContextProvider;
import io.flowinquiry.modules.usermanagement.service.dto.UserDTO;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.thymeleaf.context.Context;

public class EmailContext {
private static final String BASE_URL = "baseUrl";

private UserDTO toUser;
private final Context thymeleafContext;
private final MessageSource messageSource;
private final Locale locale;
private String subject;
private String templateName;

public EmailContext(Locale locale) {
this.locale = locale;
thymeleafContext = new Context(locale);

FlowInquiryProperties properties =
SpringContextProvider.getContext().getBean(FlowInquiryProperties.class);
messageSource = SpringContextProvider.getContext().getBean(MessageSource.class);
thymeleafContext.setVariable(BASE_URL, properties.getMail().getBaseUrl());
}

public EmailContext setToUser(UserDTO toUser) {
this.toUser = toUser;
return this;
}

public EmailContext addVariable(String name, Object value) {
thymeleafContext.setVariable(name, value);
return this;
}

public EmailContext setTemplate(String template) {
this.templateName = template;
return this;
}

public EmailContext setSubject(String title) {
return setSubject(title, (Object) null);
}

public EmailContext setSubject(String title, Object... args) {
this.subject = messageSource.getMessage(title, args, locale);
return this;
}

public UserDTO getToUser() {
return toUser;
}

public String getSubject() {
return subject;
}

public String getTemplate() {
return templateName;
}

public Context getThymeleafContext() {
return thymeleafContext;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.flowinquiry.modules.collab.service;

import io.flowinquiry.config.FlowInquiryProperties;
import io.flowinquiry.modules.collab.EmailContext;
import io.flowinquiry.modules.usermanagement.service.dto.UserDTO;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
Expand Down Expand Up @@ -106,6 +107,29 @@ public void sendEmailFromTemplate(UserDTO user, String templateName, String titl
this.sendEmail(user.getEmail(), subject, content, false, true);
}

@Async
public void sendEmail(EmailContext emailContext) {
if (emailContext.getToUser() == null
|| emailContext.getToUser().getEmail() == null
|| emailContext.getSubject() == null) {
LOG.debug(
"Email doesn't exist for user '{}' or no subject for email context '{}'",
emailContext.getToUser(),
emailContext.getSubject());
return;
}
emailContext.addVariable(USER, emailContext.getToUser());
String content =
templateEngine.process(
emailContext.getTemplate(), emailContext.getThymeleafContext());
this.sendEmail(
emailContext.getToUser().getEmail(),
emailContext.getSubject(),
content,
false,
true);
}

@Async
public void sendActivationEmail(UserDTO user) {
LOG.debug("Sending activation email to '{}'", user.getEmail());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -83,4 +86,11 @@ public class TeamRequest extends AbstractAuditingEntity<Long> {

@Column(name = "is_completed", nullable = false)
private Boolean isCompleted = false;

@ManyToMany
@JoinTable(
name = "fw_team_request_watchers",
joinColumns = @JoinColumn(name = "team_request_id"),
inverseJoinColumns = @JoinColumn(name = "user_id"))
private Set<User> watchers;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.flowinquiry.modules.teams.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.ZonedDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "fw_team_request_watchers")
@EqualsAndHashCode
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TeamRequestWatcher {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "team_request_id", nullable = false)
private Long teamRequestId;

@Column(name = "user_id", nullable = false)
private Long userId;

@Column(name = "created_at", nullable = false)
private ZonedDateTime createdAt;

@Column(name = "created_by", nullable = false)
private Long createdBy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public interface TeamRequestRepository
"assignUser",
"modifiedByUser",
"workflow",
"currentState"
"currentState",
"watchers"
})
Optional<TeamRequest> findById(@Param("id") Long id);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.flowinquiry.modules.teams.repository;

import io.flowinquiry.modules.teams.domain.TeamRequestWatcher;
import io.flowinquiry.modules.teams.service.dto.WatcherDTO;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface TeamRequestWatcherRepository extends JpaRepository<TeamRequestWatcher, Long> {

@Query(
"SELECT new io.flowinquiry.modules.teams.service.dto.WatcherDTO(u.id, u.firstName, u.lastName, u.imageUrl, u.email) "
+ "FROM TeamRequestWatcher trw "
+ "JOIN User u ON trw.userId = u.id "
+ "WHERE trw.teamRequestId = :teamRequestId")
List<WatcherDTO> findWatchersByRequestId(@Param("teamRequestId") Long teamRequestId);
}
Loading
Loading