Merge branch '119-notification-grants' into 'dev'
Resolve "Реализовать уведомления на почту" Closes #119 See merge request romanov73/ng-tracker!84environments/staging/deployments/55
commit
ff7f535980
@ -0,0 +1,72 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.core.util.DateUtils;
|
||||
import ru.ulstu.grant.model.Grant;
|
||||
import ru.ulstu.user.model.User;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class GrantNotificationService {
|
||||
private final static int DAYS_TO_DEADLINE_NOTIFICATION = 7;
|
||||
private final static String TEMPLATE_DEADLINE = "grantDeadlineNotification";
|
||||
private final static String TEMPLATE_CREATE = "grantCreateNotification";
|
||||
private final static String TEMPLATE_AUTHORS_CHANGED = "grantAuthorsChangeNotification";
|
||||
private final static String TEMPLATE_LEADER_CHANGED = "grantLeaderChangeNotification";
|
||||
|
||||
private final static String TITLE_DEADLINE = "Приближается дедлайн гранта: %s";
|
||||
private final static String TITLE_CREATE = "Создан грант: %s";
|
||||
private final static String TITLE_AUTHORS_CHANGED = "Изменился состав рабочей группы гранта: %s";
|
||||
private final static String TITLE_LEADER_CHANGED = "Изменился руководитель гранта: %s";
|
||||
|
||||
private final MailService mailService;
|
||||
|
||||
public GrantNotificationService(MailService mailService) {
|
||||
this.mailService = mailService;
|
||||
}
|
||||
|
||||
public void sendDeadlineNotifications(List<Grant> grants, boolean isDeadlineBeforeWeek) {
|
||||
Date now = DateUtils.addDays(new Date(), DAYS_TO_DEADLINE_NOTIFICATION);
|
||||
grants.stream()
|
||||
.filter(grant -> needToSendDeadlineNotification(grant, now, isDeadlineBeforeWeek))
|
||||
.forEach(grant -> sendMessageDeadline(grant));
|
||||
}
|
||||
|
||||
private boolean needToSendDeadlineNotification(Grant grant, Date compareDate, boolean isDeadlineBeforeWeek) {
|
||||
return (grant.getNextDeadline().isPresent())
|
||||
&& (compareDate.before(grant.getNextDeadline().get().getDate()) && isDeadlineBeforeWeek
|
||||
|| compareDate.after(grant.getNextDeadline().get().getDate()) && !isDeadlineBeforeWeek)
|
||||
&& grant.getNextDeadline().get().getDate().after(new Date());
|
||||
}
|
||||
|
||||
private void sendMessageDeadline(Grant grant) {
|
||||
Map<String, Object> variables = ImmutableMap.of("grant", grant);
|
||||
sendForAllAuthors(variables, grant, TEMPLATE_DEADLINE, String.format(TITLE_DEADLINE, grant.getTitle()));
|
||||
}
|
||||
|
||||
public void sendCreateNotification(Grant grant) {
|
||||
Map<String, Object> variables = ImmutableMap.of("grant", grant);
|
||||
sendForAllAuthors(variables, grant, TEMPLATE_CREATE, String.format(TITLE_CREATE, grant.getTitle()));
|
||||
}
|
||||
|
||||
public void sendAuthorsChangeNotification(Grant grant, Set<User> oldAuthors) {
|
||||
Map<String, Object> variables = ImmutableMap.of("grant", grant, "oldAuthors", oldAuthors);
|
||||
sendForAllAuthors(variables, grant, TEMPLATE_AUTHORS_CHANGED, String.format(TITLE_AUTHORS_CHANGED, grant.getTitle()));
|
||||
}
|
||||
|
||||
public void sendLeaderChangeNotification(Grant grant, User oldLeader) {
|
||||
Map<String, Object> variables = ImmutableMap.of("grant", grant, "oldLeader", oldLeader);
|
||||
sendForAllAuthors(variables, grant, TEMPLATE_LEADER_CHANGED, String.format(TITLE_LEADER_CHANGED, grant.getTitle()));
|
||||
}
|
||||
private void sendForAllAuthors(Map<String, Object> variables, Grant grant, String template, String title) {
|
||||
Set<User> allAuthors = grant.getAuthors();
|
||||
allAuthors.forEach(author -> mailService.sendEmailFromTemplate(variables, author, template, title));
|
||||
mailService.sendEmailFromTemplate(variables, grant.getLeader(), template, title);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ru.ulstu.grant.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class GrantScheduler {
|
||||
private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true;
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(GrantScheduler.class);
|
||||
|
||||
private final GrantNotificationService grantNotificationService;
|
||||
private final GrantService grantService;
|
||||
|
||||
public GrantScheduler(GrantNotificationService grantNotificationService,
|
||||
GrantService grantService) {
|
||||
this.grantNotificationService = grantNotificationService;
|
||||
this.grantService = grantService;
|
||||
}
|
||||
|
||||
|
||||
@Scheduled(cron = "0 0 8 * * MON", zone = "Europe/Samara")
|
||||
public void checkDeadlineBeforeWeek() {
|
||||
log.debug("GrantScheduler.checkDeadlineBeforeWeek started");
|
||||
grantNotificationService.sendDeadlineNotifications(grantService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK);
|
||||
log.debug("GrantScheduler.checkDeadlineBeforeWeek finished");
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||
<changeSet author="tanya" id="20190507_000000-1">
|
||||
<dropColumn columnName="deadline_date" tableName="grants"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
Loading…
Reference in New Issue