From 712e31100632973bd0c5d7647ae5e29c03f15305 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Fri, 21 Nov 2025 10:51:38 -0800 Subject: [PATCH] Update getArrivedStopIfExists method to take an argument returnNextStopOnly --- .../shuttle/RedisShuttleRepository.ts | 14 ++-------- .../shuttle/ShuttleGetterRepository.ts | 15 ++++++----- .../UnoptimizedInMemoryShuttleRepository.ts | 27 ++++++++++++++----- .../ShuttleRepositorySharedTests.test.ts | 26 ++++++++++++++++-- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index ab41196..6c0ccdc 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -443,24 +443,14 @@ 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 degreeDelta - * @returns - */ public async getArrivedStopIfExists( shuttle: IShuttle, + returnNextStopOnly: boolean = false, ): Promise { const degreeDelta = this.shuttleStopArrivalDegreeDelta; const lastStop = await this.getShuttleLastStopArrival(shuttle.id); - if (lastStop) { + if (lastStop && returnNextStopOnly) { const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId); const orderedStopAfter = lastOrderedStop?.nextStop; if (orderedStopAfter) { diff --git a/src/repositories/shuttle/ShuttleGetterRepository.ts b/src/repositories/shuttle/ShuttleGetterRepository.ts index 2678c2c..5b7aea8 100644 --- a/src/repositories/shuttle/ShuttleGetterRepository.ts +++ b/src/repositories/shuttle/ShuttleGetterRepository.ts @@ -104,10 +104,13 @@ export interface ShuttleGetterRepository extends EventEmitter { checkIfShuttleIsAtStop(shuttleId: string): Promise; /** - * Check if a shuttle has arrived at a stop within the given delta. - * Returns the stop if the shuttle is at a stop, otherwise undefined. - * @param shuttle - * @param delta - The coordinate delta tolerance (default 0.001) - */ - getArrivedStopIfExists(shuttle: IShuttle): Promise; + * Get the stop that the shuttle is currently at, if it exists. + * + * @param shuttle + * @param returnNextStopOnly If set to true, and the shuttle has a "last stop", + * only return the stop directly after the last stop. + * Otherwise, it may return any stop that is on the shuttle's route. + * @returns + */ + getArrivedStopIfExists(shuttle: IShuttle, returnNextStopOnly: boolean): Promise; } diff --git a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts index ec30b84..f6fbafa 100644 --- a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts +++ b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts @@ -260,16 +260,31 @@ export class UnoptimizedInMemoryShuttleRepository public async getArrivedStopIfExists( shuttle: IShuttle, + returnNextStopOnly: boolean = false, ): Promise { - const delta = this.shuttleStopArrivalDegreeDelta; - const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId); + const degreeDelta = this.shuttleStopArrivalDegreeDelta; - for (const orderedStop of orderedStops) { - const stop = await this.getStopById(orderedStop.stopId); - if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) { - return stop; + const lastStop = await this.getShuttleLastStopArrival(shuttle.id); + if (lastStop && returnNextStopOnly) { + 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, degreeDelta)) { + 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, degreeDelta)) { + return stop; + } } } + return undefined; } diff --git a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts index e8e4aa2..55c6bb4 100644 --- a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts +++ b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts @@ -577,7 +577,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { test("gets the stop that the shuttle is currently at, if exists", async () => { const { sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); - const result = await repository.getArrivedStopIfExists(shuttle); + const result = await repository.getArrivedStopIfExists(shuttle, false); expect(result).toBeDefined(); expect(result?.id).toBe("st2"); @@ -588,10 +588,32 @@ describe.each(repositoryImplementations)('$name', (holder) => { const { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops(); const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; // Not at any stop - const result = await repository.getArrivedStopIfExists(shuttle); + const result = await repository.getArrivedStopIfExists(shuttle, false); expect(result).toBeUndefined(); }); + + test("only gets the shuttle's next stop if parameter passed and shuttle has arrived at stop", async () => { + const { sampleShuttleNotInRepository: shuttle, stop1, stop2 } = await setupRouteAndOrderedStops(); + + shuttle.coordinates = stop1.coordinates; + await repository.addOrUpdateShuttle(shuttle); + + let result = await repository.getArrivedStopIfExists(shuttle, true); + expect(result).toBeUndefined(); + + shuttle.coordinates = stop2.coordinates; + result = await repository.getArrivedStopIfExists(shuttle, true); + expect(result).not.toBeUndefined(); + }); + + test("still gets any stop for the shuttle if the shuttle has no last stop", async () => { + const { sampleShuttleNotInRepository: shuttle, stop1 } = await setupRouteAndOrderedStops(); + + shuttle.coordinates = stop1.coordinates; + const result = await repository.getArrivedStopIfExists(shuttle, true); + expect(result).not.toBeUndefined(); + }); }); describe("getShuttleLastStopArrival", () => {