refactor fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId into separate method

This commit is contained in:
2025-01-22 14:55:51 -08:00
parent 4d5e563f9c
commit b2be3ae583

View File

@@ -103,30 +103,34 @@ export class ApiBasedRepositoryLoader {
// Pass JSON output into two different methods to update repository // Pass JSON output into two different methods to update repository
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 = { await this.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(system.id);
getStops: "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();
await this.updateStopDataForSystemAndApiResponse(system, json);
await this.updateOrderedStopDataForExistingStops(json);
await this.updatePolylineDataForExistingRoutesAndApiResponse(json);
})); }));
} }
public async fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string) {
const params = {
getStops: "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();
await this.updateStopDataForSystemAndApiResponse(systemId, json);
await this.updateOrderedStopDataForExistingStops(json);
await this.updatePolylineDataForExistingRoutesAndApiResponse(json);
}
public async fetchAndUpdateShuttleDataForExistingSystems() { public async fetchAndUpdateShuttleDataForExistingSystems() {
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) => {
@@ -208,7 +212,7 @@ export class ApiBasedRepositoryLoader {
})) }))
} }
protected async updateStopDataForSystemAndApiResponse(system: ISystem, json: any) { protected async updateStopDataForSystemAndApiResponse(systemId: string, json: any) {
if (json.stops) { if (json.stops) {
const jsonStops = Object.values(json.stops); const jsonStops = Object.values(json.stops);
@@ -216,7 +220,7 @@ export class ApiBasedRepositoryLoader {
const constructedStop: IStop = { const constructedStop: IStop = {
name: stop.name, name: stop.name,
id: stop.id, id: stop.id,
systemId: system.id, systemId,
coordinates: { coordinates: {
latitude: parseFloat(stop.latitude), latitude: parseFloat(stop.latitude),
longitude: parseFloat(stop.longitude), longitude: parseFloat(stop.longitude),