send notification
This commit is contained in:
parent
aa919906aa
commit
da1fbc3124
@ -6,7 +6,10 @@ import ru.ulstu.user.model.User;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -15,15 +18,21 @@ public class Event extends BaseEntity {
|
||||
@NotBlank
|
||||
private String title;
|
||||
|
||||
@Column(name = "execute_date")
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date executeDate;
|
||||
|
||||
@Column(name = "create_date")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date createDate;
|
||||
|
||||
@Column(name = "update_date")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date updateDate;
|
||||
|
||||
private String description;
|
||||
|
||||
@ManyToMany
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
private List<User> recipients;
|
||||
|
||||
public String getTitle() {
|
||||
@ -65,4 +74,12 @@ public class Event extends BaseEntity {
|
||||
public void setRecipients(List<User> recipients) {
|
||||
this.recipients = recipients;
|
||||
}
|
||||
|
||||
public Date getExecuteDate() {
|
||||
return executeDate;
|
||||
}
|
||||
|
||||
public void setExecuteDate(Date executeDate) {
|
||||
this.executeDate = executeDate;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
public class EventDto {
|
||||
private final Integer id;
|
||||
private final String title;
|
||||
private final Date executeDate;
|
||||
private final Date createDate;
|
||||
private final Date updateDate;
|
||||
private final String description;
|
||||
@ -20,12 +21,14 @@ public class EventDto {
|
||||
@JsonCreator
|
||||
public EventDto(@JsonProperty("id") Integer id,
|
||||
@JsonProperty("title") String title,
|
||||
@JsonProperty("executeDate") Date executeDate,
|
||||
@JsonProperty("createDate") Date createDate,
|
||||
@JsonProperty("updateDate") Date updateDate,
|
||||
@JsonProperty("description") String description,
|
||||
@JsonProperty("recipients") List<UserDto> recipients) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.executeDate = executeDate;
|
||||
this.createDate = createDate;
|
||||
this.updateDate = updateDate;
|
||||
this.description = description;
|
||||
@ -35,6 +38,7 @@ public class EventDto {
|
||||
public EventDto(Event event) {
|
||||
this.id = event.getId();
|
||||
this.title = event.getTitle();
|
||||
this.executeDate = event.getExecuteDate();
|
||||
this.createDate = event.getCreateDate();
|
||||
this.updateDate = event.getUpdateDate();
|
||||
this.description = event.getDescription();
|
||||
@ -64,4 +68,8 @@ public class EventDto {
|
||||
public List<UserDto> getRecipients() {
|
||||
return recipients;
|
||||
}
|
||||
|
||||
public Date getExecuteDate() {
|
||||
return executeDate;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,12 @@
|
||||
package ru.ulstu.timeline.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import ru.ulstu.timeline.model.Event;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EventRepository extends JpaRepository<Event, Integer> {
|
||||
@Query("SELECT e FROM Event e WHERE e.executeDate = CURRENT_DATE")
|
||||
List<Event> findByCurrentDate();
|
||||
}
|
||||
|
@ -1,15 +1,18 @@
|
||||
package ru.ulstu.timeline.service;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.ulstu.timeline.model.Event;
|
||||
import ru.ulstu.timeline.model.EventDto;
|
||||
import ru.ulstu.timeline.repository.EventRepository;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.model.UserDto;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
|
||||
@ -18,11 +21,14 @@ public class EventService {
|
||||
|
||||
private final EventRepository eventRepository;
|
||||
private final UserService userService;
|
||||
private final MailService mailService;
|
||||
|
||||
public EventService(EventRepository eventRepository,
|
||||
UserService userService) {
|
||||
UserService userService,
|
||||
MailService mailService) {
|
||||
this.eventRepository = eventRepository;
|
||||
this.userService = userService;
|
||||
this.mailService = mailService;
|
||||
}
|
||||
|
||||
public List<EventDto> findAll() {
|
||||
@ -35,6 +41,7 @@ public class EventService {
|
||||
}
|
||||
|
||||
private Event copyFromDto(Event event, EventDto eventDto) {
|
||||
event.setExecuteDate(eventDto.getExecuteDate());
|
||||
event.setCreateDate(eventDto.getCreateDate());
|
||||
event.setDescription(eventDto.getDescription());
|
||||
event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
|
||||
@ -58,4 +65,14 @@ public class EventService {
|
||||
public List<Event> findByIds(List<Integer> ids) {
|
||||
return eventRepository.findAll(ids);
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 8 1 * * ?")
|
||||
public void sendNotifications() {
|
||||
List<Event> events = eventRepository.findByCurrentDate();
|
||||
events.forEach(event -> {
|
||||
Map<String, Object> variables = ImmutableMap.of("description", event.getDescription());
|
||||
event.getRecipients()
|
||||
.forEach(recipient -> mailService.sendEmailFromTemplate(variables, recipient, "eventNotification", event.getTitle()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ import ru.ulstu.user.model.User;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class MailService {
|
||||
@ -66,6 +67,17 @@ public class MailService {
|
||||
sendEmail(user.getEmail(), subject, content);
|
||||
}
|
||||
|
||||
//Todo: выделить сервис нотификаций
|
||||
@Async
|
||||
public void sendEmailFromTemplate(Map<String, Object> variables, User user, String templateName, String subject) {
|
||||
Context context = new Context();
|
||||
variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue()));
|
||||
context.setVariable(USER, user);
|
||||
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
|
||||
String content = templateEngine.process(templateName, context);
|
||||
sendEmail(user.getEmail(), subject, content);
|
||||
}
|
||||
|
||||
@Async
|
||||
public void sendActivationEmail(User user) {
|
||||
sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE);
|
||||
|
21
src/main/resources/mail_templates/eventNotification.html
Normal file
21
src/main/resources/mail_templates/eventNotification.html
Normal file
@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Уведомление о событии</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
Уважаемый <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
|
||||
</p>
|
||||
<p th:text="${description}">
|
||||
some text
|
||||
</p>
|
||||
<p>
|
||||
Regards,
|
||||
<br/>
|
||||
<em>Balance Team.</em>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user