2020-03-31 14:02:29 +02:00
|
|
|
package eu.dnetlib.uoaadmintools.controllers;
|
|
|
|
|
|
|
|
import eu.dnetlib.uoaadmintools.dao.NotificationsDAO;
|
2021-02-26 15:54:17 +01:00
|
|
|
import eu.dnetlib.uoaadmintools.entities.Manager;
|
2020-03-31 14:02:29 +02:00
|
|
|
import eu.dnetlib.uoaadmintools.entities.Notifications;
|
2021-02-26 15:54:17 +01:00
|
|
|
import eu.dnetlib.uoaadmintools.services.ManagerService;
|
2020-09-24 14:02:39 +02:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.emailSender.EmailSender;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.entities.email.Email;
|
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.entities.email.EmailRecaptcha;
|
2021-02-25 20:00:05 +01:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
|
2020-09-24 14:02:39 +02:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.InvalidReCaptchaException;
|
2021-02-26 15:54:17 +01:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
|
2020-09-24 14:02:39 +02:00
|
|
|
import eu.dnetlib.uoaadmintoolslibrary.recaptcha.VerifyRecaptcha;
|
2020-03-31 14:02:29 +02:00
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin(origins = "*")
|
|
|
|
public class EmailController {
|
|
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private EmailSender emailSender;
|
|
|
|
@Autowired
|
|
|
|
private NotificationsDAO notificationsDAO;
|
|
|
|
@Autowired
|
2020-09-24 14:02:39 +02:00
|
|
|
private PortalDAO portalDAO;
|
2020-03-31 14:02:29 +02:00
|
|
|
@Autowired
|
|
|
|
private VerifyRecaptcha verifyRecaptcha;
|
2021-02-26 15:54:17 +01:00
|
|
|
@Autowired
|
|
|
|
private ManagerService managerService;
|
|
|
|
@Autowired
|
|
|
|
private RolesUtils rolesUtils;
|
2020-03-31 14:02:29 +02:00
|
|
|
|
|
|
|
@RequestMapping(value = "/contact", method = RequestMethod.POST)
|
|
|
|
public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException {
|
2020-09-24 14:02:39 +02:00
|
|
|
if(form.getRecaptcha() != null) {
|
|
|
|
verifyRecaptcha.processResponse(form.getRecaptcha());
|
|
|
|
}
|
2020-03-31 14:02:29 +02:00
|
|
|
Email email = form.getEmail();
|
|
|
|
ArrayList<String> sendTo = new ArrayList<>(email.getRecipients());
|
|
|
|
return emailSender.send(sendTo, email.getSubject(), email.getBody(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/sendMail", method = RequestMethod.POST)
|
|
|
|
public Map<String, ArrayList<String>> sendEmail(@RequestBody Email email,
|
|
|
|
@RequestParam(required = false) Optional<Boolean> optional) {
|
|
|
|
String successString = "success";
|
|
|
|
String failureString = "failure";
|
|
|
|
Map<String, ArrayList<String>> mailResults = new HashMap<>();
|
|
|
|
boolean bcc = (optional.isPresent())?optional.get():true;
|
|
|
|
for(String userMail:email.getRecipients()){
|
|
|
|
ArrayList<String> sendTo = new ArrayList<>();
|
|
|
|
sendTo.add(userMail);
|
|
|
|
boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc);
|
|
|
|
if(success){
|
|
|
|
if(!mailResults.containsKey(successString)) {
|
|
|
|
mailResults.put(successString, new ArrayList<>());
|
|
|
|
}
|
|
|
|
mailResults.get(successString).add(userMail);
|
|
|
|
} else {
|
|
|
|
if(!mailResults.containsKey(failureString)) {
|
|
|
|
mailResults.put(failureString, new ArrayList<>());
|
|
|
|
}
|
|
|
|
mailResults.get(failureString).add(userMail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mailResults;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-04 17:22:37 +01:00
|
|
|
@RequestMapping(value = "/notifyNewManager", method = RequestMethod.POST)
|
|
|
|
public Boolean notifyNewManager(@RequestBody Email email) throws Exception {
|
|
|
|
String userMail = rolesUtils.getEmail();
|
|
|
|
ArrayList<String> sendTo = new ArrayList<>();
|
|
|
|
sendTo.add(userMail);
|
|
|
|
boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), true);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:54:17 +01:00
|
|
|
@RequestMapping(value = "/notifyManagers/{pid}/{newRoleType}", method = RequestMethod.POST)
|
|
|
|
public Boolean notifyManagers(@PathVariable(value = "pid") String pid,
|
|
|
|
@PathVariable(value = "newRoleType") String newRoleType,
|
|
|
|
@RequestBody Email email ) throws Exception {
|
2020-03-31 14:02:29 +02:00
|
|
|
List<String> notifyrecipients = new ArrayList<String>();
|
2020-09-24 14:02:39 +02:00
|
|
|
if(portalDAO.findByPid(pid) == null){
|
|
|
|
throw new ContentNotFoundException("Portal not found");
|
2020-03-31 14:02:29 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 15:54:17 +01:00
|
|
|
Manager[] managers = managerService.getManagers(pid);
|
|
|
|
List<String> emails = new ArrayList();
|
|
|
|
|
|
|
|
for(Manager manager:managers){
|
|
|
|
String userEmail = manager.getEmail();
|
|
|
|
emails.add(userEmail);
|
|
|
|
|
|
|
|
// Do not send the notification to the new manager/ subscriber.
|
|
|
|
if(userEmail.equals(rolesUtils.getEmail())) {
|
|
|
|
continue;
|
2020-03-31 14:02:29 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 15:54:17 +01:00
|
|
|
Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(userEmail,pid);
|
2020-03-31 14:02:29 +02:00
|
|
|
|
2021-02-26 15:54:17 +01:00
|
|
|
if(userNotifications == null
|
|
|
|
|| (newRoleType.equals("manager") && userNotifications.getNotifyForNewManagers())
|
|
|
|
|| (newRoleType.equals("subscriber") && userNotifications.getNotifyForNewSubscribers())){
|
|
|
|
notifyrecipients.add(userEmail);
|
2020-03-31 14:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(notifyrecipients.size() > 0){
|
2021-02-26 15:54:17 +01:00
|
|
|
email.setBody(email.getBody().replace("((__managers__))", String.join(", ", emails)));
|
|
|
|
|
2020-03-31 14:02:29 +02:00
|
|
|
return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
|
|
|
|
}else{
|
|
|
|
log.debug("There are no users to notify ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 15:54:17 +01:00
|
|
|
// @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST)
|
|
|
|
// public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
|
|
|
|
// List<String> notifyrecipients = new ArrayList<String>();
|
|
|
|
// if(portalDAO.findByPid(pid) == null){
|
|
|
|
// throw new ContentNotFoundException("Portal not found");
|
|
|
|
// }
|
|
|
|
// for(String user:email.getRecipients()){
|
|
|
|
// Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid);
|
|
|
|
//
|
|
|
|
// if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){
|
|
|
|
// notifyrecipients.add(user);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if(notifyrecipients.size() > 0){
|
|
|
|
// return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
|
|
|
|
// }else{
|
|
|
|
// log.debug("There are no users to notify ");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return true;
|
|
|
|
// }
|
2020-03-31 14:02:29 +02:00
|
|
|
@RequestMapping(value = "/test", method = RequestMethod.GET)
|
|
|
|
public String test() throws Exception {
|
|
|
|
log.debug("Test mail");
|
|
|
|
List<String> mails = new ArrayList<>();
|
|
|
|
mails.add("argirok@di.uoa.gr");
|
|
|
|
mails.add("argirokokogiannaki@gmail.com");
|
|
|
|
log.debug("Recipients"+mails);
|
|
|
|
|
|
|
|
Email email = new Email();
|
|
|
|
email.setRecipients(mails);
|
|
|
|
email.setBody("Test body");
|
|
|
|
email.setSubject("Test theme");
|
|
|
|
String response = "";
|
2021-02-26 15:54:17 +01:00
|
|
|
response+=this.notifyManagers("ee", "manager", email);
|
2020-03-31 14:02:29 +02:00
|
|
|
log.debug("Notify managers "+response);
|
|
|
|
|
|
|
|
response+=" ";
|
2021-02-26 15:54:17 +01:00
|
|
|
response+=this.notifyManagers("ee", "subscriber", email);
|
2020-03-31 14:02:29 +02:00
|
|
|
log.debug("Notify for subscr "+response);
|
|
|
|
return response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|