Merge pull request #87 from brendan-ch/hotfix/clear-shuttle-last-stop-if-no-data

hotfix/clear-shuttle-last-stop-if-no-data
This commit is contained in:
2025-11-14 10:31:33 -08:00
committed by GitHub
3 changed files with 66 additions and 1 deletions

View File

@@ -534,6 +534,9 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
const key = this.createShuttleKey(shuttleId);
await this.redisClient.del(key);
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
await this.removeShuttleLastStopIfExists(shuttleId);
return shuttle;
}
return null;
@@ -559,6 +562,15 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
return null;
}
private async removeShuttleLastStopIfExists(shuttleId: string) {
const lastStop = await this.getShuttleLastStopArrival(shuttleId);
if (lastStop) {
const key = this.createShuttleLastStopKey(shuttleId);
await this.redisClient.del(key);
return lastStop;
}
return null;
}
// Clear methods
public async clearShuttleData(): Promise<void> {
@@ -566,6 +578,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
if (keys.length > 0) {
await this.redisClient.del(keys);
}
await this.clearShuttleLastStopData();
}
public async clearOrderedStopData(): Promise<void> {
@@ -588,4 +601,11 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
await this.redisClient.del(keys);
}
}
private async clearShuttleLastStopData(): Promise<void> {
const keys = await this.redisClient.keys('shuttle:laststop:*');
if (keys.length > 0) {
await this.redisClient.del(keys);
}
}
}

View File

@@ -267,6 +267,7 @@ export class UnoptimizedInMemoryShuttleRepository
const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
if (shuttle != null) {
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
await this.removeShuttleLastStopIfExists(shuttleId);
}
return shuttle;
}
@@ -282,8 +283,13 @@ export class UnoptimizedInMemoryShuttleRepository
}, this.orderedStops);
}
private async removeShuttleLastStopIfExists(shuttleId: string) {
this.shuttleLastStopArrivals.delete(shuttleId);
}
public async clearShuttleData(): Promise<void> {
this.shuttles = [];
await this.clearShuttleLastStopData();
}
public async clearOrderedStopData(): Promise<void> {
@@ -298,4 +304,7 @@ export class UnoptimizedInMemoryShuttleRepository
this.stops = [];
}
private async clearShuttleLastStopData(): Promise<void> {
this.shuttleLastStopArrivals.clear();
}
}

View File

@@ -18,7 +18,7 @@ class UnoptimizedInMemoryShuttleRepositoryHolder implements RepositoryHolder<Shu
factory = async () => {
return new UnoptimizedInMemoryShuttleRepository();
};
teardown = async () => {};
teardown = async () => { };
}
class RedisShuttleRepositoryHolder implements RepositoryHolder<ShuttleGetterSetterRepository> {
@@ -386,6 +386,20 @@ describe.each(repositoryImplementations)('$name', (holder) => {
expect(remainingShuttles).toHaveLength(mockShuttles.length - 1);
});
test("removes shuttle last stop if exists", async () => {
const { route, stop1, systemId } = await setupRouteAndOrderedStops();
const mockShuttle = generateMockShuttles()[0];
mockShuttle.routeId = route.id;
mockShuttle.coordinates = stop1.coordinates;
mockShuttle.systemId = systemId;
await repository.addOrUpdateShuttle(mockShuttle);
await repository.removeShuttleIfExists(mockShuttle.id);
const lastStopData = await repository.getShuttleLastStopArrival(mockShuttle.id);
expect(lastStopData).toBeUndefined();
});
test("does nothing if shuttle doesn't exist", async () => {
const systemId = "1";
const mockShuttles = generateMockShuttles();
@@ -477,6 +491,28 @@ describe.each(repositoryImplementations)('$name', (holder) => {
const result = await repository.getShuttles();
expect(result).toEqual([]);
});
test("also clears last stop data", async () => {
const { route, stop1, stop2, systemId } = await setupRouteAndOrderedStops();
const mockShuttles = generateMockShuttles();
mockShuttles[0].coordinates = stop1.coordinates;
mockShuttles[1].coordinates = stop2.coordinates;
for (const shuttle of mockShuttles) {
shuttle.routeId = route.id;
shuttle.systemId = systemId;
await repository.addOrUpdateShuttle(shuttle);
}
await repository.clearShuttleData();
for (const shuttle of mockShuttles) {
const result = await repository.getShuttleLastStopArrival(shuttle.id);
expect(result).toBeUndefined();
}
});
});