From ae20bb9bb9229f8b2214a0553308d02211aa5198 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 7 Jan 2025 20:47:37 -0800 Subject: [PATCH] add implementation for getShuttleById --- src/repositories/ApiBasedRepository.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index 3b49aac..878c2b4 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -109,7 +109,27 @@ export class ApiBasedRepository implements GetterRepository { } public async getShuttleById(shuttleId: string): Promise { - return Promise.resolve(null); + if (!this.cache.shuttleByShuttleId) return null; + let shuttle = this.cache.shuttleByShuttleId[shuttleId]; + if (!shuttle) return null; + + // If the shuttle exists and not TTL, then return it + + const now = Date.now(); + if ( + shuttle.millisecondsSinceEpoch !== undefined + && this.ttls.shuttleByShuttleId !== undefined + && now - shuttle.millisecondsSinceEpoch > this.ttls.shuttleByShuttleId + ) { + return shuttle; + } + + // Otherwise, call getShuttlesBySystemId to update the data + await this.getShuttlesBySystemId(shuttle.systemId); + + shuttle = this.cache.shuttleByShuttleId[shuttleId]; + if (!shuttle) return null; + return shuttle; } public async getShuttlesByRouteId(routeId: string): Promise<[]> {