forked from PalisadoesFoundation/talawa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add update all recurring event instances functionality
- Loading branch information
Showing
13 changed files
with
209 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { updateSingleEvent } from "./updateSingleEvent"; | ||
export { updateRecurringEvent } from "./updateRecurringEvent"; |
63 changes: 63 additions & 0 deletions
63
src/helpers/event/updateEventHelpers/updateAllRecurringInstances.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type mongoose from "mongoose"; | ||
import { Event, InterfaceEvent } from "../../../models"; | ||
import { MutationUpdateEventArgs } from "../../../types/generatedGraphQLTypes"; | ||
import { RecurrenceRule } from "../../../models/RecurrenceRule"; | ||
|
||
export const updateAllRecurringInstances = async ( | ||
args: MutationUpdateEventArgs, | ||
event: InterfaceEvent, | ||
session: mongoose.ClientSession, | ||
): Promise<InterfaceEvent> => { | ||
const { _id: eventId, recurrenceRuleId, baseRecurringEventId } = event; | ||
|
||
const recurrenceRule = await RecurrenceRule.findOne({ | ||
_id: recurrenceRuleId, | ||
}); | ||
|
||
const baseRecurringEvent = await Event.findOne({ | ||
_id: baseRecurringEventId, | ||
}); | ||
|
||
if (!recurrenceRule || !baseRecurringEvent) { | ||
return event; | ||
} | ||
|
||
if ( | ||
(recurrenceRule.endDate === null && baseRecurringEvent.endDate === null) || | ||
(recurrenceRule.endDate && | ||
baseRecurringEvent.endDate && | ||
recurrenceRule.endDate.toISOString() === baseRecurringEvent.endDate) | ||
) { | ||
await Event.updateOne( | ||
{ | ||
_id: baseRecurringEventId, | ||
}, | ||
{ | ||
...(args.data as Partial<InterfaceEvent>), | ||
}, | ||
{ | ||
session, | ||
}, | ||
); | ||
} | ||
|
||
console.log("here"); | ||
|
||
await Event.updateMany( | ||
{ | ||
recurrenceRuleId, | ||
}, | ||
{ | ||
...(args.data as Partial<InterfaceEvent>), | ||
}, | ||
{ | ||
session, | ||
}, | ||
); | ||
|
||
const updatedEvent = await Event.findOne({ | ||
_id: eventId, | ||
}).lean(); | ||
|
||
return updatedEvent as InterfaceEvent; | ||
}; |
11 changes: 11 additions & 0 deletions
11
src/helpers/event/updateEventHelpers/updateCurrentAndFollowingInstances.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// import type mongoose from "mongoose"; | ||
import { InterfaceEvent } from "../../../models"; | ||
// import { MutationUpdateEventArgs } from "../../../types/generatedGraphQLTypes"; | ||
|
||
export const updateCurrentAndFollowingInstances = async ( | ||
// args: MutationUpdateEventArgs, | ||
event: InterfaceEvent, | ||
// session: mongoose.ClientSession, | ||
): Promise<InterfaceEvent> => { | ||
return event; | ||
}; |
65 changes: 65 additions & 0 deletions
65
src/helpers/event/updateEventHelpers/updateCurrentRecurringInstance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type mongoose from "mongoose"; | ||
import { MutationUpdateEventArgs } from "../../../types/generatedGraphQLTypes"; | ||
import { Event, InterfaceEvent } from "../../../models"; | ||
import { cacheEvents } from "../../../services/EventCache/cacheEvents"; | ||
import { RecurrenceRule } from "../../../models/RecurrenceRule"; | ||
import { generateRecurrenceRuleString } from "../recurringEventHelpers"; | ||
|
||
export const updateCurrentRecurringInstance = async ( | ||
args: MutationUpdateEventArgs, | ||
event: InterfaceEvent, | ||
session: mongoose.ClientSession, | ||
): Promise<InterfaceEvent> => { | ||
let updatedEvent: InterfaceEvent = event; | ||
|
||
const recurrenceRule = await RecurrenceRule.findOne({ | ||
_id: event.recurrenceRuleId, | ||
}); | ||
|
||
if (!recurrenceRule) { | ||
return event; | ||
} | ||
|
||
let newRecurrenceRuleString = recurrenceRule.recurrenceRuleString; | ||
let isRecurrenceRuleChanged = false; | ||
|
||
const startDate = args.data?.startDate || event.startDate; | ||
const endDate = args.data?.endDate || event.endDate; | ||
|
||
if (args.recurrenceRuleData) { | ||
newRecurrenceRuleString = generateRecurrenceRuleString( | ||
args.recurrenceRuleData, | ||
startDate, | ||
endDate, | ||
); | ||
|
||
isRecurrenceRuleChanged = | ||
recurrenceRule.recurrenceRuleString !== newRecurrenceRuleString; | ||
} | ||
|
||
if ( | ||
event.isRecurringEventException !== args.data?.isRecurringEventException || | ||
!isRecurrenceRuleChanged | ||
) { | ||
updatedEvent = await Event.findOneAndUpdate( | ||
{ | ||
_id: args.id, | ||
}, | ||
{ | ||
...(args.data as Partial<InterfaceEvent>), | ||
}, | ||
{ | ||
new: true, | ||
session, | ||
}, | ||
).lean(); | ||
|
||
if (updatedEvent !== null) { | ||
await cacheEvents([updatedEvent]); | ||
} | ||
} else { | ||
// if recurrenceRule has changed | ||
} | ||
|
||
return updatedEvent; | ||
}; |
35 changes: 35 additions & 0 deletions
35
src/helpers/event/updateEventHelpers/updateRecurringEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type mongoose from "mongoose"; | ||
import { InterfaceEvent } from "../../../models"; | ||
import { MutationUpdateEventArgs } from "../../../types/generatedGraphQLTypes"; | ||
import { updateCurrentRecurringInstance } from "./updateCurrentRecurringInstance"; | ||
import { updateAllRecurringInstances } from "./updateAllRecurringInstances"; | ||
import { updateCurrentAndFollowingInstances } from "./updateCurrentAndFollowingInstances"; | ||
|
||
export const updateRecurringEvent = async ( | ||
args: MutationUpdateEventArgs, | ||
event: InterfaceEvent, | ||
session: mongoose.ClientSession, | ||
): Promise<InterfaceEvent> => { | ||
let updatedEvent: InterfaceEvent = event; | ||
|
||
if ( | ||
(args.data?.isRecurringEventException && | ||
args.data?.isRecurringEventException !== | ||
event.isRecurringEventException) || | ||
args.recurringEventUpdateType === "ThisEvent" | ||
) { | ||
updatedEvent = await updateCurrentRecurringInstance(args, event, session); | ||
} else if (args.recurringEventUpdateType === "AllEvents") { | ||
// perform a regular bulk update on all the instances | ||
updatedEvent = await updateAllRecurringInstances(args, event, session); | ||
} else { | ||
// update current and following events | ||
updatedEvent = await updateCurrentAndFollowingInstances( | ||
event, | ||
// args, | ||
// session, | ||
); | ||
} | ||
|
||
return updatedEvent; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters