mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
Update the method to accept a canReturnShuttleCurrentStop flag instead
This commit is contained in:
@@ -373,11 +373,11 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
let arrivedStop: IStop | undefined;
|
let arrivedStop: IStop | undefined;
|
||||||
|
|
||||||
if (isAtStop) {
|
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
|
// Will still return undefined when the shuttle leaves the stop
|
||||||
arrivedStop = await this.getArrivedStopIfExists(shuttle, false);
|
|
||||||
} else {
|
|
||||||
arrivedStop = await this.getArrivedStopIfExists(shuttle, true);
|
arrivedStop = await this.getArrivedStopIfExists(shuttle, true);
|
||||||
|
} else {
|
||||||
|
arrivedStop = await this.getArrivedStopIfExists(shuttle, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Will not fire *any* events if the same stop
|
// Will not fire *any* events if the same stop
|
||||||
@@ -454,13 +454,21 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
|
|
||||||
public async getArrivedStopIfExists(
|
public async getArrivedStopIfExists(
|
||||||
shuttle: IShuttle,
|
shuttle: IShuttle,
|
||||||
returnNextStopOnly: boolean = false,
|
canReturnShuttleCurrentStop: boolean = false,
|
||||||
): Promise<IStop | undefined> {
|
): Promise<IStop | undefined> {
|
||||||
const degreeDelta = this.shuttleStopArrivalDegreeDelta;
|
const degreeDelta = this.shuttleStopArrivalDegreeDelta;
|
||||||
|
|
||||||
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
const lastStopArrival = await this.getShuttleLastStopArrival(shuttle.id);
|
||||||
if (lastStop && returnNextStopOnly) {
|
if (lastStopArrival) {
|
||||||
const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId);
|
// 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;
|
const orderedStopAfter = lastOrderedStop?.nextStop;
|
||||||
if (orderedStopAfter) {
|
if (orderedStopAfter) {
|
||||||
const stopAfter = await this.getStopById(orderedStopAfter.stopId);
|
const stopAfter = await this.getStopById(orderedStopAfter.stopId);
|
||||||
|
|||||||
@@ -107,10 +107,11 @@ export interface ShuttleGetterRepository extends EventEmitter {
|
|||||||
* Get the stop that the shuttle is currently at, if it exists.
|
* Get the stop that the shuttle is currently at, if it exists.
|
||||||
*
|
*
|
||||||
* @param shuttle
|
* @param shuttle
|
||||||
* @param returnNextStopOnly If set to true, and the shuttle has a "last stop",
|
* @param canReturnShuttleCurrentStop If set to true, and the shuttle's "last stop"
|
||||||
* only return the stop directly after the last stop.
|
* matches the arrived stop, continue to return the arrived stop.
|
||||||
* Otherwise, it may return any stop that is on the shuttle's route.
|
* Otherwise, only return the shuttle's next stop.
|
||||||
|
* This flag has no effect if the shuttle has not had a "last stop".
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
getArrivedStopIfExists(shuttle: IShuttle, returnNextStopOnly: boolean): Promise<IStop | undefined>;
|
getArrivedStopIfExists(shuttle: IShuttle, canReturnShuttleCurrentStop: boolean): Promise<IStop | undefined>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,11 +188,11 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
let arrivedStop: IStop | undefined;
|
let arrivedStop: IStop | undefined;
|
||||||
|
|
||||||
if (isAtStop) {
|
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
|
// Will still return undefined when the shuttle leaves the stop
|
||||||
arrivedStop = await this.getArrivedStopIfExists(shuttle, false);
|
|
||||||
} else {
|
|
||||||
arrivedStop = await this.getArrivedStopIfExists(shuttle, true);
|
arrivedStop = await this.getArrivedStopIfExists(shuttle, true);
|
||||||
|
} else {
|
||||||
|
arrivedStop = await this.getArrivedStopIfExists(shuttle, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -270,13 +270,21 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
|
|
||||||
public async getArrivedStopIfExists(
|
public async getArrivedStopIfExists(
|
||||||
shuttle: IShuttle,
|
shuttle: IShuttle,
|
||||||
returnNextStopOnly: boolean = false,
|
canReturnShuttleCurrentStop: boolean = false,
|
||||||
): Promise<IStop | undefined> {
|
): Promise<IStop | undefined> {
|
||||||
const degreeDelta = this.shuttleStopArrivalDegreeDelta;
|
const degreeDelta = this.shuttleStopArrivalDegreeDelta;
|
||||||
|
|
||||||
const lastStop = await this.getShuttleLastStopArrival(shuttle.id);
|
const lastStopArrival = await this.getShuttleLastStopArrival(shuttle.id);
|
||||||
if (lastStop && returnNextStopOnly) {
|
if (lastStopArrival) {
|
||||||
const lastOrderedStop = await this.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId);
|
// 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;
|
const orderedStopAfter = lastOrderedStop?.nextStop;
|
||||||
if (orderedStopAfter) {
|
if (orderedStopAfter) {
|
||||||
const stopAfter = await this.getStopById(orderedStopAfter.stopId);
|
const stopAfter = await this.getStopById(orderedStopAfter.stopId);
|
||||||
|
|||||||
@@ -574,7 +574,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("getArrivedStopIfExists", () => {
|
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 { sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
|
||||||
|
|
||||||
const result = await repository.getArrivedStopIfExists(shuttle, false);
|
const result = await repository.getArrivedStopIfExists(shuttle, false);
|
||||||
@@ -593,27 +593,30 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
|||||||
expect(result).toBeUndefined();
|
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();
|
const { sampleShuttleNotInRepository: shuttle, stop1, stop2 } = await setupRouteAndOrderedStops();
|
||||||
|
|
||||||
shuttle.coordinates = stop1.coordinates;
|
shuttle.coordinates = stop1.coordinates;
|
||||||
await repository.addOrUpdateShuttle(shuttle);
|
await repository.addOrUpdateShuttle(shuttle);
|
||||||
|
|
||||||
let result = await repository.getArrivedStopIfExists(shuttle, true);
|
let result = await repository.getArrivedStopIfExists(shuttle, false);
|
||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
|
|
||||||
shuttle.coordinates = stop2.coordinates;
|
shuttle.coordinates = stop2.coordinates;
|
||||||
result = await repository.getArrivedStopIfExists(shuttle, true);
|
result = await repository.getArrivedStopIfExists(shuttle, false);
|
||||||
expect(result).not.toBeUndefined();
|
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();
|
const { sampleShuttleNotInRepository: shuttle, stop1 } = await setupRouteAndOrderedStops();
|
||||||
|
|
||||||
shuttle.coordinates = stop1.coordinates;
|
shuttle.coordinates = stop1.coordinates;
|
||||||
|
await repository.addOrUpdateShuttle(shuttle);
|
||||||
|
|
||||||
const result = await repository.getArrivedStopIfExists(shuttle, true);
|
const result = await repository.getArrivedStopIfExists(shuttle, true);
|
||||||
expect(result).not.toBeUndefined();
|
expect(result?.id === stop1.id)
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getShuttleLastStopArrival", () => {
|
describe("getShuttleLastStopArrival", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user