Added Debug Log and info pickup older 6month + hygiene crt

This commit is contained in:
2025-12-16 12:05:16 +01:00
parent ca81121f3d
commit f4323e20de
9 changed files with 842 additions and 20 deletions

View File

@@ -6,6 +6,7 @@ const notificationService = require('./notificationService');
const { readConfig, writeConfig } = require('./configStore');
const { readStoreWatch, writeStoreWatch } = require('./storeWatchStore');
const { setStoreStatus, persistStoreStatusCache } = require('./storeStatusCache');
const { sendDormantPickupWarning } = require('./notificationService');
function wait(ms) {
if (!ms || ms <= 0) {
@@ -557,6 +558,7 @@ function scheduleEntry(sessionId, entry, settings) {
function scheduleConfig(sessionId, config, settings) {
const resolvedSettings = resolveSettings(settings);
sessionStore.clearJobs(sessionId);
scheduleDormantMembershipCheck(sessionId);
const watchScheduled = scheduleStoreWatchers(sessionId, resolvedSettings);
const entries = Array.isArray(config) ? config : [];
const activeEntries = entries.filter((entry) => entry.active);
@@ -581,6 +583,100 @@ async function runStoreWatchCheck(sessionId, settings, options = {}) {
return checkWatchedStores(sessionId, resolvedSettings, options);
}
function setMonthOffset(date, offset) {
const copy = new Date(date.getTime());
copy.setMonth(copy.getMonth() + offset);
return copy;
}
async function checkDormantMembers(sessionId) {
const session = sessionStore.get(sessionId);
if (!session?.profile?.id) {
return;
}
const profileId = session.profile.id;
const ensured = await ensureSession(session);
if (!ensured) {
return;
}
const config = readConfig(profileId);
const skipMap = new Map();
config.forEach((entry) => {
if (entry?.id) {
skipMap.set(String(entry.id), !!entry.skipDormantCheck);
}
});
const stores = Array.isArray(session.storesCache?.data) ? session.storesCache.data : [];
if (stores.length === 0) {
console.warn(`[DORMANT] Keine Stores für Session ${sessionId} im Cache gefunden.`);
}
const fourMonthsAgo = setMonthOffset(new Date(), -4).getTime();
const hygieneCutoff = Date.now() + 6 * 7 * 24 * 60 * 60 * 1000;
for (const store of stores) {
const storeId = store?.id ? String(store.id) : null;
if (!storeId) {
continue;
}
if (skipMap.get(storeId)) {
continue;
}
let members = [];
try {
members = await foodsharingClient.fetchStoreMembers(storeId, session.cookieHeader);
} catch (error) {
console.warn(`[DORMANT] Mitglieder von Store ${storeId} konnten nicht geladen werden:`, error.message);
continue;
}
const memberEntry = members.find((m) => String(m?.id) === String(profileId));
if (!memberEntry) {
continue;
}
const reasons = [];
const lastFetchMs = memberEntry.last_fetch ? Number(memberEntry.last_fetch) * 1000 : null;
if (!lastFetchMs || lastFetchMs < fourMonthsAgo) {
const lastFetchLabel = lastFetchMs ? new Date(lastFetchMs).toLocaleDateString('de-DE') : 'unbekannt';
reasons.push(`Letzte Abholung: ${lastFetchLabel} (älter als 4 Monate)`);
}
if (memberEntry.hygiene_certificate_until) {
const expiry = new Date(memberEntry.hygiene_certificate_until.replace(' ', 'T'));
if (!Number.isNaN(expiry.getTime()) && expiry.getTime() < hygieneCutoff) {
reasons.push(
`Hygiene-Nachweis läuft bald ab: ${expiry.toLocaleDateString('de-DE')} (unter 6 Wochen)`
);
}
}
if (reasons.length > 0) {
try {
await sendDormantPickupWarning({
profileId,
storeName: store.name || `Store ${storeId}`,
storeId,
reasonLines: reasons
});
} catch (error) {
console.error(`[DORMANT] Warnung für Store ${storeId} konnte nicht versendet werden:`, error.message);
}
}
}
}
function scheduleDormantMembershipCheck(sessionId) {
const cronExpression = '0 4 */14 * *';
const job = cron.schedule(
cronExpression,
() => {
checkDormantMembers(sessionId).catch((error) => {
console.error('[DORMANT] Prüfung fehlgeschlagen:', error.message);
});
},
{ timezone: 'Europe/Berlin' }
);
sessionStore.attachJob(sessionId, job);
setTimeout(() => checkDormantMembers(sessionId), randomDelayMs(30, 180));
}
module.exports = {
scheduleConfig,
runStoreWatchCheck