diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index 774404b..32b317e 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -94,20 +94,54 @@ export class ApiBasedRepository implements GetterRepository { public async updateEtasForSystemIfTTL(systemId: string) { try { - const params = { - eta: "3", - stopIds: "177666", - }; + const stops = await this.getStopsBySystemId(systemId); + await Promise.all(stops.map(async (stop) => { + const params = { + eta: "3", + stopIds: stop.id, + }; - const query = new URLSearchParams(params).toString(); - const response = await fetch(`${baseUrl}?${query}`, { - method: "GET", - }); - const json = await response.json(); + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${baseUrl}?${query}`, { + method: "GET", + }); + const json = await response.json(); - if (json.ETAs && !json.ETAs["0000"]) { + if (json.ETAs && json.ETAs[systemId]) { + // Continue with the parsing + json.ETAs[systemId].forEach((jsonEta: any) => { + // Update cache + if (!this.cache.etasForStopId) { + this.cache.etasForStopId = {}; + } + + if (!this.cache.etasForShuttleId) { + this.cache.etasForShuttleId = {}; + } + + // TODO: create cache abstraction to deal with possibly undefined properties + + if (!this.cache.etasForStopId[stop.id]) { + this.cache.etasForStopId[stop.id] = [] + } + + const shuttleId: string = jsonEta.busId; + if (!this.cache.etasForShuttleId[shuttleId]) { + this.cache.etasForShuttleId[shuttleId] = []; + } + + const eta: IEta = { + secondsRemaining: jsonEta.secondsSpent, + shuttleId: `${shuttleId}`, + stopId: stop.id, + }; + + this.cache.etasForStopId[stop.id].push(eta); + this.cache.etasForShuttleId[shuttleId].push(eta); + }); + } + })); - } } catch (e) { console.error(e); } @@ -168,7 +202,8 @@ export class ApiBasedRepository implements GetterRepository { public async updateShuttlesForSystemIfTTL(systemId: string) { try { - // Update shuttleByShuttleId, shuttlesBySystemId, shuttlesByRouteId (future) + // TODO: update shuttlesByRouteId + // Update shuttleByShuttleId, shuttlesBySystemId const params = { getBuses: "2" };