Skip to content

Commit

Permalink
EVA-3529 Notify submission update to user through email (#10)
Browse files Browse the repository at this point in the history
* send mail on status update
  • Loading branch information
nitin-ebi authored Mar 26, 2024
1 parent 27f4d21 commit 6b1b342
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/uk/ac/ebi/eva/submission/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.ac.ebi.eva.submission.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class AppConfig {

@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.ebi.ac.uk");
javaMailSender.setPort(25);
return javaMailSender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import uk.ac.ebi.eva.submission.controller.BaseController;
import uk.ac.ebi.eva.submission.entity.Submission;
import uk.ac.ebi.eva.submission.entity.SubmissionAccount;
import uk.ac.ebi.eva.submission.model.SubmissionStatus;
import uk.ac.ebi.eva.submission.service.LsriTokenService;
import uk.ac.ebi.eva.submission.service.SubmissionService;
import uk.ac.ebi.eva.submission.service.WebinTokenService;
Expand Down Expand Up @@ -87,6 +88,7 @@ public ResponseEntity<?> markSubmissionUploaded(@RequestHeader("Authorization")
}

Submission submission = this.submissionService.markSubmissionUploaded(submissionId);
submissionService.sendMailNotificationForStatusUpdate(submissionAccount, submissionId, SubmissionStatus.UPLOADED, true);
return new ResponseEntity<>(stripUserDetails(submission), HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import uk.ac.ebi.eva.submission.exception.SubmissionDoesNotExistException;
import uk.ac.ebi.eva.submission.entity.Submission;
import uk.ac.ebi.eva.submission.entity.SubmissionAccount;
import uk.ac.ebi.eva.submission.exception.SubmissionDoesNotExistException;
import uk.ac.ebi.eva.submission.model.SubmissionStatus;
import uk.ac.ebi.eva.submission.repository.SubmissionAccountRepository;
import uk.ac.ebi.eva.submission.repository.SubmissionDetailsRepository;
import uk.ac.ebi.eva.submission.repository.SubmissionRepository;
import uk.ac.ebi.eva.submission.util.EmailNotificationHelper;
import uk.ac.ebi.eva.submission.util.HTMLHelper;
import uk.ac.ebi.eva.submission.util.MailSender;

import java.time.LocalDateTime;
import java.util.Optional;
Expand All @@ -25,17 +28,24 @@ public class SubmissionService {

private final GlobusDirectoryProvisioner globusDirectoryProvisioner;

private final MailSender mailSender;

@Value("${globus.uploadHttpDomain}")
private String uploadHttpDomain;

private EmailNotificationHelper emailHelper;

public SubmissionService(SubmissionRepository submissionRepository,
SubmissionAccountRepository submissionAccountRepository,
SubmissionDetailsRepository submissionDetailsRepository,
GlobusDirectoryProvisioner globusDirectoryProvisioner) {
GlobusDirectoryProvisioner globusDirectoryProvisioner,
MailSender mailSender, EmailNotificationHelper emailHelper) {
this.submissionRepository = submissionRepository;
this.submissionAccountRepository = submissionAccountRepository;
this.submissionDetailsRepository = submissionDetailsRepository;
this.globusDirectoryProvisioner = globusDirectoryProvisioner;
this.mailSender = mailSender;
this.emailHelper = emailHelper;
}

public Submission initiateSubmission(SubmissionAccount submissionAccount) {
Expand All @@ -45,7 +55,7 @@ public Submission initiateSubmission(SubmissionAccount submissionAccount) {

Optional<SubmissionAccount> optSubmissionAccount = submissionAccountRepository.findById(submissionAccount.getId());
// if the user account is not present or if its primary email has changed, save/update the user account
if (!optSubmissionAccount.isPresent() || optSubmissionAccount.get().getPrimaryEmail() != submissionAccount.getPrimaryEmail()) {
if (!optSubmissionAccount.isPresent() || !optSubmissionAccount.get().getPrimaryEmail().equals(submissionAccount.getPrimaryEmail())) {
submissionAccountRepository.save(submissionAccount);
}

Expand Down Expand Up @@ -85,9 +95,17 @@ public boolean checkUserHasAccessToSubmission(SubmissionAccount account, String
Optional<Submission> optSubmission = submissionRepository.findById(submissionId);
if (optSubmission.isPresent()) {
SubmissionAccount submissionAccount = optSubmission.get().getSubmissionAccount();
return submissionAccount.getId() == account.getId();
return submissionAccount.getId().equals(account.getId());
} else {
throw new SubmissionDoesNotExistException("Given submission with id " + submissionId + " does not exist");
}
}

public void sendMailNotificationForStatusUpdate(SubmissionAccount submissionAccount, String submissionId,
SubmissionStatus submissionStatus, boolean success) {
String sendTo = submissionAccount.getPrimaryEmail();
String subject = emailHelper.getSubjectForSubmissionStatusUpdate(submissionStatus, success);
String body = emailHelper.getTextForSubmissionStatusUpdate(submissionAccount, submissionId, submissionStatus, success);
mailSender.sendEmail(sendTo, subject, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package uk.ac.ebi.eva.submission.util;

import org.springframework.stereotype.Component;
import uk.ac.ebi.eva.submission.entity.SubmissionAccount;
import uk.ac.ebi.eva.submission.model.SubmissionStatus;

@Component
public class EmailNotificationHelper {
private static final String EVA_HELPDESK_EMAIL = "eva-helpdesk@ebi.ac.uk";

public String getSubjectForSubmissionStatusUpdate(SubmissionStatus submissionStatus, boolean success) {
String result = (success == true) ? "SUCCESS" : "FAILED";
return String.format("EVA Submission Update: %s %s", submissionStatus, result);
}

public String getTextForSubmissionStatusUpdate(SubmissionAccount submissionAccount, String submissionId,
SubmissionStatus submissionStatus, boolean success) {
String result;
String resultColor;
if (success) {
result = "SUCCESS";
resultColor = "green";
} else {
result = "FAILED";
resultColor = "red";
}

String notificationText = new HTMLHelper()
.addText("Dear " + submissionAccount.getFirstName() + ",")
.addGap(1)
.addText("Here is the update for your submission: ")
.addGap(1)
.addText("submission ID: " + submissionId)
.addLineBreak()
.addText("Submission Status: " + submissionStatus)
.addLineBreak()
.addText("Result: ")
.addBoldTextWithColor(result, resultColor)
.addGap(2)
.build();

notificationText += getNotificationFooter();

return notificationText;
}

public String getNotificationFooter() {
return new HTMLHelper()
.addTextWithSize("Please don't reply to this email.", 10)
.addLineBreak()
.addTextWithSize("For any issues/support please contact us at ", 10)
.addEmailLinkWithSize(EVA_HELPDESK_EMAIL, EVA_HELPDESK_EMAIL, 10)
.addLineBreak()
.addTextWithSize("European Variation Archive: EMBL-EBI", 10)
.build();

}
}
66 changes: 66 additions & 0 deletions src/main/java/uk/ac/ebi/eva/submission/util/HTMLHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package uk.ac.ebi.eva.submission.util;

import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class HTMLHelper {
private final StringBuilder htmlBuilder;

public HTMLHelper() {
htmlBuilder = new StringBuilder();
}

public HTMLHelper addLineBreak() {
htmlBuilder.append("<br />");
return this;
}

public HTMLHelper addGap(int count) {
htmlBuilder.append(IntStream.range(0, count+1).boxed().map(i -> "<br />").collect(Collectors.joining("")));
return this;
}

public HTMLHelper addText(String text) {
htmlBuilder.append(text);
return this;
}

public HTMLHelper addTextWithSize(String text, int size) {
htmlBuilder.append("<span style=\"font-size:" + size + "px;\">" + text + "</span>");
return this;
}

public HTMLHelper addTextWithColor(String text, String color) {
htmlBuilder.append("<span style=\"color:" + color + ";\">" + text + "</span>");
return this;
}

public HTMLHelper addBoldText(String text) {
htmlBuilder.append("<b>" + text + "</b>");
return this;
}

public HTMLHelper addLink(String url, String text) {
htmlBuilder.append("<a href=\"" + url + "\">" + text + "</a>");
return this;
}

public HTMLHelper addEmailLink(String email, String text) {
htmlBuilder.append("<a href=\"mailto:" + email + "\">" + text + "</a>");
return this;
}

public HTMLHelper addEmailLinkWithSize(String email, String text, int size) {
htmlBuilder.append("<span style=\"font-size:" + size + "px;\"> <a href=\"mailto:" + email + "\">" + text + "</a> </span>");
return this;
}

public HTMLHelper addBoldTextWithColor(String text, String color) {
htmlBuilder.append("<b><span style=\"color:" + color + ";\">" + text + "</span></b>");
return this;
}

public String build() {
return htmlBuilder.toString();
}
}
41 changes: 41 additions & 0 deletions src/main/java/uk/ac/ebi/eva/submission/util/MailSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package uk.ac.ebi.eva.submission.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailSender {
private final Logger logger = LoggerFactory.getLogger(MailSender.class);
private final JavaMailSender javaMailSender;
private final String DEFAULT_SENDER = "eva_submissions@ebi.ac.uk";

@Autowired
MailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}

public void sendEmail(String to, String subject, String body) {
sendEmail(DEFAULT_SENDER, to, subject, body);
}

public void sendEmail(String from, String to, String subject, String body) {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
try {
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body, true);
javaMailSender.send(message);
} catch (MessagingException e) {
logger.error("Error sending mail: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import uk.ac.ebi.eva.submission.service.LoginMethod;
import uk.ac.ebi.eva.submission.service.LsriTokenService;
import uk.ac.ebi.eva.submission.service.WebinTokenService;
import uk.ac.ebi.eva.submission.util.MailSender;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -76,6 +77,9 @@ public class SubmissionWSIntegrationTest {
@MockBean
private GlobusDirectoryProvisioner globusDirectoryProvisioner;

@MockBean
private MailSender mailSender;

@Container
static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:9.6")
.withInitScript("init.sql");
Expand Down Expand Up @@ -230,6 +234,7 @@ public void testMarkSubmissionUploaded() throws Exception {
String userToken = "webinUserToken";
SubmissionAccount submissionAccount = getWebinUserAccount();
when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);
doNothing().when(mailSender).sendEmail(anyString(), anyString(), anyString());

String submissionId = createNewSubmissionEntry(submissionAccount);

Expand Down
Loading

0 comments on commit 6b1b342

Please sign in to comment.