diff --git a/src/helpers/event/createEventHelpers/createRecurringEvents.ts b/src/helpers/event/createEventHelpers/createRecurringEvent.ts similarity index 71% rename from src/helpers/event/createEventHelpers/createRecurringEvents.ts rename to src/helpers/event/createEventHelpers/createRecurringEvent.ts index fe952aa734..90603f35f5 100644 --- a/src/helpers/event/createEventHelpers/createRecurringEvents.ts +++ b/src/helpers/event/createEventHelpers/createRecurringEvent.ts @@ -1,6 +1,6 @@ import type mongoose from "mongoose"; import type { InterfaceEvent } from "../../../models"; -import { Event, EventAttendee, User } from "../../../models"; +import { Event } from "../../../models"; import type { MutationCreateEventArgs } from "../../../types/generatedGraphQLTypes"; import { generateRecurrenceRuleString, @@ -8,7 +8,6 @@ import { createRecurrenceRule, generateRecurringEventInstances, } from "../recurringEventHelpers"; -import { cacheEvents } from "../../../services/EventCache/cacheEvents"; import { format } from "date-fns"; /** @@ -23,17 +22,15 @@ import { format } from "date-fns"; * 4. Get the dates for recurring instances. * 5. Create a recurrenceRule document. * 6. Generate recurring instances according to the recurrence rule. - * 7. Associate the instances with the user - * 8. Cache the instances. - * @returns Created recurring event instances + * @returns Created recurring event instance */ -export const createRecurringEvents = async ( +export const createRecurringEvent = async ( args: MutationCreateEventArgs, currentUserId: string, organizationId: string, session: mongoose.ClientSession -): Promise => { +): Promise => { const { data } = args; let { recurrenceRuleData } = args; @@ -96,8 +93,8 @@ export const createRecurringEvents = async ( session ); - // generate the recurring instances - const recurringEventInstances = await generateRecurringEventInstances({ + // generate the recurring instances and get an instance back + const recurringEventInstance = await generateRecurringEventInstances({ data, baseRecurringEventId: baseRecurringEvent[0]?._id.toString(), recurrenceRuleId: recurrenceRule?._id.toString(), @@ -107,38 +104,5 @@ export const createRecurringEvents = async ( session, }); - // associate the instances with the user - const eventAttendees = recurringEventInstances.map( - (recurringEventInstance) => ({ - userId: currentUserId, - eventId: recurringEventInstance?._id.toString(), - }) - ); - - await EventAttendee.insertMany(eventAttendees, { session }); - - const eventInstanceIds = recurringEventInstances.map((instance) => - instance._id.toString() - ); - - await User.updateOne( - { _id: currentUserId }, - { - $push: { - eventAdmin: { $each: eventInstanceIds }, - createdEvents: { $each: eventInstanceIds }, - registeredEvents: { $each: eventInstanceIds }, - }, - }, - { session } - ); - - // cache the instances - await Promise.all( - recurringEventInstances.map((recurringEventInstance) => - cacheEvents([recurringEventInstance]) - ) - ); - - return recurringEventInstances; + return recurringEventInstance; }; diff --git a/src/helpers/event/createEventHelpers/createSingleEvent.ts b/src/helpers/event/createEventHelpers/createSingleEvent.ts index f0354b46e5..1b34489ee3 100644 --- a/src/helpers/event/createEventHelpers/createSingleEvent.ts +++ b/src/helpers/event/createEventHelpers/createSingleEvent.ts @@ -18,13 +18,13 @@ import { format } from "date-fns"; */ export const createSingleEvent = async ( - args: Partial, + args: MutationCreateEventArgs, currentUserId: string, organizationId: string, session: mongoose.ClientSession -): Promise> => { - const formattedStartDate = format(args.data?.startDate, "yyyy-MM-dd"); - const formattedEndDate = format(args.data?.endDate, "yyyy-MM-dd"); +): Promise => { + const formattedStartDate = format(args.data.startDate, "yyyy-MM-dd"); + const formattedEndDate = format(args.data.endDate, "yyyy-MM-dd"); // create the single event const createdEvent = await Event.create( diff --git a/src/helpers/event/createEventHelpers/index.ts b/src/helpers/event/createEventHelpers/index.ts index 2c0299750b..ebab56acb9 100644 --- a/src/helpers/event/createEventHelpers/index.ts +++ b/src/helpers/event/createEventHelpers/index.ts @@ -1,2 +1,2 @@ export { createSingleEvent } from "./createSingleEvent"; -export { createRecurringEvents } from "./createRecurringEvents"; +export { createRecurringEvent } from "./createRecurringEvent"; diff --git a/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts b/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts index a4099741ed..c02ff57e19 100644 --- a/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts +++ b/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts @@ -1,8 +1,9 @@ import type mongoose from "mongoose"; import { format } from "date-fns"; import type { InterfaceEvent } from "../../../models"; -import { Event } from "../../../models"; +import { Event, EventAttendee, User } from "../../../models"; import type { EventInput } from "../../../types/generatedGraphQLTypes"; +import { cacheEvents } from "../../../services/EventCache/cacheEvents"; /** * This function generates the recurring event instances. @@ -15,7 +16,9 @@ import type { EventInput } from "../../../types/generatedGraphQLTypes"; * @remarks The following steps are followed: * 1. Generate the instances for each provided date. * 2. Insert the documents in the database. - * @returns The recurring instances generated during this operation. + * 3. Associate the instances with the user. + * 4. Cache the instances. + * @returns A recurring instance generated during this operation. */ interface InterfaceGenerateRecurringInstances { @@ -45,7 +48,7 @@ export const generateRecurringEventInstances = async ({ currentUserId, organizationId, session, -}: InterfaceGenerateRecurringInstances): Promise => { +}: InterfaceGenerateRecurringInstances): Promise => { const recurringInstances: InterfaceRecurringEvent[] = []; recurringInstanceDates.map((date) => { const formattedInstanceDate = format(date, "yyyy-MM-dd"); @@ -71,5 +74,39 @@ export const generateRecurringEventInstances = async ({ session, }); - return recurringEventInstances; + // add eventattendee for each instance + const eventAttendees = recurringEventInstances.map( + (recurringEventInstance) => ({ + userId: currentUserId, + eventId: recurringEventInstance?._id.toString(), + }) + ); + + await EventAttendee.insertMany(eventAttendees, { session }); + + const eventInstanceIds = recurringEventInstances.map((instance) => + instance._id.toString() + ); + + // update user event fields to include generated instances + await User.updateOne( + { _id: currentUserId }, + { + $push: { + eventAdmin: { $each: eventInstanceIds }, + createdEvents: { $each: eventInstanceIds }, + registeredEvents: { $each: eventInstanceIds }, + }, + }, + { session } + ); + + // cache the instances + await Promise.all( + recurringEventInstances.map((recurringEventInstance) => + cacheEvents([recurringEventInstance]) + ) + ); + + return recurringEventInstances[0]; }; diff --git a/src/resolvers/Mutation/createEvent.ts b/src/resolvers/Mutation/createEvent.ts index 921e766c86..fdf7b700e6 100644 --- a/src/resolvers/Mutation/createEvent.ts +++ b/src/resolvers/Mutation/createEvent.ts @@ -13,7 +13,7 @@ import { compareDates } from "../../libraries/validators/compareDates"; import { session } from "../../db"; import { createSingleEvent, - createRecurringEvents, + createRecurringEvent, } from "../../helpers/event/createEventHelpers"; /** @@ -135,14 +135,12 @@ export const createEvent: MutationResolvers["createEvent"] = async ( if (args.data.recurring) { // create recurring event instances - const recurringEventInstances = await createRecurringEvents( + createdEvent = await createRecurringEvent( args, currentUser?._id.toString(), organization?._id.toString(), session ); - - createdEvent = recurringEventInstances[0]; } else { // create a single non-recurring event createdEvent = await createSingleEvent(