fallback to latest available month when desired month isn't available

master
benedict 2026-05-06 03:21:43 +07:00
parent 5e017abcf1
commit 70657505da
1 changed files with 22 additions and 4 deletions

View File

@ -36,13 +36,31 @@ interface EmployeeSummary {
timestamp: string; timestamp: string;
} }
async function generateMonthlySummary(month: string) { async function generateMonthlySummary(requestedMonth: string) {
const monthDir = join(REPORTS_DIR, month); let month = requestedMonth;
if (!existsSync(monthDir)) { let monthDir = join(REPORTS_DIR, month);
console.error(`Month directory not found: ${monthDir}`);
if (!existsSync(REPORTS_DIR)) {
console.error(`Reports directory not found: ${REPORTS_DIR}`);
return; 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}...`); console.log(`Processing reports for month: ${month}...`);
const employees = readdirSync(monthDir).filter(f => fs.statSync(join(monthDir, f)).isDirectory()); const employees = readdirSync(monthDir).filter(f => fs.statSync(join(monthDir, f)).isDirectory());