Skip to content

Commit

Permalink
Revert "return single recurring instance"
Browse files Browse the repository at this point in the history
This reverts commit 60353fd.
  • Loading branch information
meetulr committed Feb 9, 2024
1 parent 09100f0 commit fc12a99
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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,
getRecurringInstanceDates,
createRecurrenceRule,
generateRecurringEventInstances,
} from "../recurringEventHelpers";
import { cacheEvents } from "../../../services/EventCache/cacheEvents";
import { format } from "date-fns";

/**
Expand All @@ -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<InterfaceEvent> => {
): Promise<InterfaceEvent[]> => {
const { data } = args;
let { recurrenceRuleData } = args;

Expand Down Expand Up @@ -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(),
Expand All @@ -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;
};
8 changes: 4 additions & 4 deletions src/helpers/event/createEventHelpers/createSingleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { format } from "date-fns";
*/

export const createSingleEvent = async (
args: MutationCreateEventArgs,
args: Partial<MutationCreateEventArgs>,
currentUserId: string,
organizationId: string,
session: mongoose.ClientSession
): Promise<InterfaceEvent> => {
const formattedStartDate = format(args.data.startDate, "yyyy-MM-dd");
const formattedEndDate = format(args.data.endDate, "yyyy-MM-dd");
): Promise<Promise<InterfaceEvent>> => {
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(
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/event/createEventHelpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { createSingleEvent } from "./createSingleEvent";
export { createRecurringEvent } from "./createRecurringEvent";
export { createRecurringEvents } from "./createRecurringEvents";
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -48,7 +45,7 @@ export const generateRecurringEventInstances = async ({
currentUserId,
organizationId,
session,
}: InterfaceGenerateRecurringInstances): Promise<InterfaceEvent> => {
}: InterfaceGenerateRecurringInstances): Promise<InterfaceEvent[]> => {
const recurringInstances: InterfaceRecurringEvent[] = [];
recurringInstanceDates.map((date) => {
const formattedInstanceDate = format(date, "yyyy-MM-dd");
Expand All @@ -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;
};
6 changes: 4 additions & 2 deletions src/resolvers/Mutation/createEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { compareDates } from "../../libraries/validators/compareDates";
import { session } from "../../db";
import {
createSingleEvent,
createRecurringEvent,
createRecurringEvents,
} from "../../helpers/event/createEventHelpers";

/**
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit fc12a99

Please sign in to comment.