From fc12a9947d3d4d2de1a2142316951c1d8368c649 Mon Sep 17 00:00:00 2001 From: meetul Date: Fri, 9 Feb 2024 11:26:59 +0530 Subject: [PATCH] Revert "return single recurring instance" This reverts commit 60353fd63b9f5111683dfbc7694f446864c67a22. --- ...rringEvent.ts => createRecurringEvents.ts} | 50 ++++++++++++++++--- .../createEventHelpers/createSingleEvent.ts | 8 +-- src/helpers/event/createEventHelpers/index.ts | 2 +- .../generateRecurringEventInstances.ts | 45 ++--------------- src/resolvers/Mutation/createEvent.ts | 6 ++- 5 files changed, 56 insertions(+), 55 deletions(-) rename src/helpers/event/createEventHelpers/{createRecurringEvent.ts => createRecurringEvents.ts} (71%) diff --git a/src/helpers/event/createEventHelpers/createRecurringEvent.ts b/src/helpers/event/createEventHelpers/createRecurringEvents.ts similarity index 71% rename from src/helpers/event/createEventHelpers/createRecurringEvent.ts rename to src/helpers/event/createEventHelpers/createRecurringEvents.ts index 90603f35f5..fe952aa734 100644 --- a/src/helpers/event/createEventHelpers/createRecurringEvent.ts +++ b/src/helpers/event/createEventHelpers/createRecurringEvents.ts @@ -1,6 +1,6 @@ import type mongoose from "mongoose"; import type { InterfaceEvent } from "../../../models"; -import { Event } from "../../../models"; +import { Event, EventAttendee, User } from "../../../models"; import type { MutationCreateEventArgs } from "../../../types/generatedGraphQLTypes"; import { generateRecurrenceRuleString, @@ -8,6 +8,7 @@ import { createRecurrenceRule, generateRecurringEventInstances, } from "../recurringEventHelpers"; +import { cacheEvents } from "../../../services/EventCache/cacheEvents"; import { format } from "date-fns"; /** @@ -22,15 +23,17 @@ 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. - * @returns Created recurring event instance + * 7. Associate the instances with the user + * 8. Cache the instances. + * @returns Created recurring event instances */ -export const createRecurringEvent = async ( +export const createRecurringEvents = async ( args: MutationCreateEventArgs, currentUserId: string, organizationId: string, session: mongoose.ClientSession -): Promise => { +): Promise => { const { data } = args; let { recurrenceRuleData } = args; @@ -93,8 +96,8 @@ export const createRecurringEvent = async ( session ); - // generate the recurring instances and get an instance back - const recurringEventInstance = await generateRecurringEventInstances({ + // generate the recurring instances + const recurringEventInstances = await generateRecurringEventInstances({ data, baseRecurringEventId: baseRecurringEvent[0]?._id.toString(), recurrenceRuleId: recurrenceRule?._id.toString(), @@ -104,5 +107,38 @@ export const createRecurringEvent = async ( session, }); - return recurringEventInstance; + // 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; }; diff --git a/src/helpers/event/createEventHelpers/createSingleEvent.ts b/src/helpers/event/createEventHelpers/createSingleEvent.ts index 1b34489ee3..f0354b46e5 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: MutationCreateEventArgs, + args: Partial, 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 ebab56acb9..2c0299750b 100644 --- a/src/helpers/event/createEventHelpers/index.ts +++ b/src/helpers/event/createEventHelpers/index.ts @@ -1,2 +1,2 @@ export { createSingleEvent } from "./createSingleEvent"; -export { createRecurringEvent } from "./createRecurringEvent"; +export { createRecurringEvents } from "./createRecurringEvents"; diff --git a/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts b/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts index c02ff57e19..a4099741ed 100644 --- a/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts +++ b/src/helpers/event/recurringEventHelpers/generateRecurringEventInstances.ts @@ -1,9 +1,8 @@ import type mongoose from "mongoose"; import { format } from "date-fns"; import type { InterfaceEvent } from "../../../models"; -import { Event, EventAttendee, User } from "../../../models"; +import { Event } from "../../../models"; import type { EventInput } from "../../../types/generatedGraphQLTypes"; -import { cacheEvents } from "../../../services/EventCache/cacheEvents"; /** * This function generates the recurring event instances. @@ -16,9 +15,7 @@ import { cacheEvents } from "../../../services/EventCache/cacheEvents"; * @remarks The following steps are followed: * 1. Generate the instances for each provided date. * 2. Insert the documents in the database. - * 3. Associate the instances with the user. - * 4. Cache the instances. - * @returns A recurring instance generated during this operation. + * @returns The recurring instances generated during this operation. */ interface InterfaceGenerateRecurringInstances { @@ -48,7 +45,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"); @@ -74,39 +71,5 @@ export const generateRecurringEventInstances = async ({ session, }); - // 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]; + return recurringEventInstances; }; diff --git a/src/resolvers/Mutation/createEvent.ts b/src/resolvers/Mutation/createEvent.ts index fdf7b700e6..921e766c86 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, - createRecurringEvent, + createRecurringEvents, } from "../../helpers/event/createEventHelpers"; /** @@ -135,12 +135,14 @@ export const createEvent: MutationResolvers["createEvent"] = async ( if (args.data.recurring) { // create recurring event instances - createdEvent = await createRecurringEvent( + const recurringEventInstances = await createRecurringEvents( args, currentUser?._id.toString(), organization?._id.toString(), session ); + + createdEvent = recurringEventInstances[0]; } else { // create a single non-recurring event createdEvent = await createSingleEvent(