-
-
Notifications
You must be signed in to change notification settings - Fork 961
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create weekly event recurring instance (#1658)
* create weekly recurring events * Commented out some test * Added testcase to support other tests * Added a single mongo session for whole application * Optimized the code and added tests * Fixed Failing Test case * Fixed Test case * Fixed linting and merge conflicts * Refactored file and added date-fns module
- Loading branch information
1 parent
ad3e16e
commit 663a08b
Showing
8 changed files
with
466 additions
and
94 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as Once from "./once"; | ||
import * as Weekly from "./weekly"; | ||
|
||
export { Once, Weekly }; |
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,29 @@ | ||
import type mongoose from "mongoose"; | ||
import type { | ||
InterfaceEvent, | ||
InterfaceOrganization, | ||
InterfaceUser, | ||
} from "../../models"; | ||
import { Event } from "../../models"; | ||
import type { MutationCreateEventArgs } from "../../types/generatedGraphQLTypes"; | ||
|
||
export async function generateEvent( | ||
args: Partial<MutationCreateEventArgs>, | ||
currentUser: InterfaceUser, | ||
organization: InterfaceOrganization, | ||
session: mongoose.ClientSession | ||
): Promise<Promise<InterfaceEvent[]>> { | ||
const createdEvent = await Event.create( | ||
[ | ||
{ | ||
...args.data, | ||
creatorId: currentUser._id, | ||
admins: [currentUser._id], | ||
organization: organization._id, | ||
}, | ||
], | ||
{ session } | ||
); | ||
|
||
return createdEvent; | ||
} |
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,53 @@ | ||
import type mongoose from "mongoose"; | ||
import type { | ||
InterfaceEvent, | ||
InterfaceOrganization, | ||
InterfaceUser, | ||
} from "../../models"; | ||
import { Event } from "../../models"; | ||
import type { MutationCreateEventArgs } from "../../types/generatedGraphQLTypes"; | ||
import { eachDayOfInterval, format } from "date-fns"; | ||
|
||
interface InterfaceRecurringEvent extends MutationCreateEventArgs { | ||
startDate: Date; | ||
creatorId: mongoose.Types.ObjectId; | ||
admins: mongoose.Types.ObjectId[]; | ||
organization: mongoose.Types.ObjectId; | ||
} | ||
|
||
export async function generateEvents( | ||
args: Partial<MutationCreateEventArgs>, | ||
currentUser: InterfaceUser, | ||
organization: InterfaceOrganization, | ||
session: mongoose.ClientSession | ||
): Promise<InterfaceEvent[]> { | ||
const recurringEvents: InterfaceRecurringEvent[] = []; | ||
const { data } = args; | ||
|
||
const startDate = new Date(data?.startDate); | ||
const endDate = new Date(data?.endDate); | ||
|
||
const allDays = eachDayOfInterval({ start: startDate, end: endDate }); | ||
const occurrences = allDays.filter( | ||
(date) => date.getDay() === startDate.getDay() | ||
); | ||
|
||
occurrences.map((date) => { | ||
const formattedDate = format(date, "yyyy-MM-dd"); | ||
|
||
const createdEvent = { | ||
...data, | ||
startDate: new Date(formattedDate), | ||
creatorId: currentUser._id, | ||
admins: [currentUser._id], | ||
organization: organization._id, | ||
}; | ||
|
||
recurringEvents.push(createdEvent); | ||
}); | ||
|
||
//Bulk insertion in database | ||
const weeklyEvents = await Event.insertMany(recurringEvents, { session }); | ||
|
||
return Array.isArray(weeklyEvents) ? weeklyEvents : [weeklyEvents]; | ||
} |
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
Oops, something went wrong.