aktueller stand

This commit is contained in:
2026-01-29 17:50:31 +01:00
parent 916ca1dbc2
commit 9f2825edd4
4 changed files with 4794 additions and 20 deletions

View File

@@ -19,6 +19,9 @@ function wait(ms) {
const DEFAULT_STORE_WATCH_STATUS_MAX_AGE_MINUTES = 120;
const storeWatchInFlight = new Map();
const pickupCheckInFlight = new Map();
const pickupCheckLastRun = new Map();
const PICKUP_CHECK_DEDUP_MS = 30 * 1000;
async function fetchSharedStoreStatus(session, storeId, { forceRefresh = false, maxAgeMs } = {}) {
if (!storeId) {
@@ -216,6 +219,15 @@ function persistEntryDeactivation(profileId, entryId, options = {}) {
}
}
function isEntryActiveInConfig(profileId, entryId) {
if (!profileId || !entryId) {
return false;
}
const config = readConfig(profileId);
const entry = config.find((item) => String(item?.id) === String(entryId));
return !!entry && entry.active !== false;
}
function toDateValue(input) {
if (!input) {
return null;
@@ -434,18 +446,34 @@ async function checkEntry(sessionId, entry, settings) {
if (!session) {
return;
}
if (desiredWindowExpired(entry)) {
await handleExpiredDesiredWindow(session, entry);
const dedupKey = `${sessionId}:${entry?.id ?? 'unknown'}`;
const lastRun = pickupCheckLastRun.get(dedupKey);
if (lastRun && Date.now() - lastRun < PICKUP_CHECK_DEDUP_MS) {
return;
}
const ready = await ensureSession(session);
if (!ready) {
pickupCheckLastRun.set(dedupKey, Date.now());
const inFlightKey = `${sessionId}:${entry?.id ?? 'unknown'}`;
if (pickupCheckInFlight.has(inFlightKey)) {
return;
}
pickupCheckInFlight.set(inFlightKey, Date.now());
try {
if (desiredWindowExpired(entry)) {
await handleExpiredDesiredWindow(session, entry);
return;
}
if (!isEntryActiveInConfig(session.profile.id, entry.id)) {
deactivateEntryInMemory(entry);
return;
}
const ready = await ensureSession(session);
if (!ready) {
return;
}
const pickups = await withSessionRetry(
session,
() => foodsharingClient.fetchPickups(entry.id, session.cookieHeader, session),
@@ -458,21 +486,30 @@ async function checkEntry(sessionId, entry, settings) {
pickups.forEach((pickup) => {
const pickupDate = new Date(pickup.date);
if (
entry.checkProfileId &&
pickup.occupiedSlots?.some((slot) => String(slot.profile?.id) === String(session.profile.id))
) {
hasProfileId = true;
}
if (!matchesDesiredDate(pickupDate, entry.desiredDate, entry.desiredDateRange)) {
return;
}
if (!matchesDesiredWeekday(pickupDate, desiredWeekday)) {
return;
}
if (entry.checkProfileId && pickup.occupiedSlots?.some((slot) => slot.profile?.id === session.profile.id)) {
hasProfileId = true;
return;
}
if (pickup.isAvailable && !availablePickup) {
availablePickup = pickup;
}
});
if (entry.checkProfileId && hasProfileId) {
console.log(
`[INFO] Profil bereits in einem Slot für ${entry.label || entry.id} eingetragen überspringe Buchung.`
);
return;
}
if (!availablePickup) {
console.log(
`[INFO] Kein freier Slot für ${entry.label || entry.id} in dieser Runde gefunden. Profil bereits eingetragen: ${
@@ -492,6 +529,8 @@ async function checkEntry(sessionId, entry, settings) {
}
} catch (error) {
console.error(`[ERROR] Pickup-Check für Store ${entry.id} fehlgeschlagen:`, error.message);
} finally {
pickupCheckInFlight.delete(inFlightKey);
}
}
@@ -637,10 +676,6 @@ function scheduleEntry(sessionId, entry, settings) {
}
);
sessionStore.attachJob(sessionId, job);
setTimeout(
() => checkEntry(sessionId, entry, settings),
randomDelayMs(settings.initialDelayMinSeconds, settings.initialDelayMaxSeconds)
);
}
function scheduleConfig(sessionId, config, settings) {