mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
Update getArrivedStopIfExists method to take an argument returnNextStopOnly
This commit is contained in:
@@ -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<IStop | undefined> {
|
||||
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) {
|
||||
|
||||
@@ -104,10 +104,13 @@ export interface ShuttleGetterRepository extends EventEmitter {
|
||||
checkIfShuttleIsAtStop(shuttleId: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Get the stop that the shuttle is currently at, if it exists.
|
||||
*
|
||||
* @param shuttle
|
||||
* @param delta - The coordinate delta tolerance (default 0.001)
|
||||
* @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): Promise<IStop | undefined>;
|
||||
getArrivedStopIfExists(shuttle: IShuttle, returnNextStopOnly: boolean): Promise<IStop | undefined>;
|
||||
}
|
||||
|
||||
@@ -260,16 +260,31 @@ export class UnoptimizedInMemoryShuttleRepository
|
||||
|
||||
public async getArrivedStopIfExists(
|
||||
shuttle: IShuttle,
|
||||
returnNextStopOnly: boolean = false,
|
||||
): Promise<IStop | undefined> {
|
||||
const delta = this.shuttleStopArrivalDegreeDelta;
|
||||
const degreeDelta = this.shuttleStopArrivalDegreeDelta;
|
||||
|
||||
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, delta)) {
|
||||
if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, degreeDelta)) {
|
||||
return stop;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user