From dd2f246d2b04f738bcaf3aff1b60510fa172d6c4 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:56:34 -0500 Subject: [PATCH] fix(ManageTripTracking): Use profile of primary traveler. --- .../middleware/models/MonitoredTrip.java | 8 +++++ .../triptracker/ManageTripTracking.java | 2 +- .../middleware/models/MonitoredTripTest.java | 32 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java b/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java index 4a49703a5..e14d7d996 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 803fc6529..15ef5ef20 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 a58adec69..2fde790db 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." + ) + ); + } }