fixed the reccurring event delete bug
This commit is contained in:
@@ -419,19 +419,40 @@ router.delete('/:id', authMiddleware, async (req, res) => {
|
||||
if (!event) return res.status(404).json({ error: 'Not found' });
|
||||
const itm = await isToolManagerFn(req.schema, req.user);
|
||||
if (!itm && event.created_by !== req.user.id) return res.status(403).json({ error: 'Access denied' });
|
||||
const { recurringScope } = req.body || {};
|
||||
if (recurringScope === 'future' && event.recurrence_rule) {
|
||||
// Delete this event and all future occurrences with same creator/title
|
||||
await exec(req.schema, `
|
||||
DELETE FROM events WHERE created_by=$1 AND recurrence_rule IS NOT NULL
|
||||
AND title=$2 AND start_at >= $3
|
||||
`, [event.created_by, event.title, event.start_at]);
|
||||
} else if (recurringScope === 'all' && event.recurrence_rule) {
|
||||
// Delete present and future occurrences only — preserve past records
|
||||
await exec(req.schema, `
|
||||
DELETE FROM events WHERE created_by=$1 AND recurrence_rule IS NOT NULL AND title=$2 AND end_at >= NOW()
|
||||
`, [event.created_by, event.title]);
|
||||
const { recurringScope, occurrenceStart } = req.body || {};
|
||||
const pad = n => String(n).padStart(2, '0');
|
||||
|
||||
if (event.recurrence_rule && recurringScope === 'all') {
|
||||
// Delete the single base row — all virtual occurrences disappear with it
|
||||
await exec(req.schema, 'DELETE FROM events WHERE id=$1', [req.params.id]);
|
||||
|
||||
} else if (event.recurrence_rule && recurringScope === 'future') {
|
||||
// Truncate the series so it ends before this occurrence
|
||||
const occDate = new Date(occurrenceStart || event.start_at);
|
||||
if (occDate <= new Date(event.start_at)) {
|
||||
// Occurrence is at or before the base start — delete the whole series
|
||||
await exec(req.schema, 'DELETE FROM events WHERE id=$1', [req.params.id]);
|
||||
} else {
|
||||
const endBefore = new Date(occDate);
|
||||
endBefore.setDate(endBefore.getDate() - 1);
|
||||
const rule = { ...event.recurrence_rule };
|
||||
rule.ends = 'on';
|
||||
rule.endDate = `${endBefore.getFullYear()}-${pad(endBefore.getMonth()+1)}-${pad(endBefore.getDate())}`;
|
||||
delete rule.endCount;
|
||||
await exec(req.schema, 'UPDATE events SET recurrence_rule=$1 WHERE id=$2', [JSON.stringify(rule), req.params.id]);
|
||||
}
|
||||
|
||||
} else if (event.recurrence_rule && recurringScope === 'this') {
|
||||
// Add occurrence date to exceptions — base row and other occurrences are untouched
|
||||
const occDate = new Date(occurrenceStart || event.start_at);
|
||||
const occDateStr = `${occDate.getFullYear()}-${pad(occDate.getMonth()+1)}-${pad(occDate.getDate())}`;
|
||||
const rule = { ...event.recurrence_rule };
|
||||
const existing = Array.isArray(rule.exceptions) ? rule.exceptions : [];
|
||||
rule.exceptions = [...existing.filter(d => d !== occDateStr), occDateStr];
|
||||
await exec(req.schema, 'UPDATE events SET recurrence_rule=$1 WHERE id=$2', [JSON.stringify(rule), req.params.id]);
|
||||
|
||||
} else {
|
||||
// Non-recurring single delete
|
||||
await exec(req.schema, 'DELETE FROM events WHERE id=$1', [req.params.id]);
|
||||
}
|
||||
res.json({ success: true });
|
||||
|
||||
Reference in New Issue
Block a user