• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DataBiosphere / consent / #5772

29 Apr 2025 01:12PM UTC coverage: 79.983% (+0.5%) from 79.492%
#5772

push

web-flow
DT-1542: Refactor email sending service code (#2499)

172 of 179 new or added lines in 18 files covered. (96.09%)

12 existing lines in 2 files now uncovered.

10257 of 12824 relevant lines covered (79.98%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/src/main/java/org/broadinstitute/consent/http/mail/SendGridAPI.java
1
package org.broadinstitute.consent.http.mail;
2

3
import com.google.api.client.http.HttpStatusCodes;
4
import com.sendgrid.Method;
5
import com.sendgrid.Request;
6
import com.sendgrid.Response;
7
import com.sendgrid.SendGrid;
8
import com.sendgrid.helpers.mail.Mail;
9
import jakarta.ws.rs.WebApplicationException;
10
import java.io.IOException;
11
import java.util.Map;
12
import java.util.Objects;
13
import org.broadinstitute.consent.http.configurations.MailConfiguration;
14
import org.broadinstitute.consent.http.db.UserDAO;
15
import org.broadinstitute.consent.http.models.User;
16
import org.broadinstitute.consent.http.util.ConsentLogger;
17

18
public class SendGridAPI implements ConsentLogger {
19

20
  private final SendGrid sendGrid;
21
  private final boolean activateEmailNotifications;
22

23
  private final UserDAO userDAO;
24

25
  public SendGridAPI(MailConfiguration config, UserDAO userDAO) {
1✔
26
    this.sendGrid = new SendGrid(config.getSendGridApiKey());
1✔
27
    this.activateEmailNotifications = config.isActivateEmailNotifications();
1✔
28
    this.userDAO = userDAO;
1✔
29
  }
1✔
30

31
  /**
32
   * Determine if the user we are sending an email to has set their preference to false or not.
33
   * Users who have been disabled like this should never receive an email.
34
   *
35
   * @param userEmail The email of the user we are sending an email to.
36
   * @return False if the user has explicitly disabled email, True otherwise.
37
   */
38
  private boolean findUserEmailPreference(String userEmail) {
39
    User user = userDAO.findUserByEmail(userEmail);
1✔
40
    if (user == null) {
1✔
41
      logWarn("Unknown user ID: %s".formatted(userEmail));
1✔
42
      return false;
1✔
43
    }
44
    return Objects.requireNonNullElse(user.getEmailPreference(), true);
1✔
45
  }
46

47
  public Response sendMessage(Mail message, String toUserEmail) {
48
    if (!activateEmailNotifications) {
1✔
NEW
49
      return null;
×
50
    }
51
    boolean userEmailPreference = findUserEmailPreference(toUserEmail);
1✔
52
    if (!userEmailPreference) {
1✔
53
      logInfo(
1✔
54
          "User Email Preference has evaluated to 'false', not sending to: %s"
55
              .formatted(toUserEmail));
1✔
56
      return null;
1✔
57
    }
58
    try {
59
      // See https://github.com/sendgrid/sendgrid-java/issues/163
60
      // for what actually works as compared to the documentation - which doesn't.
61
      Request request = new Request();
1✔
62
      request.setMethod(Method.POST);
1✔
63
      request.setBody(message.build());
1✔
64
      // make request
65
      request.setBaseUri(sendGrid.getHost());
1✔
66
      request.setEndpoint("/" + sendGrid.getVersion() + "/mail/send");
1✔
67
      sendGrid.getRequestHeaders().forEach(request::addHeader);
1✔
68
      // send
69
      Response response = sendGrid.makeCall(request);
1✔
70
      if (response.getStatusCode() > 202) {
1✔
71
        // Indicates some form of error:
72
        // https://docs.sendgrid.com/api-reference/mail-send/mail-send#responses
73
        logException(
1✔
74
            "Error sending email via SendGrid: '%s': %s"
75
                .formatted(response.getStatusCode(), response.getBody()),
1✔
76
            new WebApplicationException(response.getStatusCode()));
1✔
77
      }
78
      return response;
1✔
79
    } catch (IOException ex) {
1✔
80
      logException("Exception sending email via SendGrid: %s".formatted(ex.getMessage()), ex);
1✔
81
      // Create a response that we can use to capture this failure.
82
      return new Response(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, ex.getMessage(), Map.of());
1✔
83
    }
84
  }
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc