Refactor remaining methods in ApiBasedShuttleRepositoryLoader.ts to follow the same sub-procedure pattern

This commit is contained in:
2025-09-26 14:56:34 -07:00
parent 74a55b3f57
commit db263122a1

View File

@@ -40,11 +40,37 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
} }
public async updateRouteDataForSystem() { public async updateRouteDataForSystem() {
const systemId = this.passioSystemId; try {
const json = await this.fetchRouteDataJson();
const routes = this.constructRoutesFromJson(json);
if (routes !== null) {
await this.updateRouteDataInRepository(routes);
} else {
console.warn(`Route update failed for the following JSON: ${JSON.stringify(json)}`);
}
} catch(e: any) {
throw new ApiResponseError(e.message);
}
}
private async updateRouteDataInRepository(routes: IRoute[]) {
const routeIdsToPrune = await this.constructExistingEntityIdSet(async () => { const routeIdsToPrune = await this.constructExistingEntityIdSet(async () => {
return await this.repository.getRoutes(); return await this.repository.getRoutes();
}); });
await Promise.all(routes.map(async (route) => {
await this.repository.addOrUpdateRoute(route);
routeIdsToPrune.delete(route.id);
}));
await Promise.all(Array.from(routeIdsToPrune).map(async (routeId) => {
await this.repository.removeRouteIfExists(routeId);
}));
}
private async fetchRouteDataJson() {
const systemId = this.passioSystemId;
const params = { const params = {
getRoutes: "2", getRoutes: "2",
}; };
@@ -52,53 +78,63 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
const formDataJsonObject = { const formDataJsonObject = {
"systemSelected0": systemId, "systemSelected0": systemId,
"amount": "1", "amount": "1",
} };
const formData = new FormData(); const formData = new FormData();
formData.set("json", JSON.stringify(formDataJsonObject)); formData.set("json", JSON.stringify(formDataJsonObject));
const query = new URLSearchParams(params).toString(); const query = new URLSearchParams(params).toString();
try { const response = await fetch(`${this.baseUrl}?${query}`, {
const response = await fetch(`${this.baseUrl}?${query}`, { method: "POST",
method: "POST", body: formData,
body: formData, });
return await response.json();
}
private constructRoutesFromJson(json: any): IRoute[] | null {
if (typeof json.all === "object") {
return json.all.map((jsonRoute: any) => {
const constructedRoute: IRoute = {
name: jsonRoute.name,
color: jsonRoute.color,
id: jsonRoute.myid,
polylineCoordinates: [],
systemId: this.systemIdForConstructedData,
updatedTime: new Date(),
};
return constructedRoute;
}); });
const json = await response.json(); }
if (typeof json.all === "object") { return null;
await Promise.all(json.all.map(async (jsonRoute: any) => { }
const constructedRoute: IRoute = {
name: jsonRoute.name,
color: jsonRoute.color,
id: jsonRoute.myid,
polylineCoordinates: [],
systemId: this.systemIdForConstructedData,
updatedTime: new Date(),
};
await this.repository.addOrUpdateRoute(constructedRoute); public async updateStopAndPolylineDataForRoutesInSystem() {
try {
routeIdsToPrune.delete(constructedRoute.id); const json = await this.fetchStopAndPolylineDataJson();
})) await this.updateStopAndPolylineDataInRepository(json);
}
await Promise.all(Array.from(routeIdsToPrune).map(async (routeId) => {
await this.repository.removeRouteIfExists(routeId);
}));
} catch(e: any) { } catch(e: any) {
throw new ApiResponseError(e.message); throw new ApiResponseError(e.message);
} }
} }
public async updateStopAndPolylineDataForRoutesInSystem() { private async updateStopAndPolylineDataInRepository(json: any) {
const passioSystemId = this.passioSystemId;
// Fetch from the API
// Pass JSON output into two different methods to update repository
const stopIdsToPrune = await this.constructExistingEntityIdSet(async () => { const stopIdsToPrune = await this.constructExistingEntityIdSet(async () => {
return await this.repository.getStops(); return await this.repository.getStops();
}); });
await this.updateStopDataForSystemAndApiResponse(json, stopIdsToPrune);
await this.updateOrderedStopDataForExistingStops(json);
await this.updatePolylineDataForExistingRoutesAndApiResponse(json);
await Promise.all(Array.from(stopIdsToPrune).map(async (stopId) => {
await this.repository.removeStopIfExists(stopId);
}));
}
private async fetchStopAndPolylineDataJson() {
const passioSystemId = this.passioSystemId;
const params = { const params = {
getStops: "2", getStops: "2",
}; };
@@ -112,23 +148,11 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
const query = new URLSearchParams(params).toString(); const query = new URLSearchParams(params).toString();
try { const response = await fetch(`${this.baseUrl}?${query}`, {
const response = await fetch(`${this.baseUrl}?${query}`, { method: "POST",
method: "POST", body: formData,
body: formData, });
}); return await response.json();
const json = await response.json();
await this.updateStopDataForSystemAndApiResponse(json, stopIdsToPrune);
await this.updateOrderedStopDataForExistingStops(json);
await this.updatePolylineDataForExistingRoutesAndApiResponse(json);
await Promise.all(Array.from(stopIdsToPrune).map(async (stopId) => {
await this.repository.removeStopIfExists(stopId);
}));
} catch(e: any) {
throw new ApiResponseError(e.message);
}
} }
public async updateShuttleDataForSystem() { public async updateShuttleDataForSystem() {
@@ -219,6 +243,24 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
} }
public async updateEtaDataForStopId(stopId: string) { public async updateEtaDataForStopId(stopId: string) {
try {
const json = await this.fetchEtaDataJson(stopId);
const etas = this.constructEtasFromJson(json, stopId);
if (etas !== null) {
await this.updateEtaDataInRepository(etas);
}
} catch(e: any) {
throw new ApiResponseError(e.message);
}
}
private async updateEtaDataInRepository(etas: IEta[]) {
await Promise.all(etas.map(async (eta) => {
await this.repository.addOrUpdateEta(eta);
}));
}
private async fetchEtaDataJson(stopId: string) {
const params = { const params = {
eta: "3", eta: "3",
stopIds: stopId, stopIds: stopId,
@@ -226,32 +268,28 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
const query = new URLSearchParams(params).toString(); const query = new URLSearchParams(params).toString();
try { const response = await fetch(`${this.baseUrl}?${query}`, {
const response = await fetch(`${this.baseUrl}?${query}`, { method: "GET",
method: "GET", });
return await response.json();
}
private constructEtasFromJson(json: any, stopId: string): IEta[] | null {
if (json.ETAs && json.ETAs[stopId]) {
return json.ETAs[stopId].map((jsonEta: any) => {
const shuttleId: string = jsonEta.busId;
const eta: IEta = {
secondsRemaining: jsonEta.secondsSpent,
shuttleId: `${shuttleId}`,
stopId: stopId,
updatedTime: new Date(),
systemId: this.systemIdForConstructedData,
};
return eta;
}); });
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,
updatedTime: new Date(),
systemId: this.systemIdForConstructedData,
};
this.repository.addOrUpdateEta(eta);
});
}
} catch(e: any) {
throw new ApiResponseError(e.message);
} }
return null;
} }
protected async updateStopDataForSystemAndApiResponse( protected async updateStopDataForSystemAndApiResponse(