mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
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:
@@ -534,6 +534,9 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
const key = this.createShuttleKey(shuttleId);
|
const key = this.createShuttleKey(shuttleId);
|
||||||
await this.redisClient.del(key);
|
await this.redisClient.del(key);
|
||||||
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
|
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
|
||||||
|
|
||||||
|
await this.removeShuttleLastStopIfExists(shuttleId);
|
||||||
|
|
||||||
return shuttle;
|
return shuttle;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -559,6 +562,15 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
return null;
|
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
|
// Clear methods
|
||||||
public async clearShuttleData(): Promise<void> {
|
public async clearShuttleData(): Promise<void> {
|
||||||
@@ -566,6 +578,7 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
if (keys.length > 0) {
|
if (keys.length > 0) {
|
||||||
await this.redisClient.del(keys);
|
await this.redisClient.del(keys);
|
||||||
}
|
}
|
||||||
|
await this.clearShuttleLastStopData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async clearOrderedStopData(): Promise<void> {
|
public async clearOrderedStopData(): Promise<void> {
|
||||||
@@ -588,4 +601,11 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
|||||||
await this.redisClient.del(keys);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,6 +267,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
|
const shuttle = await this.removeEntityByIdIfExists(shuttleId, this.shuttles);
|
||||||
if (shuttle != null) {
|
if (shuttle != null) {
|
||||||
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
|
this.emit(ShuttleRepositoryEvent.SHUTTLE_REMOVED, shuttle);
|
||||||
|
await this.removeShuttleLastStopIfExists(shuttleId);
|
||||||
}
|
}
|
||||||
return shuttle;
|
return shuttle;
|
||||||
}
|
}
|
||||||
@@ -282,8 +283,13 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
}, this.orderedStops);
|
}, this.orderedStops);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async removeShuttleLastStopIfExists(shuttleId: string) {
|
||||||
|
this.shuttleLastStopArrivals.delete(shuttleId);
|
||||||
|
}
|
||||||
|
|
||||||
public async clearShuttleData(): Promise<void> {
|
public async clearShuttleData(): Promise<void> {
|
||||||
this.shuttles = [];
|
this.shuttles = [];
|
||||||
|
await this.clearShuttleLastStopData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async clearOrderedStopData(): Promise<void> {
|
public async clearOrderedStopData(): Promise<void> {
|
||||||
@@ -298,4 +304,7 @@ export class UnoptimizedInMemoryShuttleRepository
|
|||||||
this.stops = [];
|
this.stops = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async clearShuttleLastStopData(): Promise<void> {
|
||||||
|
this.shuttleLastStopArrivals.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class UnoptimizedInMemoryShuttleRepositoryHolder implements RepositoryHolder<Shu
|
|||||||
factory = async () => {
|
factory = async () => {
|
||||||
return new UnoptimizedInMemoryShuttleRepository();
|
return new UnoptimizedInMemoryShuttleRepository();
|
||||||
};
|
};
|
||||||
teardown = async () => {};
|
teardown = async () => { };
|
||||||
}
|
}
|
||||||
|
|
||||||
class RedisShuttleRepositoryHolder implements RepositoryHolder<ShuttleGetterSetterRepository> {
|
class RedisShuttleRepositoryHolder implements RepositoryHolder<ShuttleGetterSetterRepository> {
|
||||||
@@ -386,6 +386,20 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
|||||||
expect(remainingShuttles).toHaveLength(mockShuttles.length - 1);
|
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 () => {
|
test("does nothing if shuttle doesn't exist", async () => {
|
||||||
const systemId = "1";
|
const systemId = "1";
|
||||||
const mockShuttles = generateMockShuttles();
|
const mockShuttles = generateMockShuttles();
|
||||||
@@ -477,6 +491,28 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
|||||||
const result = await repository.getShuttles();
|
const result = await repository.getShuttles();
|
||||||
expect(result).toEqual([]);
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user