implement pruning code

This commit is contained in:
2025-01-22 21:10:59 -08:00
parent a8594032bc
commit e7e513b0b1

View File

@@ -1,5 +1,5 @@
import { GetterSetterRepository } from "../repositories/GetterSetterRepository";
import { IEta, IRoute, IShuttle, IStop, ISystem } from "../entities/entities";
import { IEntityWithId, IEta, IRoute, IShuttle, IStop, ISystem } from "../entities/entities";
export class ApiResponseError extends Error {
constructor(message: string) {
@@ -17,12 +17,25 @@ export class ApiBasedRepositoryLoader {
) {
}
private async constructExistingEntityIdSet<T extends IEntityWithId>(entitySearchCallback: () => Promise<T[]>) {
const existingEntities = await entitySearchCallback();
const ids = new Set<string>();
existingEntities.forEach((entity) => {
ids.add(entity.id);
});
return ids;
}
public async fetchAndUpdateSystemData() {
const params = {
getSystems: "2",
};
const query = new URLSearchParams(params).toString();
const systemIds = await this.constructExistingEntityIdSet(async () => {
return await this.repository.getSystems();
})
try {
const response = await fetch(`${this.baseUrl}?${query}`);
const json = await response.json();
@@ -41,10 +54,18 @@ export class ApiBasedRepositoryLoader {
};
await this.repository.addOrUpdateSystem(constructedSystem);
if (systemIds.has(constructedSystem.id)) {
systemIds.delete(constructedSystem.id);
}
}));
} else {
throw new Error("Received JSON object does not contain `all` field")
}
// Prune systems
await Promise.all(Array.from(systemIds).map(async (systemId) => {
await this.repository.removeSystemIfExists(systemId);
}));
} catch(e: any) {
throw new ApiResponseError(e.message);
}