1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
|
4
|
import eu.dnetlib.uoaadmintools.dao.NotificationsDAO;
|
5
|
import eu.dnetlib.uoaadmintools.emailSender.EmailSender;
|
6
|
import eu.dnetlib.uoaadmintools.entities.Email;
|
7
|
import eu.dnetlib.uoaadmintools.entities.Notifications;
|
8
|
import eu.dnetlib.uoaadmintools.handlers.NotFoundException;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.web.bind.annotation.*;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
@RestController
|
17
|
@CrossOrigin(origins = "*")
|
18
|
public class EmailController {
|
19
|
private final Logger log = Logger.getLogger(this.getClass());
|
20
|
|
21
|
@Autowired
|
22
|
private EmailSender emailSender;
|
23
|
@Autowired
|
24
|
private NotificationsDAO notificationsDAO;
|
25
|
@Autowired
|
26
|
private CommunityDAO communityDAO;
|
27
|
//invitation // no check
|
28
|
//welcome new manager - no check
|
29
|
//notify about new manager - check manager option
|
30
|
//notify about new subscribers - check manager option
|
31
|
//notify for claims - check manager option --> claim API
|
32
|
@RequestMapping(value = "/email", method = RequestMethod.POST)
|
33
|
public Boolean sendEmail(@RequestBody Email email ) throws Exception {
|
34
|
for(String userMail:email.getRecipients()){
|
35
|
ArrayList<String> sendTo = new ArrayList<>();
|
36
|
sendTo.add(userMail);
|
37
|
emailSender.send(sendTo,email.getSubject(),email.getBody());
|
38
|
}
|
39
|
|
40
|
return true;
|
41
|
}
|
42
|
|
43
|
@RequestMapping(value = "/notifyNewManagers/{pid}", method = RequestMethod.POST)
|
44
|
public Boolean notifyNewManagers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
|
45
|
List<String> notifyrecipients = new ArrayList<String>();
|
46
|
if(communityDAO.findByPid(pid) == null){
|
47
|
throw new NotFoundException("Community not found");
|
48
|
}
|
49
|
for(String user:email.getRecipients()){
|
50
|
Notifications userNotifications = notificationsDAO.findByManagerEmailAndCommunityPid(user,pid);
|
51
|
|
52
|
if(userNotifications == null || userNotifications.getNotifyForNewManagers()){
|
53
|
notifyrecipients.add(user);
|
54
|
}
|
55
|
}
|
56
|
if(notifyrecipients.size() > 0){
|
57
|
emailSender.send(notifyrecipients,email.getSubject(),email.getBody());
|
58
|
}else{
|
59
|
log.debug("There are no users to notify ");
|
60
|
}
|
61
|
|
62
|
return true;
|
63
|
}
|
64
|
@RequestMapping(value = "/notifyNewSubscribers/{pid}", method = RequestMethod.POST)
|
65
|
public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
|
66
|
List<String> notifyrecipients = new ArrayList<String>();
|
67
|
if(communityDAO.findByPid(pid) == null){
|
68
|
throw new NotFoundException("Community not found");
|
69
|
}
|
70
|
for(String user:email.getRecipients()){
|
71
|
Notifications userNotifications = notificationsDAO.findByManagerEmailAndCommunityPid(user,pid);
|
72
|
|
73
|
if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){
|
74
|
notifyrecipients.add(user);
|
75
|
}
|
76
|
}
|
77
|
if(notifyrecipients.size() > 0){
|
78
|
emailSender.send(notifyrecipients,email.getSubject(),email.getBody());
|
79
|
}else{
|
80
|
log.debug("There are no users to notify ");
|
81
|
}
|
82
|
|
83
|
return true;
|
84
|
}
|
85
|
|
86
|
}
|