Improve consecutive group calculation to only exclude all weekend groups

This commit is contained in:
Zachary
2024-11-22 20:46:35 +01:00
parent 98be02de8c
commit 1c4aa803bd
2 changed files with 12 additions and 7 deletions

View File

@@ -56,14 +56,14 @@ export function calculateConsecutiveDaysOff(holidays: { date: Date }[], optimize
if (isWeekend(d, weekendDays) || isHoliday(d, holidays) || allDaysOff.has(dateKey(d))) { if (isWeekend(d, weekendDays) || isHoliday(d, holidays) || allDaysOff.has(dateKey(d))) {
currentGroup.push(new Date(d)); currentGroup.push(new Date(d));
} else if (currentGroup.length > 0) { } else if (currentGroup.length > 0) {
if (hasWeekendAndNonWeekendHoliday(currentGroup, weekendDays, holidays, optimizedDaysOff)) { if (isValidConsecutiveGroup(currentGroup, weekendDays)) {
consecutiveDaysOff.push(createPeriod(currentGroup, optimizedDaysOff)); consecutiveDaysOff.push(createPeriod(currentGroup, optimizedDaysOff));
} }
currentGroup = []; currentGroup = [];
} }
} }
if (currentGroup.length > 0 && hasWeekendAndNonWeekendHoliday(currentGroup, weekendDays, holidays, optimizedDaysOff)) { if (currentGroup.length > 0 && isValidConsecutiveGroup(currentGroup, weekendDays)) {
consecutiveDaysOff.push(createPeriod(currentGroup, optimizedDaysOff)); consecutiveDaysOff.push(createPeriod(currentGroup, optimizedDaysOff));
} }
@@ -162,10 +162,16 @@ function selectDaysOff(rankedGaps: any[], daysOff: number, allDaysOff: Set<strin
return selectedDays; return selectedDays;
} }
// Check if a group contains both a weekend day and a non-weekend holiday/PTO day // Check if a group is valid (2+ days, not just weekends)
function hasWeekendAndNonWeekendHoliday(group: Date[], weekendDays: number[], holidays: { date: Date }[], optimizedDaysOff: Date[]) { function isValidConsecutiveGroup(group: Date[], weekendDays: number[]): boolean {
return group.some(d => weekendDays.includes(d.getDay())) && // Must be at least 2 days
group.some(d => !weekendDays.includes(d.getDay()) && (isHoliday(d, holidays) || optimizedDaysOff.some(od => dateKey(od) === dateKey(d)))); if (group.length < 2) return false;
// Check if ALL days are weekends
const allDaysAreWeekends = group.every(d => weekendDays.includes(d.getDay()));
// Valid if not all days are weekends
return !allDaysAreWeekends;
} }
// Create a period object from a group of consecutive days // Create a period object from a group of consecutive days

View File

@@ -253,7 +253,6 @@
$: visibleHolidaysCount = holidays.filter(h => !h.hidden).length; $: visibleHolidaysCount = holidays.filter(h => !h.hidden).length;
function toggleWeekendDay(dayNumber: number) { function toggleWeekendDay(dayNumber: number) {
console.log('Toggling weekend day:', dayNumber);
if (weekendDays.includes(dayNumber)) { if (weekendDays.includes(dayNumber)) {
weekendDays = weekendDays.filter(d => d !== dayNumber); weekendDays = weekendDays.filter(d => d !== dayNumber);
} else { } else {