Skip to content

Commit

Permalink
refactor(TripMonitorNotification): Add and use ctor when creating not…
Browse files Browse the repository at this point in the history
…ification instances.
  • Loading branch information
binh-dam-ibigroup committed Nov 28, 2023
1 parent bf224d1 commit 2cfff4a
Showing 1 changed file with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
*/
public class TripMonitorNotification extends Model {
private static final Logger LOG = LoggerFactory.getLogger(TripMonitorNotification.class);
public NotificationType type;
public String body;
public final NotificationType type;
public final String body;

public TripMonitorNotification(NotificationType type, String body) {
this.type = type;
this.body = body;
}

public static TripMonitorNotification createAlertNotification(
Set<LocalizedAlert> previousAlerts,
Expand All @@ -30,15 +35,15 @@ public static TripMonitorNotification createAlertNotification(
HashSet<LocalizedAlert> resolvedAlerts = new HashSet<>(previousAlerts);
resolvedAlerts.removeAll(newAlerts);
// If there is no change in alerts from previous check, no notification should be created.
if (unseenAlerts.size() == 0 && resolvedAlerts.size() == 0) {
if (unseenAlerts.isEmpty() && resolvedAlerts.isEmpty()) {
return null;
}
// Otherwise, construct a notification from the alert sets.
TripMonitorNotification notification = new TripMonitorNotification();
// FIXME: notification type should be determined from alert sets (ALERT_ALL_CLEAR, etc.)?
notification.type = NotificationType.ALERT_FOUND;
notification.body = bodyFromAlerts(previousAlerts, resolvedAlerts, unseenAlerts);
return notification;
return new TripMonitorNotification(
// FIXME: notification type should be determined from alert sets (ALERT_ALL_CLEAR, etc.)?
NotificationType.ALERT_FOUND,
bodyFromAlerts(previousAlerts, resolvedAlerts, unseenAlerts)
);
}

/**
Expand All @@ -53,8 +58,6 @@ public static TripMonitorNotification createDelayNotification(
Date targetDatetime,
NotificationType delayType
) {
TripMonitorNotification notification = new TripMonitorNotification();
notification.type = delayType;
if (delayType != NotificationType.ARRIVAL_DELAY && delayType != NotificationType.DEPARTURE_DELAY) {
LOG.error("Delay notification not permitted for type {}", delayType);
return null;
Expand All @@ -68,15 +71,17 @@ public static TripMonitorNotification createDelayNotification(
delayHumanTime = String.format("%d minute%s early", delayInMinutes, delayInMinutes < -1 ? "s" : "");
}

notification.body = String.format(
"Your trip is now predicted to %s %s (at %s).",
delayType == NotificationType.ARRIVAL_DELAY ? "arrive" : "depart",
delayHumanTime,
ZonedDateTime
.ofInstant(targetDatetime.toInstant(), DateTimeUtils.getOtpZoneId())
.format(DateTimeUtils.NOTIFICATION_TIME_FORMATTER)
return new TripMonitorNotification(
delayType,
String.format(
"Your trip is now predicted to %s %s (at %s).",
delayType == NotificationType.ARRIVAL_DELAY ? "arrive" : "depart",
delayHumanTime,
ZonedDateTime
.ofInstant(targetDatetime.toInstant(), DateTimeUtils.getOtpZoneId())
.format(DateTimeUtils.NOTIFICATION_TIME_FORMATTER)
)
);
return notification;
}

/**
Expand All @@ -85,12 +90,12 @@ public static TripMonitorNotification createDelayNotification(
public static TripMonitorNotification createItineraryNotFoundNotification(
boolean stillPossibleOnOtherMonitoredDaysOfTheWeek
) {
TripMonitorNotification notification = new TripMonitorNotification();
notification.type = NotificationType.ITINERARY_NOT_FOUND;
notification.body = stillPossibleOnOtherMonitoredDaysOfTheWeek
? "Your itinerary was not found in trip planner results for today! Please check realtime conditions and plan a new trip."
: "Your itinerary is no longer possible any monitored day of the week! Please plan and save a new trip.";
return notification;
return new TripMonitorNotification(
NotificationType.ITINERARY_NOT_FOUND,
stillPossibleOnOtherMonitoredDaysOfTheWeek
? "Your itinerary was not found in trip planner results for today! Please check realtime conditions and plan a new trip."
: "Your itinerary is no longer possible any monitored day of the week! Please plan and save a new trip."
);
}

/**
Expand All @@ -100,17 +105,17 @@ public static TripMonitorNotification createInitialReminderNotification(
MonitoredTrip trip,
boolean noOtherNotifications
) {
TripMonitorNotification notification = new TripMonitorNotification();
notification.type = NotificationType.INITIAL_REMINDER;
// TODO: i18n and add itinerary details.
StringBuilder body = new StringBuilder();
body.append(String.format("Reminder for your upcoming trip at %s.", trip.tripTime));
if (noOtherNotifications) {
body.append(" ");
body.append("We will let you know if anything changes.");
}
notification.body = body.toString();
return notification;
return new TripMonitorNotification(
NotificationType.INITIAL_REMINDER,
body.toString()
);
}

private static String bodyFromAlerts(
Expand All @@ -120,20 +125,20 @@ private static String bodyFromAlerts(
{
StringBuilder body = new StringBuilder();
// If all previous alerts were resolved and there are no unseen alerts, send ALL CLEAR.
if (previousAlerts.size() == resolvedAlerts.size() && unseenAlerts.size() == 0) {
if (previousAlerts.size() == resolvedAlerts.size() && unseenAlerts.isEmpty()) {
body.append("All clear! The following alerts on your itinerary were all resolved:");
body.append(listFromAlerts(resolvedAlerts, true));
// Preempt the final return statement to avoid duplicating
return body.toString();
}
// If there are any unseen alerts, include list of these.
if (unseenAlerts.size() > 0) {
if (!unseenAlerts.isEmpty()) {
// TODO: Improve message.
body.append("New alerts found! They are:");
body.append(listFromAlerts(unseenAlerts, false));
}
// If there are any resolved alerts, include list of these.
if (resolvedAlerts.size() > 0) {
if (!resolvedAlerts.isEmpty()) {
if (body.length() > 0) body.append("\n");
// TODO: Improve message.
body.append("Resolved alerts are:");
Expand Down

0 comments on commit 2cfff4a

Please sign in to comment.