Update the method to accept a canReturnShuttleCurrentStop flag instead

This commit is contained in:
2025-11-21 11:07:47 -08:00
parent 8c341e91e0
commit 413443a162
4 changed files with 44 additions and 24 deletions

View File

@@ -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<IStop | undefined> {
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);

View File

@@ -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<IStop | undefined>;
getArrivedStopIfExists(shuttle: IShuttle, canReturnShuttleCurrentStop: boolean): Promise<IStop | undefined>;
}

View File

@@ -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<IStop | undefined> {
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);

View File

@@ -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", () => {