refactoring
This commit is contained in:
@@ -4,6 +4,7 @@ const sessionStore = require('./sessionStore');
|
||||
const { DEFAULT_SETTINGS } = require('./adminConfig');
|
||||
const notificationService = require('./notificationService');
|
||||
const { readConfig, writeConfig } = require('./configStore');
|
||||
const { readStoreWatch, writeStoreWatch } = require('./storeWatchStore');
|
||||
|
||||
const weekdayMap = {
|
||||
Montag: 'Monday',
|
||||
@@ -39,6 +40,13 @@ function resolveSettings(settings) {
|
||||
initialDelayMaxSeconds: Number.isFinite(settings.initialDelayMaxSeconds)
|
||||
? settings.initialDelayMaxSeconds
|
||||
: DEFAULT_SETTINGS.initialDelayMaxSeconds,
|
||||
storeWatchCron: settings.storeWatchCron || DEFAULT_SETTINGS.storeWatchCron,
|
||||
storeWatchInitialDelayMinSeconds: Number.isFinite(settings.storeWatchInitialDelayMinSeconds)
|
||||
? settings.storeWatchInitialDelayMinSeconds
|
||||
: DEFAULT_SETTINGS.storeWatchInitialDelayMinSeconds,
|
||||
storeWatchInitialDelayMaxSeconds: Number.isFinite(settings.storeWatchInitialDelayMaxSeconds)
|
||||
? settings.storeWatchInitialDelayMaxSeconds
|
||||
: DEFAULT_SETTINGS.storeWatchInitialDelayMaxSeconds,
|
||||
ignoredSlots: Array.isArray(settings.ignoredSlots) ? settings.ignoredSlots : DEFAULT_SETTINGS.ignoredSlots,
|
||||
notifications: {
|
||||
ntfy: {
|
||||
@@ -287,6 +295,78 @@ async function checkEntry(sessionId, entry, settings) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkWatchedStores(sessionId) {
|
||||
const session = sessionStore.get(sessionId);
|
||||
if (!session?.profile?.id) {
|
||||
return;
|
||||
}
|
||||
const watchers = readStoreWatch(session.profile.id);
|
||||
if (!Array.isArray(watchers) || watchers.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const ready = await ensureSession(session);
|
||||
if (!ready) {
|
||||
return;
|
||||
}
|
||||
|
||||
let changed = false;
|
||||
for (const watcher of watchers) {
|
||||
try {
|
||||
const details = await foodsharingClient.fetchStoreDetails(watcher.storeId, session.cookieHeader);
|
||||
const status = details?.teamSearchStatus === 1 ? 1 : 0;
|
||||
if (status === 1 && watcher.lastTeamSearchStatus !== 1) {
|
||||
await notificationService.sendStoreWatchNotification({
|
||||
profileId: session.profile.id,
|
||||
storeName: watcher.storeName,
|
||||
storeId: watcher.storeId,
|
||||
regionName: watcher.regionName
|
||||
});
|
||||
}
|
||||
if (watcher.lastTeamSearchStatus !== status) {
|
||||
watcher.lastTeamSearchStatus = status;
|
||||
changed = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[WATCH] Prüfung für Store ${watcher.storeId} fehlgeschlagen:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
writeStoreWatch(session.profile.id, watchers);
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleStoreWatchers(sessionId, settings) {
|
||||
const session = sessionStore.get(sessionId);
|
||||
if (!session?.profile?.id) {
|
||||
return false;
|
||||
}
|
||||
const watchers = readStoreWatch(session.profile.id);
|
||||
if (!Array.isArray(watchers) || watchers.length === 0) {
|
||||
return false;
|
||||
}
|
||||
const cronExpression = settings.storeWatchCron || DEFAULT_SETTINGS.storeWatchCron;
|
||||
const job = cron.schedule(
|
||||
cronExpression,
|
||||
() => {
|
||||
checkWatchedStores(sessionId).catch((error) => {
|
||||
console.error('[WATCH] Regelmäßige Prüfung fehlgeschlagen:', error.message);
|
||||
});
|
||||
},
|
||||
{ timezone: 'Europe/Berlin' }
|
||||
);
|
||||
sessionStore.attachJob(sessionId, job);
|
||||
setTimeout(
|
||||
() => checkWatchedStores(sessionId),
|
||||
randomDelayMs(settings.storeWatchInitialDelayMinSeconds, settings.storeWatchInitialDelayMaxSeconds)
|
||||
);
|
||||
console.log(
|
||||
`[WATCH] Überwache ${watchers.length} Betriebe für Session ${sessionId} (Cron: ${cronExpression}).`
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
function scheduleEntry(sessionId, entry, settings) {
|
||||
const cronExpression = settings.scheduleCron || DEFAULT_SETTINGS.scheduleCron;
|
||||
const job = cron.schedule(
|
||||
@@ -312,9 +392,17 @@ function scheduleEntry(sessionId, entry, settings) {
|
||||
function scheduleConfig(sessionId, config, settings) {
|
||||
const resolvedSettings = resolveSettings(settings);
|
||||
sessionStore.clearJobs(sessionId);
|
||||
const activeEntries = config.filter((entry) => entry.active);
|
||||
const watchScheduled = scheduleStoreWatchers(sessionId, resolvedSettings);
|
||||
const entries = Array.isArray(config) ? config : [];
|
||||
const activeEntries = entries.filter((entry) => entry.active);
|
||||
if (activeEntries.length === 0) {
|
||||
console.log(`[INFO] Keine aktiven Einträge für Session ${sessionId} – Scheduler ruht.`);
|
||||
if (watchScheduled) {
|
||||
console.log(
|
||||
`[INFO] Keine aktiven Pickup-Einträge für Session ${sessionId} – Store-Watch bleibt aktiv.`
|
||||
);
|
||||
} else {
|
||||
console.log(`[INFO] Keine aktiven Einträge für Session ${sessionId} – Scheduler ruht.`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
activeEntries.forEach((entry) => scheduleEntry(sessionId, entry, resolvedSettings));
|
||||
|
||||
Reference in New Issue
Block a user