-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
Copy pathdeleteRecurringEventInstances.ts
219 lines (203 loc) · 6.42 KB
/
deleteRecurringEventInstances.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import type mongoose from "mongoose";
import type { Types } from "mongoose";
import type { InterfaceEvent, InterfaceRecurrenceRule } from "../../../models";
import {
ActionItem,
AppUserProfile,
Event,
EventAttendee,
User,
RecurrenceRule,
} from "../../../models";
import { shouldUpdateBaseRecurringEvent } from "../updateEventHelpers";
import { removeDanglingDocuments } from "../recurringEventHelpers";
/**
* Deletes all instances or thisAndFollowingInstances of a recurring event.
*
* @param event - The event instance to be deleted:
* - For thisAndFollowingInstances, represents the starting instance.
* - For allInstances, should be null.
* @param recurrenceRule - The recurrence rule associated with the instances.
* @param baseRecurringEvent - The base recurring event from which instances are derived.
*
* @remarks
* This function performs the following steps:
* 1. Constructs a query object to fetch instances based on the delete type.
* 2. Retrieves and deletes all associated documents (attendees, users, profiles, action items).
* 3. Deletes the instances themselves.
* 4. Updates the recurrence rule and base recurring event as needed.
* 5. Removes any dangling documents related to the recurrence rule and base recurring event.
*/
export const deleteRecurringEventInstances = async (
event: InterfaceEvent | null,
recurrenceRule: InterfaceRecurrenceRule,
baseRecurringEvent: InterfaceEvent,
session: mongoose.ClientSession,
): Promise<void> => {
// Construct the query object to filter events to be deleted:
// - For thisAndFollowingInstances, find all instances after (and including) this one
// - For allInstances, find all instances
const eventsQueryObject: {
recurrenceRuleId: Types.ObjectId;
baseRecurringEventId: Types.ObjectId;
isBaseRecurringEvent: boolean;
isRecurringEventException: boolean;
startDate?: { $gte: string };
} = {
recurrenceRuleId: recurrenceRule._id,
baseRecurringEventId: baseRecurringEvent._id,
isBaseRecurringEvent: false,
isRecurringEventException: false,
};
if (event) {
eventsQueryObject.startDate = { $gte: event.startDate };
}
// Get all the instances to be deleted
const recurringEventInstances = await Event.find(
{
...eventsQueryObject,
},
null,
{ session },
);
// Get the IDs of those instances
const recurringEventInstancesIds = recurringEventInstances.map(
(recurringEventInstance) => recurringEventInstance._id,
);
// Remove all associations for the instances that are being deleted
await Promise.all([
EventAttendee.deleteMany(
{ eventId: { $in: recurringEventInstancesIds } },
{ session },
),
User.updateMany(
{
registeredEvents: { $in: recurringEventInstancesIds },
},
{
$pull: {
registeredEvents: { $in: recurringEventInstancesIds },
},
},
{ session },
),
AppUserProfile.updateMany(
{
$or: [
{ createdEvents: { $in: recurringEventInstancesIds } },
{ eventAdmin: { $in: recurringEventInstancesIds } },
],
},
{
$pull: {
createdEvents: { $in: recurringEventInstancesIds },
eventAdmin: { $in: recurringEventInstancesIds },
},
},
{ session },
),
// Delete action items associated with the instances
ActionItem.deleteMany(
{ eventId: { $in: recurringEventInstancesIds } },
{ session },
),
// Delete the instances themselves
Event.deleteMany(
{
_id: { $in: recurringEventInstancesIds },
},
{
session,
},
),
]);
// Check if there are instances following the current recurrence rule
const instancesFollowingCurrentRecurrence = await Event.find(
{
recurrenceRuleId: recurrenceRule._id,
isRecurringEventException: false,
},
null,
{ session },
).sort({ startDate: -1 });
// Determine if more instances following this recurrence rule exist
const moreInstancesExist =
instancesFollowingCurrentRecurrence &&
instancesFollowingCurrentRecurrence.length;
if (moreInstancesExist) {
// Get the latest instance following the current recurrence rule
const updatedEndDateString =
instancesFollowingCurrentRecurrence[0].startDate;
const updatedEndDate = new Date(updatedEndDateString);
// Update the latestInstanceDate and recurrenceEndDate of the current recurrenceRule
await RecurrenceRule.findOneAndUpdate(
{
_id: recurrenceRule._id,
},
{
latestInstanceDate: updatedEndDate,
recurrenceEndDate: updatedEndDate,
},
{ session },
).lean();
// Update the baseRecurringEvent if it is the latest recurrence rule that the instances were following
if (
shouldUpdateBaseRecurringEvent(
recurrenceRule.recurrenceEndDate?.toString(),
baseRecurringEvent.endDate?.toString(),
)
) {
await Event.updateOne(
{
_id: baseRecurringEvent._id,
},
{
endDate: updatedEndDateString,
},
{
session,
},
);
}
} else {
// If no instances conforming to the current recurrence rule exist,
// find any previous recurrence rules associated with this baseRecurringEvent
const previousRecurrenceRules = await RecurrenceRule.find(
{
baseRecurringEventId: baseRecurringEvent._id,
recurrenceEndDate: { $lt: recurrenceRule.recurrenceStartDate },
},
null,
{ session },
)
.sort({ recurrenceEndDate: -1 })
.lean();
const previousRecurrenceRulesExist =
previousRecurrenceRules && previousRecurrenceRules.length;
if (previousRecurrenceRulesExist) {
// Update the baseRecurringEvent if it is the latest recurrence rule that the instances were following
if (
shouldUpdateBaseRecurringEvent(
recurrenceRule.recurrenceEndDate?.toString(),
baseRecurringEvent.endDate?.toString(),
)
) {
await Event.updateOne(
{
_id: baseRecurringEvent._id,
},
{
endDate: previousRecurrenceRules[0].recurrenceEndDate.toISOString(),
},
{ session },
);
}
}
}
// Remove any dangling recurrence rule and base recurring event documents
await removeDanglingDocuments(
recurrenceRule._id.toString(),
baseRecurringEvent._id.toString(),
session,
);
};