-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
removeDanglingDocuments.ts
59 lines (53 loc) · 1.8 KB
/
removeDanglingDocuments.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
import type mongoose from "mongoose";
import { Event, RecurrenceRule } from "../../../models";
/**
* This function removes dangling recurrence rule and base recurring event documents.
* @param baseRecurringEventId - _id of the base recurring event.
* @param recurrenceRuleId - _id of the recurrence rule.
* @remarks The following steps are followed:
* 1. Call the function associated with the document to be removed, i.e. removeRecurrenceRule or removeBaseRecurringEvent.
* 2. Check if the document has any associations, i.e.:
* - for recurrence rule, check if there exist any event that follow this given recurrence rule
* - for base recurring event, check if there exist any event that has this event as its base recurring event
* 3. Remove the documents if no associations are found.
*/
export const removeDanglingDocuments = async (
recurrenceRuleId: string,
baseRecurringEventId: string,
session: mongoose.ClientSession,
): Promise<void> => {
await removeRecurrenceRule(recurrenceRuleId, session);
await removeBaseRecurringEvent(baseRecurringEventId, session);
};
const removeRecurrenceRule = async (
recurrenceRuleId: string,
session: mongoose.ClientSession,
): Promise<void> => {
const eventsFollowingRecurrenceRule = await Event.exists({
recurrenceRuleId,
});
if (!eventsFollowingRecurrenceRule) {
await RecurrenceRule.deleteOne(
{
_id: recurrenceRuleId,
},
{ session },
);
}
};
const removeBaseRecurringEvent = async (
baseRecurringEventId: string,
session: mongoose.ClientSession,
): Promise<void> => {
const eventsHavingBaseRecurringEvent = await Event.exists({
baseRecurringEventId,
});
if (!eventsHavingBaseRecurringEvent) {
await Event.deleteOne(
{
_id: baseRecurringEventId,
},
{ session },
);
}
};