Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions core/events/RecurrenceEngineV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,41 @@ export class RecurrenceEngineV2 {
next.setMonth(next.getMonth() + rule.interval);
this.setToNthWeekdayOfMonth(next, byDay.weekday, nthOccurrence);
} else if (rule.bySetPos && rule.bySetPos.length > 0) {
// BYSETPOS for selecting from set
// BYSETPOS selects from the set of candidates generated by other BY* rules
next.setMonth(next.getMonth() + rule.interval);
// Complex BYSETPOS logic would go here
next.setDate(1);

const dayMap = { SU: 0, MO: 1, TU: 2, WE: 3, TH: 4, FR: 5, SA: 6 };
const candidates = [];

if (rule.byDay && rule.byDay.length > 0) {
// Generate all matching weekday occurrences in the month
const lastDay = new Date(next.getFullYear(), next.getMonth() + 1, 0).getDate();
for (let d = 1; d <= lastDay; d++) {
const date = new Date(next.getFullYear(), next.getMonth(), d);
const dayOfWeek = date.getDay();
for (const byDay of rule.byDay) {
const dayStr = typeof byDay === 'string' ? byDay.replace(/^-?\d+/, '') : byDay.weekday;
if (dayMap[dayStr] === dayOfWeek) {
candidates.push(d);
break;
}
}
}
}

// Apply BYSETPOS indices (1-based, negative from end)
const selectedDays = [];
for (const pos of rule.bySetPos) {
const index = pos > 0 ? pos - 1 : candidates.length + pos;
if (index >= 0 && index < candidates.length) {
selectedDays.push(candidates[index]);
}
}

if (selectedDays.length > 0) {
next.setDate(selectedDays[0]);
}
} else {
// Same day of next month
const currentDay = next.getDate();
Expand Down
Loading