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

View File

@@ -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);
}