From c7c6a44b3756636cb797cb9fcb26428b2b6d29e4 Mon Sep 17 00:00:00 2001 From: SachsenspieltCoding Date: Tue, 30 Jul 2024 18:39:18 +0200 Subject: [PATCH 1/2] fix --- src/lib/cache/cache.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/lib/cache/cache.ts b/src/lib/cache/cache.ts index 38ec7c56..ba9d3ffe 100644 --- a/src/lib/cache/cache.ts +++ b/src/lib/cache/cache.ts @@ -32,15 +32,31 @@ export function clearCache(): void { /** * Removes all plans that are older than the given amount of days * @param {number} days The amount of days + * @param {Date[]} holidays The holidays to exclude, defaults to [] * @returns {void} */ -export function removeStale(days: number): void { +export function removeStale(days: number, holidays: Date[] = []): void { const cache = getCache(); const now = new Date(); + const startDate = now; + + // Check if today is a holiday or weekend + const isHoliday = holidays.some(holiday => holiday.toDateString() === now.toDateString()); + const isWeekend = now.getDay() === 0 || now.getDay() === 6; + + if (isHoliday || isWeekend) { + // Find the most recent school day + do { + startDate.setDate(startDate.getDate() - 1); + } while (holidays.some(holiday => holiday.toDateString() === startDate.toDateString()) || startDate.getDay() === 0 || startDate.getDay() === 6); + } + for (const schoolnumber in cache) { - cache[schoolnumber] = cache[schoolnumber].filter((plan) => { + cache[schoolnumber] = cache[schoolnumber].filter((plan, index, array) => { const date = new Date(plan.date); - return now.getTime() - date.getTime() < 1000 * 60 * 60 * 24 * days; + const isWithinRange = startDate.getTime() - date.getTime() < 1000 * 60 * 60 * 24 * days; + // Ensure at least one plan remains in the cache + return isWithinRange || array.length === 1; }); } setCache(cache); From 7b9453f691aa16b09a3efed40fedfc21f217e5b2 Mon Sep 17 00:00:00 2001 From: SachsenspieltCoding Date: Tue, 30 Jul 2024 18:41:24 +0200 Subject: [PATCH 2/2] format files --- src/lib/cache/cache.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/cache/cache.ts b/src/lib/cache/cache.ts index ba9d3ffe..dd17ee13 100644 --- a/src/lib/cache/cache.ts +++ b/src/lib/cache/cache.ts @@ -41,14 +41,18 @@ export function removeStale(days: number, holidays: Date[] = []): void { const startDate = now; // Check if today is a holiday or weekend - const isHoliday = holidays.some(holiday => holiday.toDateString() === now.toDateString()); + const isHoliday = holidays.some((holiday) => holiday.toDateString() === now.toDateString()); const isWeekend = now.getDay() === 0 || now.getDay() === 6; if (isHoliday || isWeekend) { // Find the most recent school day do { startDate.setDate(startDate.getDate() - 1); - } while (holidays.some(holiday => holiday.toDateString() === startDate.toDateString()) || startDate.getDay() === 0 || startDate.getDay() === 6); + } while ( + holidays.some((holiday) => holiday.toDateString() === startDate.toDateString()) || + startDate.getDay() === 0 || + startDate.getDay() === 6 + ); } for (const schoolnumber in cache) {