From b74a0d5d6454359c8e3998f5568e0744469480b8 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 13 Nov 2025 22:13:17 -0800 Subject: [PATCH] Restrict stop arrival event to only next stop after shuttle's last stop, if last stop exists --- .../shuttle/RedisShuttleRepository.ts | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index 1b7ffd0..cd90147 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -445,18 +445,42 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt } } + /** + * Get the stop that the shuttle is currently at, if it exists. + * + * If the shuttle has a "last stop", it will only return the stop + * directly after the last stop. Otherwise, it may return any stop that + * is on the shuttle's route. + * + * @param shuttle + * @param delta + * @returns + */ public async getArrivedStopIfExists( shuttle: IShuttle, delta = 0.001, ): Promise { - const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId); + const lastStop = await this.getShuttleLastStopArrival(shuttle.id); + if (lastStop) { + const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId); + const orderedStopAfter = lastOrderedStop?.nextStop; + if (orderedStopAfter) { + const stopAfter = await this.getStopById(orderedStopAfter.stopId); + if (stopAfter && shuttleHasArrivedAtStop(shuttle, stopAfter, delta)) { + return stopAfter; + } + } + } else { + const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId); - for (const orderedStop of orderedStops) { - const stop = await this.getStopById(orderedStop.stopId); - if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) { - return stop; + for (const orderedStop of orderedStops) { + const stop = await this.getStopById(orderedStop.stopId); + if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) { + return stop; + } } } + return undefined; }