refactor remaining methods into separate methods for improved testing

This commit is contained in:
2025-01-22 15:16:28 -08:00
parent 95f2181b1c
commit bc3526df88
2 changed files with 87 additions and 72 deletions

View File

@@ -137,15 +137,21 @@ export class ApiBasedRepositoryLoader {
} }
} }
public async fetchAndUpdateShuttleDataForExistingSystems() { public async fetchAndUpdateShuttleDataForExistingSystemsInRepository() {
const systems = await this.repository.getSystems(); const systems = await this.repository.getSystems();
await Promise.all(systems.map(async (system: ISystem) => { await Promise.all(systems.map(async (system: ISystem) => {
const systemId = system.id;
await this.fetchAndUpdateShuttleDataForSystemId(systemId);
}));
}
public async fetchAndUpdateShuttleDataForSystemId(systemId: string) {
const params = { const params = {
getBuses: "2" getBuses: "2"
}; };
const formDataJsonObject = { const formDataJsonObject = {
"s0": system.id, "s0": systemId,
"sA": "1" "sA": "1"
}; };
@@ -172,24 +178,35 @@ export class ApiBasedRepositoryLoader {
longitude: parseFloat(jsonBus.longitude), longitude: parseFloat(jsonBus.longitude),
}, },
routeId: jsonBus.routeId, routeId: jsonBus.routeId,
systemId: system.id, systemId: systemId,
id: `${jsonBus.busId}` id: `${jsonBus.busId}`
} }
await this.repository.addOrUpdateShuttle(constructedShuttle); await this.repository.addOrUpdateShuttle(constructedShuttle);
})) }))
} }
}
public async fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository() {
const systems = await this.repository.getSystems()
await Promise.all(systems.map(async (system: ISystem) => {
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 fetchAndUpdateEtaDataForExistingOrderedStops() { public async fetchAndUpdateEtaDataForStopId(stopId: string) {
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 = { const params = {
eta: "3", eta: "3",
stopIds: stop.id, stopIds: stopId,
}; };
const query = new URLSearchParams(params).toString(); const query = new URLSearchParams(params).toString();
@@ -198,24 +215,22 @@ export class ApiBasedRepositoryLoader {
}); });
const json = await response.json(); const json = await response.json();
if (json.ETAs && json.ETAs[stop.id]) { if (json.ETAs && json.ETAs[stopId]) {
// Continue with the parsing // Continue with the parsing
json.ETAs[stop.id].forEach((jsonEta: any) => { json.ETAs[stopId].forEach((jsonEta: any) => {
// Update cache // Update cache
const shuttleId: string = jsonEta.busId; const shuttleId: string = jsonEta.busId;
const eta: IEta = { const eta: IEta = {
secondsRemaining: jsonEta.secondsSpent, secondsRemaining: jsonEta.secondsSpent,
shuttleId: `${shuttleId}`, shuttleId: `${shuttleId}`,
stopId: stop.id, stopId: stopId,
millisecondsSinceEpoch: Date.now(), millisecondsSinceEpoch: Date.now(),
}; };
this.repository.addOrUpdateEta(eta); this.repository.addOrUpdateEta(eta);
}); });
} }
}));
}))
} }
protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) { protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) {

View File

@@ -54,9 +54,9 @@ export class TimedApiBasedRepositoryLoader extends ApiBasedRepositoryLoader {
await this.repository.clearStopData(); await this.repository.clearStopData();
await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository(); await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository();
await this.repository.clearShuttleData(); await this.repository.clearShuttleData();
await this.fetchAndUpdateShuttleDataForExistingSystems(); await this.fetchAndUpdateShuttleDataForExistingSystemsInRepository();
await this.repository.clearEtaData(); await this.repository.clearEtaData();
await this.fetchAndUpdateEtaDataForExistingOrderedStops(); await this.fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository();
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }