diff --git a/src/loaders/ApiBasedRepositoryLoader.ts b/src/loaders/ApiBasedRepositoryLoader.ts index fce25df..17cbbb8 100644 --- a/src/loaders/ApiBasedRepositoryLoader.ts +++ b/src/loaders/ApiBasedRepositoryLoader.ts @@ -137,85 +137,100 @@ export class ApiBasedRepositoryLoader { } } - public async fetchAndUpdateShuttleDataForExistingSystems() { + public async fetchAndUpdateShuttleDataForExistingSystemsInRepository() { const systems = await this.repository.getSystems(); await Promise.all(systems.map(async (system: ISystem) => { - const params = { - getBuses: "2" - }; - - const formDataJsonObject = { - "s0": system.id, - "sA": "1" - }; - - const formData = new FormData(); - formData.set("json", JSON.stringify(formDataJsonObject)); - - const query = new URLSearchParams(params).toString(); - const response = await fetch(`${this.baseUrl}?${query}`, { - method: "POST", - body: formData, - }); - const json = await response.json(); - - if (json.buses && json.buses["-1"] === undefined) { - const jsonBuses = Object.values(json.buses).map((busesArr: any) => { - return busesArr[0]; - }); - - await Promise.all(jsonBuses.map(async (jsonBus: any) => { - const constructedShuttle: IShuttle = { - name: jsonBus.bus, - coordinates: { - latitude: parseFloat(jsonBus.latitude), - longitude: parseFloat(jsonBus.longitude), - }, - routeId: jsonBus.routeId, - systemId: system.id, - id: `${jsonBus.busId}` - } - - await this.repository.addOrUpdateShuttle(constructedShuttle); - })) - } + const systemId = system.id; + await this.fetchAndUpdateShuttleDataForSystemId(systemId); })); } - public async fetchAndUpdateEtaDataForExistingOrderedStops() { + public async fetchAndUpdateShuttleDataForSystemId(systemId: string) { + const params = { + getBuses: "2" + }; + + const formDataJsonObject = { + "s0": systemId, + "sA": "1" + }; + + const formData = new FormData(); + formData.set("json", JSON.stringify(formDataJsonObject)); + + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${this.baseUrl}?${query}`, { + method: "POST", + body: formData, + }); + const json = await response.json(); + + if (json.buses && json.buses["-1"] === undefined) { + const jsonBuses = Object.values(json.buses).map((busesArr: any) => { + return busesArr[0]; + }); + + await Promise.all(jsonBuses.map(async (jsonBus: any) => { + const constructedShuttle: IShuttle = { + name: jsonBus.bus, + coordinates: { + latitude: parseFloat(jsonBus.latitude), + longitude: parseFloat(jsonBus.longitude), + }, + routeId: jsonBus.routeId, + systemId: systemId, + id: `${jsonBus.busId}` + } + + await this.repository.addOrUpdateShuttle(constructedShuttle); + })) + } + } + + public async fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository() { const systems = await this.repository.getSystems() await Promise.all(systems.map(async (system: ISystem) => { - const stops = await this.repository.getStopsBySystemId(system.id); - await Promise.all(stops.map(async (stop) => { - const params = { - eta: "3", - stopIds: stop.id, + const systemId = system.id; + await this.fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId); + })) + } + + public async fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId: string) { + const stops = await this.repository.getStopsBySystemId(systemId); + await Promise.all(stops.map(async (stop) => { + let stopId = stop.id; + await this.fetchAndUpdateEtaDataForStopId(stopId); + })); + } + + public async fetchAndUpdateEtaDataForStopId(stopId: string) { + const params = { + eta: "3", + stopIds: stopId, + }; + + const query = new URLSearchParams(params).toString(); + const response = await fetch(`${this.baseUrl}?${query}`, { + method: "GET", + }); + const json = await response.json(); + + if (json.ETAs && json.ETAs[stopId]) { + // Continue with the parsing + json.ETAs[stopId].forEach((jsonEta: any) => { + // Update cache + const shuttleId: string = jsonEta.busId; + + const eta: IEta = { + secondsRemaining: jsonEta.secondsSpent, + shuttleId: `${shuttleId}`, + stopId: stopId, + millisecondsSinceEpoch: Date.now(), }; - const query = new URLSearchParams(params).toString(); - const response = await fetch(`${this.baseUrl}?${query}`, { - method: "GET", - }); - const json = await response.json(); - - if (json.ETAs && json.ETAs[stop.id]) { - // Continue with the parsing - json.ETAs[stop.id].forEach((jsonEta: any) => { - // Update cache - const shuttleId: string = jsonEta.busId; - - const eta: IEta = { - secondsRemaining: jsonEta.secondsSpent, - shuttleId: `${shuttleId}`, - stopId: stop.id, - millisecondsSinceEpoch: Date.now(), - }; - - this.repository.addOrUpdateEta(eta); - }); - } - })); - })) + this.repository.addOrUpdateEta(eta); + }); + } } protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) { diff --git a/src/loaders/TimedApiBasedRepositoryLoader.ts b/src/loaders/TimedApiBasedRepositoryLoader.ts index 7588316..763c9fa 100644 --- a/src/loaders/TimedApiBasedRepositoryLoader.ts +++ b/src/loaders/TimedApiBasedRepositoryLoader.ts @@ -54,9 +54,9 @@ export class TimedApiBasedRepositoryLoader extends ApiBasedRepositoryLoader { await this.repository.clearStopData(); await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository(); await this.repository.clearShuttleData(); - await this.fetchAndUpdateShuttleDataForExistingSystems(); + await this.fetchAndUpdateShuttleDataForExistingSystemsInRepository(); await this.repository.clearEtaData(); - await this.fetchAndUpdateEtaDataForExistingOrderedStops(); + await this.fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository(); } catch (e) { console.error(e); }