mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
refactor remaining methods into separate methods for improved testing
This commit is contained in:
@@ -137,85 +137,100 @@ 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 params = {
|
const systemId = system.id;
|
||||||
getBuses: "2"
|
await this.fetchAndUpdateShuttleDataForSystemId(systemId);
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
const systems = await this.repository.getSystems()
|
||||||
await Promise.all(systems.map(async (system: ISystem) => {
|
await Promise.all(systems.map(async (system: ISystem) => {
|
||||||
const stops = await this.repository.getStopsBySystemId(system.id);
|
const systemId = system.id;
|
||||||
await Promise.all(stops.map(async (stop) => {
|
await this.fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId);
|
||||||
const params = {
|
}))
|
||||||
eta: "3",
|
}
|
||||||
stopIds: stop.id,
|
|
||||||
|
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();
|
this.repository.addOrUpdateEta(eta);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) {
|
protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) {
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user