-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Update to send email to accpet trusted companion):
- Loading branch information
Showing
11 changed files
with
204 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/main/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package org.opentripplanner.middleware.tripmonitor; | ||
|
||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.opentripplanner.middleware.auth.Auth0Connection; | ||
import org.opentripplanner.middleware.auth.RequestingUser; | ||
import org.opentripplanner.middleware.i18n.Message; | ||
import org.opentripplanner.middleware.models.OtpUser; | ||
import org.opentripplanner.middleware.models.RelatedUser; | ||
import org.opentripplanner.middleware.persistence.Persistence; | ||
import org.opentripplanner.middleware.utils.ConfigUtils; | ||
import org.opentripplanner.middleware.utils.HttpUtils; | ||
import org.opentripplanner.middleware.utils.I18nUtils; | ||
import org.opentripplanner.middleware.utils.NotificationUtils; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import static org.opentripplanner.middleware.controllers.api.ApiController.USER_ID_PARAM; | ||
import static org.opentripplanner.middleware.tripmonitor.jobs.CheckMonitoredTrip.SETTINGS_PATH; | ||
import static org.opentripplanner.middleware.utils.I18nUtils.label; | ||
import static org.opentripplanner.middleware.utils.JsonUtils.logMessageAndHalt; | ||
|
||
public class TrustedCompanion { | ||
|
||
private TrustedCompanion() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
private static final String OTP_UI_URL = ConfigUtils.getConfigPropertyAsText("OTP_UI_URL"); | ||
|
||
public static final String ACCEPT_DEPENDENT_PATH = "api/secure/user/acceptdependent"; | ||
|
||
/** | ||
* Accept a request from another user to be their dependent. This will include both companions and observers. | ||
*/ | ||
public static OtpUser acceptDependent(Request request, Response response) { | ||
RequestingUser requestingUser = Auth0Connection.getUserFromRequest(request); | ||
OtpUser relatedUser = requestingUser.otpUser; | ||
if (relatedUser == null) { | ||
logMessageAndHalt(request, HttpStatus.BAD_REQUEST_400, "Otp user unknown."); | ||
return null; | ||
} | ||
|
||
String dependentUserId = HttpUtils.getQueryParamFromRequest(request, USER_ID_PARAM, false); | ||
if (dependentUserId.isEmpty()) { | ||
logMessageAndHalt(request, HttpStatus.BAD_REQUEST_400, "Dependent user id not provided."); | ||
return null; | ||
} | ||
|
||
OtpUser dependentUser = Persistence.otpUsers.getById(dependentUserId); | ||
if (dependentUser == null) { | ||
logMessageAndHalt(request, HttpStatus.BAD_REQUEST_400, "Dependent user unknown!"); | ||
return null; | ||
} | ||
|
||
boolean isRelated = dependentUser.relatedUsers | ||
.stream() | ||
.filter(related -> related.userId.equals(relatedUser.id)) | ||
// Update related user status. This assumes a related user with "pending" status was previously added. | ||
.peek(related -> related.status = RelatedUser.RelatedUserStatus.CONFIRMED) | ||
.findFirst() | ||
.isPresent(); | ||
|
||
if (isRelated) { | ||
// Maintain a list of dependents. | ||
relatedUser.dependents.add(dependentUserId); | ||
Persistence.otpUsers.replace(relatedUser.id, relatedUser); | ||
// Update list of related users. | ||
Persistence.otpUsers.replace(dependentUser.id, dependentUser); | ||
} else { | ||
logMessageAndHalt(request, HttpStatus.BAD_REQUEST_400, "Dependent did not request user to be related!"); | ||
return null; | ||
} | ||
|
||
// TODO: Not sure what is required in the response. For now, returning the updated related user. | ||
return relatedUser; | ||
} | ||
|
||
/** | ||
* 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) { | ||
if (dependentUser.relatedUsers.isEmpty()) { | ||
// No related users defined by dependent. | ||
return; | ||
} | ||
|
||
dependentUser.relatedUsers | ||
.stream() | ||
.filter(relatedUser -> !relatedUser.acceptDependentEmailSent) | ||
.forEach(relatedUser -> { | ||
OtpUser user = Persistence.otpUsers.getById(relatedUser.userId); | ||
if (user != null && sendAcceptDependentEmail(dependentUser, user)) { | ||
relatedUser.acceptDependentEmailSent = true; | ||
} | ||
}); | ||
|
||
// Preserve email sent status. | ||
Persistence.otpUsers.replace(dependentUser.id, dependentUser); | ||
} | ||
|
||
/** | ||
* Send 'accept dependent' email. | ||
*/ | ||
private static boolean sendAcceptDependentEmail(OtpUser dependentUser, OtpUser relatedUser) { | ||
Locale locale = I18nUtils.getOtpUserLocale(relatedUser); | ||
|
||
String acceptDependentLinkLabel = Message.ACCEPT_DEPENDENT_EMAIL_LINK_TEXT.get(locale); | ||
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( | ||
"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), | ||
"manageLinkText", Message.ACCEPT_DEPENDENT_EMAIL_MANAGE.get(locale) | ||
)); | ||
|
||
return NotificationUtils.sendEmail( | ||
relatedUser, | ||
Message.ACCEPT_DEPENDENT_EMAIL_SUBJECT.get(locale), | ||
"AcceptDependentText.ftl", | ||
"AcceptDependentHtml.ftl", | ||
templateData | ||
); | ||
} | ||
|
||
private static String getAcceptDependentUrl(OtpUser dependentUser) { | ||
// TODO: Is OTP_UI_URL the correct base URL to user here? If not, what?! | ||
return String.format("%s%s?userId=%s", OTP_UI_URL, ACCEPT_DEPENDENT_PATH, dependentUser.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<#ftl auto_esc=false> | ||
<#include "OtpUserContainer.ftl"> | ||
|
||
<#-- | ||
This is a template for an HTML email that gets sent when a dependent user is requesting a trusted companion. | ||
--> | ||
|
||
<#macro EmailMain> | ||
<div> | ||
<h1>${emailGreeting}</h1> | ||
<p><a href="${acceptDependentUrl}">${acceptDependentLinkAnchorLabel}</a></p> | ||
</div> | ||
</#macro> | ||
|
||
<@HtmlEmail/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<#-- | ||
This is a template for a text email that gets sent when a dependent requests a trusted companion. | ||
Note: in plain text emails, all whitespace is preserved, | ||
so the indentation of the notification content is intentionally not aligned | ||
with the indentation of the macros. | ||
--> | ||
${emailGreeting} | ||
|
||
${tripLinkLabelAndUrl} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters