|
|
|
@ -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()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|