Skip to content

Commit

Permalink
remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Feb 11, 2024
1 parent 4cac65e commit 8662fa1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { format } from "date-fns";
import type { RecurrenceRuleInput } from "../../../types/generatedGraphQLTypes";
import { adjustForTimezoneOffset } from "../../../utilities/recurrenceDatesUtil";
import { convertToRRuleDateString } from "../../../utilities/recurrenceDatesUtil";

/**
* This function generates the recurrence rule (rrule) string.
* @param recurrenceRuleData - the recurrenceRuleInput provided in the args.
* @param recurrenceStartDate - start date of recurrence.
* @param recurrenceEndDate - end date of recurrence.
* @remarks The following steps are followed:
* 1. Adjust the start and end dates of recurrence for timezone offsets.
* 1. Get the date strings for start and end of recurrence.
* 2. Get the recurrence rules and make a recurrenceRuleString.
* @returns The recurrence rule string that would be used to create a valid rrule object.
*/
Expand All @@ -18,52 +17,34 @@ export const generateRecurrenceRuleString = (
recurrenceStartDate: Date,
recurrenceEndDate?: Date
): string => {
// adjust the dates according to the timezone offset
recurrenceStartDate = adjustForTimezoneOffset(recurrenceStartDate);
// get the start date string for rrule's "DTSTART" property
const recurrenceStartDateString =
convertToRRuleDateString(recurrenceStartDate);

// get the end date string for rrule's "UNTIL" property
let recurrenceEndDateString = "";
if (recurrenceEndDate) {
recurrenceEndDate = adjustForTimezoneOffset(recurrenceEndDate);
recurrenceEndDateString = convertToRRuleDateString(recurrenceEndDate);
}

// recurrence start date
// (not necessarily the start date of the first recurring instance)
let formattedRecurrenceStartDate = format(
recurrenceStartDate,
"yyyyMMdd'T'HHmmss'Z'"
);

// format it to be UTC midnight
formattedRecurrenceStartDate = formattedRecurrenceStartDate.replace(
/T\d{6}Z/,
"T000000Z"
);

// date upto which instances would be generated
let formattedRecurrenceEndDate = recurrenceEndDate
? format(recurrenceEndDate, "yyyyMMdd'T'HHmmss'Z'")
: "";

// format it to be UTC midnight
formattedRecurrenceEndDate = formattedRecurrenceEndDate.replace(
/T\d{6}Z/,
"T000000Z"
);

// destructure the recurrence rules
const { frequency, count, weekDays } = recurrenceRuleData;

// string representing the days of the week the event would recur
const weekDaysString = weekDays?.length ? weekDays.join(",") : "";

// initiate recurrence rule string
let recurrenceRuleString = `DTSTART:${formattedRecurrenceStartDate}\nRRULE:FREQ=${frequency}`;
let recurrenceRuleString = `DTSTART:${recurrenceStartDateString}\nRRULE:FREQ=${frequency}`;

if (formattedRecurrenceEndDate) {
recurrenceRuleString += `;UNTIL=${formattedRecurrenceEndDate}`;
if (recurrenceEndDateString) {
recurrenceRuleString += `;UNTIL=${recurrenceEndDateString}`;
}

if (count) {
// maximum number of instances to create
recurrenceRuleString += `;COUNT=${count}`;
}

if (weekDaysString) {
recurrenceRuleString += `;BYDAY=${weekDaysString}`;
}
Expand Down
21 changes: 10 additions & 11 deletions src/utilities/recurrenceDatesUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ export const convertToUTCDate = (date: Date): Date => {
};

/**
* This function adjusts for the timezone offset.
* @param date - the date to be adjusted.
* @returns adjusted date.
* This function converts the date to a valid rrule string argument.
* @param date - the date string to be converted.
* @returns converted date string.
*/
export const adjustForTimezoneOffset = (date: Date): Date => {
const timeZoneOffset = new Date().getTimezoneOffset();

/* c8 ignore start */
if (timeZoneOffset > 0) {
date = new Date(date.getTime() + timeZoneOffset * 60 * 1000);
}
export const convertToRRuleDateString = (date: Date): string => {
let dateString = date.toISOString();

/* c8 ignore stop */
return date;
dateString = dateString.replace(/[-:]/g, "");

dateString = dateString.replace(/\.\d{3}/, "");

return dateString;
};

0 comments on commit 8662fa1

Please sign in to comment.