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; }