Skip to content

Commit

Permalink
refactor(Update to send email): Sends accept depentent email to relat…
Browse files Browse the repository at this point in the history
…ed user on create/update if app
  • Loading branch information
br648 committed Oct 24, 2024
1 parent bacb87e commit c3214fa
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ public static OtpUser acceptDependent(Request request, Response response) {
return relatedUser;
}

public static void manageAcceptDependentEmail(OtpUser dependentUser) {
manageAcceptDependentEmail(dependentUser, false);
}

/**
* When creating or updating an OTP user, extract a list of newly defined dependents and send an 'accept dependent'
* email to each. Then update which dependents have been sent an email so subsequent updates do not trigger
* additional emails.
*/
public static void manageAcceptDependentEmail(OtpUser dependentUser) {
public static void manageAcceptDependentEmail(OtpUser dependentUser, boolean isTest) {
if (dependentUser.relatedUsers.isEmpty()) {
// No related users defined by dependent.
return;
Expand All @@ -94,8 +98,8 @@ public static void manageAcceptDependentEmail(OtpUser dependentUser) {
.stream()
.filter(relatedUser -> !relatedUser.acceptDependentEmailSent)
.forEach(relatedUser -> {
OtpUser user = Persistence.otpUsers.getById(relatedUser.userId);
if (user != null && sendAcceptDependentEmail(dependentUser, user)) {
OtpUser userToReceiveEmail = Persistence.otpUsers.getById(relatedUser.userId);
if (userToReceiveEmail != null && (isTest || sendAcceptDependentEmail(dependentUser, userToReceiveEmail))) {
relatedUser.acceptDependentEmailSent = true;
}
});
Expand All @@ -114,16 +118,19 @@ private static boolean sendAcceptDependentEmail(OtpUser dependentUser, OtpUser r
String acceptDependentUrl = getAcceptDependentUrl(dependentUser);

// A HashMap is needed instead of a Map for template data to be serialized to the template renderer.
Map<String, Object> templateData = new HashMap<>(Map.of(
Map<String, Object> templateData = new HashMap<>(
Map.of(
"acceptDependentLinkAnchorLabel", acceptDependentLinkLabel,
"acceptDependentLinkLabelAndUrl", label(acceptDependentLinkLabel, acceptDependentUrl, locale),
"acceptDependentUrl", getAcceptDependentUrl(dependentUser),
"emailFooter", Message.ACCEPT_DEPENDENT_EMAIL_FOOTER.get(locale),
"emailGreeting", String.format("%s%s", dependentUser.email, Message.ACCEPT_DEPENDENT_EMAIL_GREETING.get(locale)),
// TODO: This is required in the `OtpUserContainer.ftl` template. Not sure what to link to so providing link back to settings.
"manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH),
// TODO: The user's email address isn't very personal, but that is all I have to work with! Suggetions?
"emailGreeting", String.format("%s%s", dependentUser.email, Message.ACCEPT_DEPENDENT_EMAIL_GREETING.get(locale)),
// TODO: This is required in the `OtpUserContainer.ftl` template. Not sure what to provide. Suggestions?
"manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH),
"manageLinkText", Message.ACCEPT_DEPENDENT_EMAIL_MANAGE.get(locale)
));
)
);

return NotificationUtils.sendEmail(
relatedUser,
Expand All @@ -135,7 +142,7 @@ private static boolean sendAcceptDependentEmail(OtpUser dependentUser, OtpUser r
}

private static String getAcceptDependentUrl(OtpUser dependentUser) {
// TODO: Is OTP_UI_URL the correct base URL to user here? If not, what?!
// TODO: Is OTP_UI_URL the correct base URL to user here? I'm not sure.
return String.format("%s%s?userId=%s", OTP_UI_URL, ACCEPT_DEPENDENT_PATH, dependentUser.id);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/Message.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ACCEPT_DEPENDENT_EMAIL_FOOTER = You are receiving this email because you have been selected to be a trusted companion.
ACCEPT_DEPENDENT_EMAIL_GREETING = %s would like you to be their trusted companion.
ACCEPT_DEPENDENT_EMAIL_GREETING = %s would like you to be a trusted companion.
ACCEPT_DEPENDENT_EMAIL_LINK_TEXT = Accept trusted companion
ACCEPT_DEPENDENT_EMAIL_SUBJECT = Trusted companion request
ACCEPT_DEPENDENT_EMAIL_MANAGE = Manage settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static void tearDown() {
multiAdminUser = Persistence.adminUsers.getById(multiAdminUser.id);
if (multiAdminUser != null) multiAdminUser.delete();
deleteOtpUser(
IS_END_TO_END,
soloOtpUser,
multiOtpUser
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static void setUp() throws Exception {
@AfterAll
public static void tearDown() {
deleteOtpUser(
IS_END_TO_END,
otpUser,
relatedUserOne,
relatedUserTwo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ static Itinerary createItinerary() {
return itinerary;
}

public static void deleteOtpUser(OtpUser... optUsers) {
public static void deleteOtpUser(boolean isEndToEnd, OtpUser... optUsers) {
for (OtpUser otpUser : optUsers) {
if (otpUser != null) {
OtpUser user = Persistence.otpUsers.getById(otpUser.id);
if (user != null) {
user.delete(user.auth0UserId != null);
user.delete(isEndToEnd);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.opentripplanner.middleware.tripmonitor;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.opentripplanner.middleware.models.OtpUser;
import org.opentripplanner.middleware.models.RelatedUser;
import org.opentripplanner.middleware.persistence.Persistence;
import org.opentripplanner.middleware.testutils.ApiTestUtils;
import org.opentripplanner.middleware.testutils.OtpMiddlewareTestEnvironment;
import org.opentripplanner.middleware.testutils.PersistenceTestUtils;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.middleware.auth.Auth0Connection.setAuthDisabled;
import static org.opentripplanner.middleware.testutils.PersistenceTestUtils.deleteOtpUser;

public class TrustedCompanionTest extends OtpMiddlewareTestEnvironment {
private static OtpUser relatedUserOne;
private static OtpUser dependentUserOne;

@BeforeAll
public static void setUp() {
setAuthDisabled(false);
relatedUserOne = PersistenceTestUtils.createUser(ApiTestUtils.generateEmailAddress("related-user-one"));
dependentUserOne = PersistenceTestUtils.createUser(ApiTestUtils.generateEmailAddress("dependent-one"));
}

@AfterAll
public static void tearDown() {
deleteOtpUser(IS_END_TO_END,
relatedUserOne,
dependentUserOne
);
}

@Test
void canManageAcceptDependentEmail() {
dependentUserOne.relatedUsers.add(new RelatedUser(relatedUserOne.id, relatedUserOne.email, RelatedUser.RelatedUserStatus.PENDING));
Persistence.otpUsers.replace(dependentUserOne.id, dependentUserOne);
TrustedCompanion.manageAcceptDependentEmail(dependentUserOne, true);
assertTrue(dependentUserOne.relatedUsers.get(0).acceptDependentEmailSent);
}
}

0 comments on commit c3214fa

Please sign in to comment.