aktueller stand
This commit is contained in:
66
server.js
66
server.js
@@ -7,7 +7,12 @@ const sessionStore = require('./services/sessionStore');
|
||||
const credentialStore = require('./services/credentialStore');
|
||||
const { readConfig, writeConfig } = require('./services/configStore');
|
||||
const foodsharingClient = require('./services/foodsharingClient');
|
||||
const { scheduleConfig, runStoreWatchCheck } = require('./services/pickupScheduler');
|
||||
const {
|
||||
scheduleConfig,
|
||||
runStoreWatchCheck,
|
||||
runImmediatePickupCheck,
|
||||
runDormantMembershipCheck
|
||||
} = require('./services/pickupScheduler');
|
||||
const adminConfig = require('./services/adminConfig');
|
||||
const { readNotificationSettings, writeNotificationSettings } = require('./services/userSettingsStore');
|
||||
const notificationService = require('./services/notificationService');
|
||||
@@ -92,6 +97,7 @@ app.use((req, res, next) => {
|
||||
durationMs: Date.now() - startedAt,
|
||||
sessionId: req.session?.id || null,
|
||||
profileId: req.session?.profile?.id || null,
|
||||
profileName: req.session?.profile?.name || null,
|
||||
responseBody: responseBodySnippet
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -120,7 +126,7 @@ async function fetchProfileWithCache(session, { force = false } = {}) {
|
||||
try {
|
||||
const details = await withSessionRetry(
|
||||
session,
|
||||
() => foodsharingClient.fetchProfile(session.cookieHeader, { throwOnError: true }),
|
||||
() => foodsharingClient.fetchProfile(session.cookieHeader, { throwOnError: true }, session),
|
||||
{ label: 'fetchProfile' }
|
||||
);
|
||||
sessionStore.update(session.id, {
|
||||
@@ -191,6 +197,15 @@ function mergeStoresIntoConfig(config = [], stores = []) {
|
||||
return { merged: Array.from(map.values()), changed };
|
||||
}
|
||||
|
||||
function getMissingLastPickupStoreIds(config = []) {
|
||||
if (!Array.isArray(config)) {
|
||||
return [];
|
||||
}
|
||||
return config
|
||||
.filter((entry) => entry && entry.id && !entry.hidden && !entry.skipDormantCheck && !entry.lastPickupAt)
|
||||
.map((entry) => String(entry.id));
|
||||
}
|
||||
|
||||
function getCachedRegionStores(regionId) {
|
||||
const entry = regionStoreCache.get(String(regionId));
|
||||
if (!entry) {
|
||||
@@ -263,7 +278,7 @@ async function ensureStoreLocationIndex(session, { force = false } = {}) {
|
||||
if (!payload) {
|
||||
const result = await withSessionRetry(
|
||||
session,
|
||||
() => foodsharingClient.fetchRegionStores(region.id, session.cookieHeader),
|
||||
() => foodsharingClient.fetchRegionStores(region.id, session.cookieHeader, session),
|
||||
{ label: 'fetchRegionStores' }
|
||||
);
|
||||
payload = {
|
||||
@@ -371,7 +386,7 @@ async function refreshStoreStatus(
|
||||
try {
|
||||
const details = await withSessionRetry(
|
||||
session,
|
||||
() => foodsharingClient.fetchStoreDetails(storeId, session.cookieHeader),
|
||||
() => foodsharingClient.fetchStoreDetails(storeId, session.cookieHeader, session),
|
||||
{ label: 'fetchStoreDetails' }
|
||||
);
|
||||
const status = Number(details?.teamSearchStatus);
|
||||
@@ -542,14 +557,19 @@ async function runStoreRefreshJob(session, job) {
|
||||
const stores = await withSessionRetry(
|
||||
session,
|
||||
() =>
|
||||
foodsharingClient.fetchStores(session.cookieHeader, session.profile.id, {
|
||||
delayBetweenRequestsMs: settings.storePickupCheckDelayMs,
|
||||
onStoreCheck: (store, processed, total) => {
|
||||
job.processed = processed;
|
||||
job.total = total;
|
||||
job.currentStore = store.name || `Store ${store.id}`;
|
||||
}
|
||||
}),
|
||||
foodsharingClient.fetchStores(
|
||||
session.cookieHeader,
|
||||
session.profile.id,
|
||||
{
|
||||
delayBetweenRequestsMs: settings.storePickupCheckDelayMs,
|
||||
onStoreCheck: (store, processed, total) => {
|
||||
job.processed = processed;
|
||||
job.total = total;
|
||||
job.currentStore = store.name || `Store ${store.id}`;
|
||||
}
|
||||
},
|
||||
session
|
||||
),
|
||||
{ label: 'fetchStores' }
|
||||
);
|
||||
job.processed = stores.length;
|
||||
@@ -567,6 +587,17 @@ async function runStoreRefreshJob(session, job) {
|
||||
writeConfig(session.profile.id, config);
|
||||
scheduleWithCurrentSettings(session.id, config);
|
||||
}
|
||||
const missingLastPickupStoreIds = getMissingLastPickupStoreIds(config);
|
||||
if (missingLastPickupStoreIds.length > 0) {
|
||||
try {
|
||||
await runDormantMembershipCheck(session.id, { storeIds: missingLastPickupStoreIds });
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`[DORMANT] Letzte Abholung nach Store-Refresh konnte nicht aktualisiert werden:`,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
job.status = 'done';
|
||||
job.finishedAt = Date.now();
|
||||
@@ -828,7 +859,7 @@ app.get('/api/store-watch/regions/:regionId/stores', requireAuth, async (req, re
|
||||
try {
|
||||
const result = await withSessionRetry(
|
||||
req.session,
|
||||
() => foodsharingClient.fetchRegionStores(regionId, req.session.cookieHeader),
|
||||
() => foodsharingClient.fetchRegionStores(regionId, req.session.cookieHeader, req.session),
|
||||
{ label: 'fetchRegionStores' }
|
||||
);
|
||||
basePayload = {
|
||||
@@ -973,6 +1004,15 @@ app.post('/api/config', requireAuth, (req, res) => {
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
app.post('/api/config/check', requireAuth, (req, res) => {
|
||||
const config = readConfig(req.session.profile.id);
|
||||
const settings = adminConfig.readSettings();
|
||||
runImmediatePickupCheck(req.session.id, config, settings).catch((error) => {
|
||||
console.error('[PICKUP] Sofortprüfung fehlgeschlagen:', error.message);
|
||||
});
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
app.get('/api/notifications/settings', requireAuth, (req, res) => {
|
||||
const userSettings = readNotificationSettings(req.session.profile.id);
|
||||
const adminSettings = adminConfig.readSettings();
|
||||
|
||||
Reference in New Issue
Block a user