Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import org.broadinstitute.consent.http.db.mapper.MailMessageMapper;
import org.broadinstitute.consent.http.models.mail.MailMessage;
import org.broadinstitute.consent.http.models.mail.MailMessageInsert;
import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindMethods;
Expand All @@ -19,11 +20,11 @@ WITH insterted_row AS (
INSERT INTO email_entity
(entity_reference_id, vote_id, user_id, email_type, date_sent, email_text, sendgrid_response, sendgrid_status, create_date)
VALUES
(:entityReferenceId, :voteId, :userId, :emailType, :dateSent, :emailText, :sendgridResponse, :sendgridStatus, :createDate)
(:entityReferenceId, :voteId, :userId, :emailType, :dateSent, :emailText, :sendgridResponse, :sendgridStatus, NOW())
RETURNING *)
SELECT * FROM insterted_row
""")
MailMessage insert(@BindMethods MailMessage mail);
MailMessage insert(@BindMethods MailMessageInsert mail);

@SqlQuery(
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.broadinstitute.consent.http.models.mail;

import java.util.Date;

public record MailMessageInsert(
String entityReferenceId,
Integer voteId,
Integer userId,
Integer emailType,
Date dateSent,
String emailText,
String sendgridResponse,
Integer sendgridStatus) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import org.broadinstitute.consent.http.configurations.ConsentConfiguration;
import org.broadinstitute.consent.http.db.DAOContainer;
import org.broadinstitute.consent.http.db.DatasetDAO;
Expand All @@ -38,6 +36,7 @@
import org.broadinstitute.consent.http.models.StudyDatasetCountRecord;
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.models.UserVoteReminder;
import org.broadinstitute.consent.http.models.mail.MailMessageInsert;
import org.broadinstitute.consent.http.util.ConsentLogger;
import org.jdbi.v3.core.result.ResultIterable;

Expand Down Expand Up @@ -73,37 +72,6 @@ public EmailService(
this.fromAccount = config.getMailConfiguration().getGoogleAccount();
}

/**
* This method saves an email (either sent or unsent) with all available metadata from the
* SendGrid response.
*/
private void saveEmailAndResponse(
@Nullable Response response,
@Nullable String entityReferenceId,
@Nullable Integer voteId,
Integer userId,
EmailType emailType,
String content) {
Instant now = Instant.now();
Date dateSent =
(Objects.nonNull(response) && response.getStatusCode() < 400) ? Date.from(now) : null;
String sendgridResponse = Objects.nonNull(response) ? response.getBody() : null;
Integer sendgridStatus = Objects.nonNull(response) ? response.getStatusCode() : null;
org.broadinstitute.consent.http.models.mail.MailMessage mailMessage =
new org.broadinstitute.consent.http.models.mail.MailMessage(
entityReferenceId,
null,
voteId,
userId,
emailType.getTypeInt(),
dateSent,
content,
sendgridResponse,
sendgridStatus,
Date.from(now));
emailDAO.insert(mailMessage);
}

public void sendMessage(MailMessage mailMessage, Integer userId)
throws IOException, TemplateException {
Writer out = new StringWriter();
Expand All @@ -123,13 +91,21 @@ public void sendMessage(MailMessage mailMessage, Integer userId)
}
// Checks that the user has not disabled email before sending
Response response = sendGridAPI.sendMessage(message, mailMessage.toUser.getEmail());
saveEmailAndResponse(
response,
mailMessage.getEntityReferenceId(),
mailMessage.getVoteId(),
userId,
mailMessage.emailType,
content);
Instant now = Instant.now();
Date dateSent = (response != null && response.getStatusCode() < 400) ? Date.from(now) : null;
String sendgridResponse = response != null ? response.getBody() : null;
Integer sendgridStatus = response != null ? response.getStatusCode() : null;
MailMessageInsert mailMessageInsert =
new MailMessageInsert(
mailMessage.getEntityReferenceId(),
mailMessage.getVoteId(),
userId,
mailMessage.emailType.getTypeInt(),
dateSent,
content,
sendgridResponse,
sendgridStatus);
emailDAO.insert(mailMessageInsert);
}

public List<org.broadinstitute.consent.http.models.mail.MailMessage> fetchEmailMessagesByType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.broadinstitute.consent.http.models.Election;
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.models.Vote;
import org.broadinstitute.consent.http.models.mail.MailMessage;
import org.broadinstitute.consent.http.models.mail.MailMessageInsert;
import org.jdbi.v3.core.JdbiException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -1267,17 +1267,15 @@ void testFindAgedDARsByEmailTypeOlderThanIntervalExpiringEntriesDoesNotRepeatIfE
dataAccessRequestDAO.insertDARDatasetRelation(
darTwo.getReferenceId(), datasetThree.getDatasetId());
mailMessageDAO.insert(
new MailMessage(
new MailMessageInsert(
darOne.getReferenceId(),
null,
null,
userOneId,
EmailType.DAR_EXPIRATION_REMINDER.getTypeInt(),
Date.from(Instant.now()),
"hello world!",
"success",
200,
Date.from(Instant.now())));
200));
List<DataAccessRequest> dars =
dataAccessRequestDAO.findAgedDARsByEmailTypeOlderThanInterval(
EmailType.DAR_EXPIRATION_REMINDER.getTypeInt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.broadinstitute.consent.http.models.User;
import org.broadinstitute.consent.http.models.UserVoteReminder;
import org.broadinstitute.consent.http.models.Vote;
import org.broadinstitute.consent.http.models.mail.MailMessage;
import org.broadinstitute.consent.http.models.mail.MailMessageInsert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -692,17 +692,15 @@ void testFindVotesThatNeedReminders_AlreadyEmailed() {
String referenceId = Instant.now().toString();
Integer emailType = EmailType.COLLECT.getTypeInt();
mailMessageDAO.insert(
new MailMessage(
new MailMessageInsert(
referenceId,
null,
vote.getVoteId(),
vote.getUserId(),
emailType,
Date.from(Instant.now()),
"Extra, Extra!",
null,
null,
Date.from(Instant.now())));
null));

List<UserVoteReminder> userVoteReminders =
electionDAO.findElectionReminders(-1, emailType, referenceId);
Expand Down
Loading
Loading