Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Feb 14, 2024
1 parent d8c8bcf commit 0e4fb12
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../recurringEventHelpers";
import { session } from "../../../db";
import type { Recurrance } from "../../../types/generatedGraphQLTypes";
import type { InterfaceGenerateRecurringInstancesData } from "../recurringEventHelpers/generateRecurringEventInstances";
import type { InterfaceRecurringEvent } from "../recurringEventHelpers/generateRecurringEventInstances";
import { RECURRING_EVENT_INSTANCES_QUERY_LIMIT } from "../../../constants";

/**
Expand All @@ -18,6 +18,7 @@ import { RECURRING_EVENT_INSTANCES_QUERY_LIMIT } from "../../../constants";
* 1. Get the limit date upto which we would want to query the recurrenceRules and generate new instances.
* 2. Get the recurrence rules to be used for instance generation during this query.
* 3. For every recurrence rule found:
* - find the base recurring event to get the data to be used for new instance generation.
* - get the number of existing instances and how many more to generate based on the recurrenceRule's count (if specified).
* - generate new instances after their latestInstanceDates.
* - update the latestInstanceDate.
Expand Down Expand Up @@ -46,43 +47,48 @@ export const createRecurringEventInstancesDuringQuery = async (
await Promise.all(
recurrenceRules.map(async (recurrenceRule) => {
// find the baseRecurringEvent for the recurrenceRule
const baseRecurringEvent = await Event.findOne({
const baseRecurringEvent = await Event.find({
_id: recurrenceRule.baseRecurringEventId,
}).lean();

if (!baseRecurringEvent) {
throw new Error();
}

// get the data from the baseRecurringEvent
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { _id, recurrance, ...data } = baseRecurringEvent;
const {
_id: baseRecurringEventId,
recurrance,
...data
} = baseRecurringEvent[0];

// get the input data for the generateRecurringEventInstances function
const currentInputData: InterfaceGenerateRecurringInstancesData = {
const currentInputData: InterfaceRecurringEvent = {
...data,
organizationId: recurrenceRule.organizationId.toString(),
recurrance: recurrance as Recurrance,
};

// get the latestInstanceDate of the current recurrenceRule
const latestInstanceDate = recurrenceRule.latestInstanceDate;
// get the properties from recurrenceRule
const {
_id: recurrenceRuleId,
latestInstanceDate,
recurrenceRuleString,
endDate: recurrenceEndDate,
count: totalInstancesCount,
} = recurrenceRule;

// get the date from which new instances would be generated
const recurrenceStartDate = addDays(latestInstanceDate, 1);
const currentRecurrenceStartDate = addDays(latestInstanceDate, 1);

// get the dates for recurrence
let recurringInstanceDates = getRecurringInstanceDates(
recurrenceRule.recurrenceRuleString,
recurrenceStartDate,
recurrenceRule.endDate,
recurrenceRuleString,
currentRecurrenceStartDate,
recurrenceEndDate,
queryUptoDate
);

// find out how many instances following the recurrence rule already exist and how many more to generate
const { count: totalInstancesCount } = recurrenceRule;
if (totalInstancesCount) {
const totalExistingInstances = await Event.countDocuments({
recurrenceRuleId: recurrenceRule._id,
recurrenceRuleId,
});

const remainingInstances = totalInstancesCount - totalExistingInstances;
Expand All @@ -100,7 +106,6 @@ export const createRecurringEventInstancesDuringQuery = async (
}

/* c8 ignore stop */

try {
if (recurringInstanceDates && recurringInstanceDates.length) {
const updatedLatestRecurringInstanceDate =
Expand All @@ -109,7 +114,7 @@ export const createRecurringEventInstancesDuringQuery = async (
// update the latestInstanceDate of the recurrenceRule
await RecurrenceRule.updateOne(
{
_id: recurrenceRule._id,
_id: recurrenceRuleId,
},
{
latestInstanceDate: updatedLatestRecurringInstanceDate,
Expand All @@ -120,10 +125,10 @@ export const createRecurringEventInstancesDuringQuery = async (
// generate recurring event instances
await generateRecurringEventInstances({
data: currentInputData,
baseRecurringEventId: baseRecurringEvent._id.toString(),
recurrenceRuleId: recurrenceRule._id.toString(),
baseRecurringEventId: baseRecurringEventId.toString(),
recurrenceRuleId: recurrenceRuleId.toString(),
recurringInstanceDates,
creatorId: baseRecurringEvent.creatorId,
creatorId: baseRecurringEvent[0].creatorId.toString(),
organizationId,
session,
});
Expand All @@ -134,10 +139,7 @@ export const createRecurringEventInstancesDuringQuery = async (
// commit transaction if everything's successful
await session.commitTransaction();
}

/* c8 ignore stop */
} catch (error) {
/* c8 ignore start */
if (session) {
// abort transaction if something fails
await session.abortTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ import { cacheEvents } from "../../../services/EventCache/cacheEvents";
* @returns A recurring instance generated during this operation.
*/

export interface InterfaceGenerateRecurringInstancesData extends EventInput {
attendees?: string;
admins?: string[];
status?: string;
}

interface InterfaceGenerateRecurringInstances {
data: InterfaceGenerateRecurringInstancesData;
data: InterfaceRecurringEvent;
baseRecurringEventId: string;
recurrenceRuleId: string;
recurringInstanceDates: Date[];
Expand All @@ -37,14 +31,13 @@ interface InterfaceGenerateRecurringInstances {
session: mongoose.ClientSession;
}

interface InterfaceRecurringEvent extends EventInput {
attendees?: string;
isBaseRecurringEvent: boolean;
recurrenceRuleId: string;
baseRecurringEventId: string;
creatorId: string;
admins: string[];
organization: string;
export interface InterfaceRecurringEvent extends EventInput {
isBaseRecurringEvent?: boolean;
recurrenceRuleId?: string;
baseRecurringEventId?: string;
creatorId?: string;
admins?: string[];
organization?: string;
status?: string;
}

Expand All @@ -61,7 +54,6 @@ export const generateRecurringEventInstances = async ({
recurringInstanceDates.map((date): void => {
const createdEventInstance = {
...data,
attendees: data.attendees,
startDate: date,
endDate: date,
recurring: true,
Expand Down
Loading

0 comments on commit 0e4fb12

Please sign in to comment.