Skip to content

Commit

Permalink
refactor(NotificationUtils): Extract trip email attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
binh-dam-ibigroup committed Nov 26, 2024
1 parent a6b9c40 commit 2a64b88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.opentripplanner.middleware.persistence.Persistence;
import org.opentripplanner.middleware.persistence.TypedPersistence;
import org.opentripplanner.middleware.tripmonitor.JourneyState;
import org.opentripplanner.middleware.utils.ConfigUtils;
import org.opentripplanner.middleware.utils.DateTimeUtils;
import org.opentripplanner.middleware.utils.ItineraryUtils;
import spark.Request;
Expand All @@ -32,8 +31,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.opentripplanner.middleware.tripmonitor.jobs.CheckMonitoredTrip.ACCOUNT_PATH;

/**
* A monitored trip represents a trip a user would like to receive notification on if affected by a delay and/or route
* change.
Expand All @@ -43,10 +40,6 @@ public class MonitoredTrip extends Model {

public static final String USER_ID_FIELD_NAME = "userId";

private final String TRIPS_PATH = ACCOUNT_PATH + "/trips";

private static final String OTP_UI_URL = ConfigUtils.getConfigPropertyAsText("OTP_UI_URL");

/**
* Mongo Id of the {@link OtpUser} who owns this monitored trip.
*/
Expand Down Expand Up @@ -437,12 +430,6 @@ public boolean isOneTime() {
return !monday && !tuesday && !wednesday && !thursday && !friday && !saturday && !sunday;
}

@JsonIgnore
@BsonIgnore
public String getTripUrl() {
return String.format("%s%s/%s", OTP_UI_URL, TRIPS_PATH, id);
}

/**
* Gets users not previously involved (as primary traveler, companion, or observer) in a trip.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,21 +527,15 @@ private void sendNotifications() {
String tripNameOrReminder = hasInitialReminder ? initialReminderNotification.body : trip.tripName;

Locale locale = getOtpUserLocale();
String tripLinkLabel = Message.TRIP_LINK_TEXT.get(locale);
String tripUrl = trip.getTripUrl();
// 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<>();
templateData.putAll(Map.of(
"emailGreeting", Message.TRIP_EMAIL_GREETING.get(locale),
"tripNameOrReminder", tripNameOrReminder,
"tripLinkLabelAndUrl", label(tripLinkLabel, tripUrl, locale),
"tripLinkAnchorLabel", tripLinkLabel,
"tripUrl", tripUrl,
"emailFooter", String.format(Message.TRIP_EMAIL_FOOTER.get(locale), OTP_UI_NAME),
"manageLinkText", Message.TRIP_EMAIL_MANAGE_NOTIFICATIONS.get(locale),
"manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH),
"notifications", new ArrayList<>(notifications),
"smsFooter", Message.SMS_STOP_NOTIFICATIONS.get(locale)
));
templateData.putAll(NotificationUtils.getTripNotificationFields(trip, locale));
if (hasInitialReminder) {
templateData.put("initialReminder", initialReminderNotification);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -41,6 +42,7 @@
import static org.opentripplanner.middleware.i18n.Message.TRIP_INVITE_PRIMARY_TRAVELER;
import static org.opentripplanner.middleware.i18n.Message.TRIP_LINK_TEXT;
import static org.opentripplanner.middleware.i18n.Message.TRIP_SURVEY_NOTIFICATION;
import static org.opentripplanner.middleware.tripmonitor.jobs.CheckMonitoredTrip.ACCOUNT_PATH;
import static org.opentripplanner.middleware.tripmonitor.jobs.CheckMonitoredTrip.SETTINGS_PATH;
import static org.opentripplanner.middleware.utils.ConfigUtils.getConfigPropertyAsText;
import static org.opentripplanner.middleware.utils.I18nUtils.getOtpUserLocale;
Expand Down Expand Up @@ -69,6 +71,7 @@ public class NotificationUtils {
private static final String TRIP_SURVEY_SUBDOMAIN = getConfigPropertyAsText("TRIP_SURVEY_SUBDOMAIN");
private static final String OTP_UI_NAME = ConfigUtils.getConfigPropertyAsText("OTP_UI_NAME");
private static final String OTP_UI_URL = ConfigUtils.getConfigPropertyAsText("OTP_UI_URL");
private static final String TRIPS_PATH = ACCOUNT_PATH + "/trips";

public enum UserType {
COMPANION,
Expand Down Expand Up @@ -483,8 +486,6 @@ public static String replaceUserNameInFromEmail(String fromEmail, OtpUser otpUse
public static void notifyCompanion(MonitoredTrip monitoredTrip, OtpUser tripCreator, OtpUser companionUser, UserType userType) {
if (companionUser != null) {
Locale locale = getOtpUserLocale(companionUser);
String tripLinkLabel = TRIP_LINK_TEXT.get(locale);
String tripUrl = monitoredTrip.getTripUrl();

String greeting;
switch (userType) {
Expand All @@ -500,28 +501,36 @@ public static void notifyCompanion(MonitoredTrip monitoredTrip, OtpUser tripCrea
break;
}

// A HashMap is needed instead of a Map for template data to be serialized to the template renderer.
Map<String, Object> templateData = new HashMap<>();
templateData.put("emailGreeting", String.format(greeting, tripCreator.email));
templateData.putAll(getTripNotificationFields(monitoredTrip, locale));

sendEmail(
replaceUserNameInFromEmail(FROM_EMAIL, tripCreator),
companionUser.email,
getTripEmailSubject(companionUser, locale, monitoredTrip),
"ShareTripText.ftl",
"ShareTripHtml.ftl",
Map.of(
"emailGreeting", String.format(
greeting,
tripCreator.email
),
"tripUrl", tripUrl,
"tripLinkAnchorLabel", tripLinkLabel,
"tripLinkLabelAndUrl", label(tripLinkLabel, tripUrl, locale),
"emailFooter", String.format(TRIP_EMAIL_FOOTER.get(locale), OTP_UI_NAME),
"manageLinkText", TRIP_EMAIL_MANAGE_NOTIFICATIONS.get(locale),
"manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH)
)
templateData
);
}
}

public static Map<String, Object> getTripNotificationFields(MonitoredTrip monitoredTrip, Locale locale) {
String tripLinkLabel = TRIP_LINK_TEXT.get(locale);
String tripUrl = String.format("%s%s/%s", OTP_UI_URL, TRIPS_PATH, monitoredTrip.id);

return Map.of(
"tripUrl", tripUrl,
"tripLinkAnchorLabel", tripLinkLabel,
"tripLinkLabelAndUrl", label(tripLinkLabel, tripUrl, locale),
"emailFooter", String.format(TRIP_EMAIL_FOOTER.get(locale), OTP_UI_NAME),
"manageLinkText", TRIP_EMAIL_MANAGE_NOTIFICATIONS.get(locale),
"manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH)
);
}

static class NotificationInfo {
/** ID for tracking notifications and survey responses. */
public final String notificationId;
Expand Down

0 comments on commit 2a64b88

Please sign in to comment.