minor changes
This commit is contained in:
131
server.js
131
server.js
@@ -27,6 +27,34 @@ const regionStoreCache = new Map();
|
||||
const REGION_STORE_CACHE_MS = 15 * 60 * 1000;
|
||||
const STORE_STATUS_MAX_AGE_MS = 60 * 24 * 60 * 60 * 1000;
|
||||
const storeStatusCache = new Map();
|
||||
const storeLocationIndex = new Map();
|
||||
let storeLocationIndexUpdatedAt = 0;
|
||||
const STORE_LOCATION_INDEX_TTL_MS = 12 * 60 * 60 * 1000;
|
||||
|
||||
function toRadians(value) {
|
||||
return (value * Math.PI) / 180;
|
||||
}
|
||||
|
||||
function haversineDistanceKm(lat1, lon1, lat2, lon2) {
|
||||
if (
|
||||
!Number.isFinite(lat1) ||
|
||||
!Number.isFinite(lon1) ||
|
||||
!Number.isFinite(lat2) ||
|
||||
!Number.isFinite(lon2)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const R = 6371;
|
||||
const dLat = toRadians(lat2 - lat1);
|
||||
const dLon = toRadians(lon2 - lon1);
|
||||
const radLat1 = toRadians(lat1);
|
||||
const radLat2 = toRadians(lat2);
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(radLat1) * Math.cos(radLat2) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return R * c;
|
||||
}
|
||||
|
||||
(function bootstrapStoreStatusCache() {
|
||||
try {
|
||||
@@ -145,6 +173,91 @@ function getCachedStoreStatus(storeId) {
|
||||
return storeStatusCache.get(String(storeId)) || null;
|
||||
}
|
||||
|
||||
function ingestStoreLocations(stores = []) {
|
||||
let changed = false;
|
||||
stores.forEach((store) => {
|
||||
const lat = Number(store?.location?.lat);
|
||||
const lon = Number(store?.location?.lon);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) {
|
||||
return;
|
||||
}
|
||||
const storeId = String(store.id);
|
||||
const entry = {
|
||||
storeId,
|
||||
lat,
|
||||
lon,
|
||||
name: store.name || `Store ${storeId}`,
|
||||
city: store.city || '',
|
||||
regionName: store.region?.name || '',
|
||||
label:
|
||||
store.city && store.region?.name && store.city.toLowerCase() !== store.region.name.toLowerCase()
|
||||
? `${store.city} • ${store.region.name}`
|
||||
: store.city || store.region?.name || store.name || `Store ${storeId}`
|
||||
};
|
||||
storeLocationIndex.set(storeId, entry);
|
||||
changed = true;
|
||||
});
|
||||
if (changed) {
|
||||
storeLocationIndexUpdatedAt = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureStoreLocationIndex(session, { force = false } = {}) {
|
||||
if (!session?.cookieHeader) {
|
||||
throw new Error('Keine gültige Session für Standortbestimmung verfügbar.');
|
||||
}
|
||||
const fresh = Date.now() - storeLocationIndexUpdatedAt < STORE_LOCATION_INDEX_TTL_MS;
|
||||
if (!force && storeLocationIndex.size > 0 && fresh) {
|
||||
return;
|
||||
}
|
||||
const details = await foodsharingClient.fetchProfile(session.cookieHeader);
|
||||
const regions = Array.isArray(details?.regions)
|
||||
? details.regions.filter((region) => Number(region?.classification) === 1)
|
||||
: [];
|
||||
if (regions.length === 0) {
|
||||
return;
|
||||
}
|
||||
for (const region of regions) {
|
||||
let payload = getCachedRegionStores(region.id);
|
||||
if (!payload) {
|
||||
const result = await foodsharingClient.fetchRegionStores(region.id, session.cookieHeader);
|
||||
payload = {
|
||||
total: Number(result?.total) || 0,
|
||||
stores: Array.isArray(result?.stores) ? result.stores : []
|
||||
};
|
||||
setCachedRegionStores(region.id, payload);
|
||||
}
|
||||
const filtered = payload.stores
|
||||
.filter((store) => Number(store.cooperationStatus) === 5)
|
||||
.map((store) => ({ ...store, id: String(store.id) }));
|
||||
ingestStoreLocations(filtered);
|
||||
}
|
||||
}
|
||||
|
||||
function findNearestStoreLocation(lat, lon) {
|
||||
if (storeLocationIndex.size === 0) {
|
||||
return null;
|
||||
}
|
||||
let closest = null;
|
||||
storeLocationIndex.forEach((entry) => {
|
||||
const distance = haversineDistanceKm(lat, lon, entry.lat, entry.lon);
|
||||
if (distance === null) {
|
||||
return;
|
||||
}
|
||||
if (!closest || distance < closest.distanceKm) {
|
||||
closest = {
|
||||
storeId: entry.storeId,
|
||||
label: entry.label,
|
||||
name: entry.name,
|
||||
city: entry.city,
|
||||
regionName: entry.regionName,
|
||||
distanceKm: distance
|
||||
};
|
||||
}
|
||||
});
|
||||
return closest;
|
||||
}
|
||||
|
||||
async function notifyWatchersForStatusChanges(changes = [], storeInfoMap = new Map()) {
|
||||
if (!Array.isArray(changes) || changes.length === 0) {
|
||||
return;
|
||||
@@ -580,6 +693,22 @@ app.get('/api/profile', requireAuth, async (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/api/location/nearest-store', requireAuth, async (req, res) => {
|
||||
const lat = Number(req.query.lat);
|
||||
const lon = Number(req.query.lon);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) {
|
||||
return res.status(400).json({ error: 'Ungültige Koordinaten' });
|
||||
}
|
||||
try {
|
||||
await ensureStoreLocationIndex(req.session);
|
||||
const store = findNearestStoreLocation(lat, lon);
|
||||
res.json({ store });
|
||||
} catch (error) {
|
||||
console.error('[LOCATION] Reverse Lookup fehlgeschlagen:', error.message);
|
||||
res.status(500).json({ error: 'Ort konnte nicht bestimmt werden' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/store-watch/regions', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const details = await foodsharingClient.fetchProfile(req.session.cookieHeader);
|
||||
@@ -627,6 +756,8 @@ app.get('/api/store-watch/regions/:regionId/stores', requireAuth, async (req, re
|
||||
.filter((store) => Number(store.cooperationStatus) === 5)
|
||||
.map((store) => ({ ...store, id: String(store.id) }));
|
||||
|
||||
ingestStoreLocations(filteredStores);
|
||||
|
||||
const { stores: enrichedStores, statusMeta } = await enrichStoresWithTeamStatus(
|
||||
filteredStores,
|
||||
req.session.cookieHeader,
|
||||
|
||||
Reference in New Issue
Block a user