diff --git a/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java b/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java index 4a49703a..e14d7d99 100644 --- a/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java +++ b/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java @@ -489,6 +489,14 @@ public static TripUsers getAddedUsers(MonitoredTrip monitoredTrip, MonitoredTrip return new TripUsers(addedPrimaryTraveler, addedCompanion, addedObservers); } + /** + * @return The id of the primary traveler (the primary user of a trip, + * or the user who created the trip if no primary user has been set). + */ + public String getPrimaryTravelerId() { + return primary != null ? primary.userId : userId; + } + public static class TripUsers { public final RelatedUser companion; public final List observers; diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java b/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java index 803fc652..15ef5ef2 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java @@ -89,7 +89,7 @@ private static TrackingResponse doUpdateTracking(Request request, TripTrackingDa TripActions.getDefault().handleSegmentAction( ((SelfLegInstruction)instruction).getLegStep(), travelerPosition.expectedLeg.steps, - Persistence.otpUsers.getById(tripData.trip.userId) + Persistence.otpUsers.getById(tripData.trip.getPrimaryTravelerId()) ); } diff --git a/src/test/java/org/opentripplanner/middleware/models/MonitoredTripTest.java b/src/test/java/org/opentripplanner/middleware/models/MonitoredTripTest.java index a58adec6..2fde790d 100644 --- a/src/test/java/org/opentripplanner/middleware/models/MonitoredTripTest.java +++ b/src/test/java/org/opentripplanner/middleware/models/MonitoredTripTest.java @@ -150,4 +150,36 @@ private static Stream createGetAddedUsersCases() { ) ); } + + @ParameterizedTest + @MethodSource("createGetPrimaryTravelerCases") + void canGetPrimaryTraveler(MobilityProfileLite primary, String creatorId, String expectedId, String message) { + MonitoredTrip trip = new MonitoredTrip(); + trip.userId = creatorId; + trip.primary = primary; + + assertEquals(expectedId, trip.getPrimaryTravelerId(), message); + } + + private static Stream createGetPrimaryTravelerCases() { + final String creatorId = "creator-user-id"; + + MobilityProfileLite primary = new MobilityProfileLite(); + primary.userId = "primary-user-id"; + + return Stream.of( + Arguments.of( + primary, + creatorId, + primary.userId, + "Should return the id of primary if it was set." + ), + Arguments.of( + null, + creatorId, + creatorId, + "Should return the id of the user that created the trip if primary is null." + ) + ); + } }