diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index 1b4d952..d4669bc 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -373,11 +373,11 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt let arrivedStop: IStop | undefined; if (isAtStop) { - // Allow retrieval of the same stop + // Allow retrieval of the shuttle's current stop // Will still return undefined when the shuttle leaves the stop - arrivedStop = await this.getArrivedStopIfExists(shuttle, false); - } else { arrivedStop = await this.getArrivedStopIfExists(shuttle, true); + } else { + arrivedStop = await this.getArrivedStopIfExists(shuttle, false); } // Will not fire *any* events if the same stop @@ -454,13 +454,21 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt public async getArrivedStopIfExists( shuttle: IShuttle, - returnNextStopOnly: boolean = false, + canReturnShuttleCurrentStop: boolean = false, ): Promise { const degreeDelta = this.shuttleStopArrivalDegreeDelta; - const lastStop = await this.getShuttleLastStopArrival(shuttle.id); - if (lastStop && returnNextStopOnly) { - const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId); + const lastStopArrival = await this.getShuttleLastStopArrival(shuttle.id); + if (lastStopArrival) { + // Return the shuttle's current stop depending on the flag + if (canReturnShuttleCurrentStop) { + const lastStop = await this.getStopById(lastStopArrival.stopId); + if (lastStop && shuttleHasArrivedAtStop(shuttle, lastStop, degreeDelta)) { + return lastStop; + } + } + + const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStopArrival.stopId); const orderedStopAfter = lastOrderedStop?.nextStop; if (orderedStopAfter) { const stopAfter = await this.getStopById(orderedStopAfter.stopId); diff --git a/src/repositories/shuttle/ShuttleGetterRepository.ts b/src/repositories/shuttle/ShuttleGetterRepository.ts index 5b7aea8..44815ec 100644 --- a/src/repositories/shuttle/ShuttleGetterRepository.ts +++ b/src/repositories/shuttle/ShuttleGetterRepository.ts @@ -107,10 +107,11 @@ export interface ShuttleGetterRepository extends EventEmitter { * 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. + * @param canReturnShuttleCurrentStop If set to true, and the shuttle's "last stop" + * matches the arrived stop, continue to return the arrived stop. + * Otherwise, only return the shuttle's next stop. + * This flag has no effect if the shuttle has not had a "last stop". * @returns */ - getArrivedStopIfExists(shuttle: IShuttle, returnNextStopOnly: boolean): Promise; + getArrivedStopIfExists(shuttle: IShuttle, canReturnShuttleCurrentStop: boolean): Promise; } diff --git a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts index 5239099..38c7c02 100644 --- a/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts +++ b/src/repositories/shuttle/UnoptimizedInMemoryShuttleRepository.ts @@ -188,11 +188,11 @@ export class UnoptimizedInMemoryShuttleRepository let arrivedStop: IStop | undefined; if (isAtStop) { - // Allow retrieval of the same stop + // Allow retrieval of the shuttle's current stop // Will still return undefined when the shuttle leaves the stop - arrivedStop = await this.getArrivedStopIfExists(shuttle, false); - } else { arrivedStop = await this.getArrivedStopIfExists(shuttle, true); + } else { + arrivedStop = await this.getArrivedStopIfExists(shuttle, false); } @@ -270,13 +270,21 @@ export class UnoptimizedInMemoryShuttleRepository public async getArrivedStopIfExists( shuttle: IShuttle, - returnNextStopOnly: boolean = false, + canReturnShuttleCurrentStop: boolean = false, ): Promise { const degreeDelta = this.shuttleStopArrivalDegreeDelta; - const lastStop = await this.getShuttleLastStopArrival(shuttle.id); - if (lastStop && returnNextStopOnly) { - const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId); + const lastStopArrival = await this.getShuttleLastStopArrival(shuttle.id); + if (lastStopArrival) { + // Return the shuttle's current stop depending on the flag + if (canReturnShuttleCurrentStop) { + const lastStop = await this.getStopById(lastStopArrival.stopId); + if (lastStop && shuttleHasArrivedAtStop(shuttle, lastStop, degreeDelta)) { + return lastStop; + } + } + + const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStopArrival.stopId); const orderedStopAfter = lastOrderedStop?.nextStop; if (orderedStopAfter) { const stopAfter = await this.getStopById(orderedStopAfter.stopId); diff --git a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts index 55c6bb4..08deaa2 100644 --- a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts +++ b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts @@ -574,7 +574,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { }); describe("getArrivedStopIfExists", () => { - test("gets the stop that the shuttle is currently at, if exists", async () => { + test("gets any stop that the shuttle is currently at, if the shuttle has not had a last stop", async () => { const { sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); const result = await repository.getArrivedStopIfExists(shuttle, false); @@ -593,27 +593,30 @@ describe.each(repositoryImplementations)('$name', (holder) => { expect(result).toBeUndefined(); }); - test("only gets the shuttle's next stop if parameter passed and shuttle has arrived at stop", async () => { + test("only gets the shuttle's next stop if shuttle has previously arrived at a stop", async () => { const { sampleShuttleNotInRepository: shuttle, stop1, stop2 } = await setupRouteAndOrderedStops(); shuttle.coordinates = stop1.coordinates; await repository.addOrUpdateShuttle(shuttle); - let result = await repository.getArrivedStopIfExists(shuttle, true); + let result = await repository.getArrivedStopIfExists(shuttle, false); expect(result).toBeUndefined(); shuttle.coordinates = stop2.coordinates; - result = await repository.getArrivedStopIfExists(shuttle, true); + result = await repository.getArrivedStopIfExists(shuttle, false); expect(result).not.toBeUndefined(); }); - test("still gets any stop for the shuttle if the shuttle has no last stop", async () => { + test("returns the shuttle's currently arrived stop if flag passed", async () => { const { sampleShuttleNotInRepository: shuttle, stop1 } = await setupRouteAndOrderedStops(); shuttle.coordinates = stop1.coordinates; + await repository.addOrUpdateShuttle(shuttle); + const result = await repository.getArrivedStopIfExists(shuttle, true); - expect(result).not.toBeUndefined(); + expect(result?.id === stop1.id) }); + }); describe("getShuttleLastStopArrival", () => {