From 70657505da75fe32cc475a0937f6b8d37b4ecffb Mon Sep 17 00:00:00 2001 From: benedict Date: Wed, 6 May 2026 03:21:43 +0700 Subject: [PATCH] fallback to latest available month when desired month isn't available --- generateMonthlySummary.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/generateMonthlySummary.ts b/generateMonthlySummary.ts index ffc88c3..ed6d843 100644 --- a/generateMonthlySummary.ts +++ b/generateMonthlySummary.ts @@ -36,13 +36,31 @@ interface EmployeeSummary { timestamp: string; } -async function generateMonthlySummary(month: string) { - const monthDir = join(REPORTS_DIR, month); - if (!existsSync(monthDir)) { - console.error(`Month directory not found: ${monthDir}`); +async function generateMonthlySummary(requestedMonth: string) { + let month = requestedMonth; + let monthDir = join(REPORTS_DIR, month); + + if (!existsSync(REPORTS_DIR)) { + console.error(`Reports directory not found: ${REPORTS_DIR}`); return; } + if (!existsSync(monthDir)) { + const availableMonths = readdirSync(REPORTS_DIR) + .filter(f => fs.statSync(join(REPORTS_DIR, f)).isDirectory()) + .filter(f => /^\d{4}$/.test(f)) + .sort((a, b) => b.localeCompare(a)); + + if (availableMonths.length === 0) { + console.error(`No month directories found in ${REPORTS_DIR}`); + return; + } + + month = availableMonths[0]; + monthDir = join(REPORTS_DIR, month); + console.log(`⚠️ Requested month ${requestedMonth} not found. Falling back to latest: ${month}`); + } + console.log(`Processing reports for month: ${month}...`); const employees = readdirSync(monthDir).filter(f => fs.statSync(join(monthDir, f)).isDirectory());