mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
Refactor remaining methods in ApiBasedShuttleRepositoryLoader.ts to follow the same sub-procedure pattern
This commit is contained in:
@@ -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,21 +78,22 @@ 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,
|
||||||
});
|
});
|
||||||
const json = await response.json();
|
return await response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
private constructRoutesFromJson(json: any): IRoute[] | null {
|
||||||
if (typeof json.all === "object") {
|
if (typeof json.all === "object") {
|
||||||
await Promise.all(json.all.map(async (jsonRoute: any) => {
|
return json.all.map((jsonRoute: any) => {
|
||||||
const constructedRoute: IRoute = {
|
const constructedRoute: IRoute = {
|
||||||
name: jsonRoute.name,
|
name: jsonRoute.name,
|
||||||
color: jsonRoute.color,
|
color: jsonRoute.color,
|
||||||
@@ -75,30 +102,39 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
systemId: this.systemIdForConstructedData,
|
systemId: this.systemIdForConstructedData,
|
||||||
updatedTime: new Date(),
|
updatedTime: new Date(),
|
||||||
};
|
};
|
||||||
|
return constructedRoute;
|
||||||
await this.repository.addOrUpdateRoute(constructedRoute);
|
});
|
||||||
|
|
||||||
routeIdsToPrune.delete(constructedRoute.id);
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(Array.from(routeIdsToPrune).map(async (routeId) => {
|
return null;
|
||||||
await this.repository.removeRouteIfExists(routeId);
|
}
|
||||||
}));
|
|
||||||
|
public async updateStopAndPolylineDataForRoutesInSystem() {
|
||||||
|
try {
|
||||||
|
const json = await this.fetchStopAndPolylineDataJson();
|
||||||
|
await this.updateStopAndPolylineDataInRepository(json);
|
||||||
} 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,
|
||||||
});
|
});
|
||||||
const json = await response.json();
|
return 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,18 +268,16 @@ 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",
|
||||||
});
|
});
|
||||||
const json = await response.json();
|
return await response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
private constructEtasFromJson(json: any, stopId: string): IEta[] | null {
|
||||||
if (json.ETAs && json.ETAs[stopId]) {
|
if (json.ETAs && json.ETAs[stopId]) {
|
||||||
// Continue with the parsing
|
return json.ETAs[stopId].map((jsonEta: any) => {
|
||||||
json.ETAs[stopId].forEach((jsonEta: any) => {
|
|
||||||
// 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}`,
|
||||||
@@ -245,13 +285,11 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
updatedTime: new Date(),
|
updatedTime: new Date(),
|
||||||
systemId: this.systemIdForConstructedData,
|
systemId: this.systemIdForConstructedData,
|
||||||
};
|
};
|
||||||
|
return eta;
|
||||||
this.repository.addOrUpdateEta(eta);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch(e: any) {
|
|
||||||
throw new ApiResponseError(e.message);
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async updateStopDataForSystemAndApiResponse(
|
protected async updateStopDataForSystemAndApiResponse(
|
||||||
|
|||||||
Reference in New Issue
Block a user