updated events to remove duplicates when edit reccurring
This commit is contained in:
@@ -1125,14 +1125,57 @@ function expandRecurringEvent(ev, rangeStart, rangeEnd) {
|
||||
// Expand all recurring events in a list within a date range
|
||||
function expandEvents(events, rangeStart, rangeEnd) {
|
||||
const result = [];
|
||||
|
||||
// First, collect all exception instances and their dates by master event
|
||||
const exceptionDatesByMaster = new Map();
|
||||
const masterEvents = [];
|
||||
const standaloneEvents = [];
|
||||
|
||||
for (const ev of events) {
|
||||
if (ev.recurrence_rule?.freq) {
|
||||
const expanded = expandRecurringEvent(ev, rangeStart, rangeEnd);
|
||||
result.push(...expanded);
|
||||
if (ev.recurring_master_id) {
|
||||
// This is an exception instance
|
||||
if (!exceptionDatesByMaster.has(ev.recurring_master_id)) {
|
||||
exceptionDatesByMaster.set(ev.recurring_master_id, new Set());
|
||||
}
|
||||
const dateStr = new Date(ev.original_start_at || ev.start_at).toISOString().split('T')[0];
|
||||
exceptionDatesByMaster.get(ev.recurring_master_id).add(dateStr);
|
||||
standaloneEvents.push(ev); // Exception instances are standalone events
|
||||
} else if (ev.recurrence_rule?.freq) {
|
||||
masterEvents.push(ev);
|
||||
} else {
|
||||
result.push(ev);
|
||||
standaloneEvents.push(ev);
|
||||
}
|
||||
}
|
||||
|
||||
// Expand master events, skipping dates that have exception instances
|
||||
for (const ev of masterEvents) {
|
||||
const exceptions = exceptionDatesByMaster.get(ev.id);
|
||||
if (exceptions) {
|
||||
// Merge the rule's exceptions with the instance exceptions
|
||||
const rule = ev.recurrence_rule || {};
|
||||
const ruleExceptions = new Set(rule.exceptions || []);
|
||||
for (const date of exceptions) {
|
||||
ruleExceptions.add(date);
|
||||
}
|
||||
// Create a copy of the event with merged exceptions
|
||||
const evWithMergedExceptions = {
|
||||
...ev,
|
||||
recurrence_rule: {
|
||||
...rule,
|
||||
exceptions: Array.from(ruleExceptions)
|
||||
}
|
||||
};
|
||||
const expanded = expandRecurringEvent(evWithMergedExceptions, rangeStart, rangeEnd);
|
||||
result.push(...expanded);
|
||||
} else {
|
||||
const expanded = expandRecurringEvent(ev, rangeStart, rangeEnd);
|
||||
result.push(...expanded);
|
||||
}
|
||||
}
|
||||
|
||||
// Add all standalone events (non-recurring + exception instances)
|
||||
result.push(...standaloneEvents);
|
||||
|
||||
// Sort by start_at
|
||||
result.sort((a,b) => new Date(a.start_at) - new Date(b.start_at));
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user