2022-04-14 21:25:24 +00:00
|
|
|
import { I18n } from "next-i18next";
|
2022-05-05 21:16:25 +00:00
|
|
|
import { RRule } from "rrule";
|
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs, { Dayjs } from "@calcom/dayjs";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { detectBrowserTimeFormat } from "@calcom/lib/timeFormat";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { inferQueryOutput } from "@calcom/trpc/react";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { RecurringEvent } from "@calcom/types/Calendar";
|
2022-04-14 21:25:24 +00:00
|
|
|
|
|
|
|
import { parseZone } from "./parseZone";
|
|
|
|
|
2022-05-05 21:16:25 +00:00
|
|
|
const processDate = (date: string | null | Dayjs, i18n: I18n) => {
|
2022-04-14 21:25:24 +00:00
|
|
|
const parsedZone = parseZone(date);
|
|
|
|
if (!parsedZone?.isValid()) return "Invalid date";
|
|
|
|
const formattedTime = parsedZone?.format(detectBrowserTimeFormat);
|
|
|
|
return formattedTime + ", " + dayjs(date).toDate().toLocaleString(i18n.language, { dateStyle: "full" });
|
|
|
|
};
|
2022-05-05 21:16:25 +00:00
|
|
|
|
|
|
|
export const parseDate = (date: string | null | Dayjs, i18n: I18n) => {
|
|
|
|
if (!date) return ["No date"];
|
|
|
|
return processDate(date, i18n);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const parseRecurringDates = (
|
|
|
|
{
|
|
|
|
startDate,
|
2022-07-07 01:08:38 +00:00
|
|
|
timeZone,
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringEvent,
|
|
|
|
recurringCount,
|
2022-07-07 01:08:38 +00:00
|
|
|
}: {
|
|
|
|
startDate: string | null | Dayjs;
|
|
|
|
timeZone?: string;
|
|
|
|
recurringEvent: RecurringEvent | null;
|
|
|
|
recurringCount: number;
|
|
|
|
},
|
2022-05-05 21:16:25 +00:00
|
|
|
i18n: I18n
|
|
|
|
): [string[], Date[]] => {
|
2022-05-17 20:43:27 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-06-10 00:32:34 +00:00
|
|
|
const { count, ...restRecurringEvent } = recurringEvent || {};
|
2022-05-05 21:16:25 +00:00
|
|
|
const rule = new RRule({
|
|
|
|
...restRecurringEvent,
|
|
|
|
count: recurringCount,
|
2022-10-17 17:28:57 +00:00
|
|
|
dtstart: new Date(
|
|
|
|
Date.UTC(
|
|
|
|
dayjs.utc(startDate).get("year"),
|
|
|
|
dayjs.utc(startDate).get("month"),
|
|
|
|
dayjs.utc(startDate).get("date"),
|
|
|
|
dayjs.utc(startDate).get("hour"),
|
|
|
|
dayjs.utc(startDate).get("minute")
|
|
|
|
)
|
|
|
|
),
|
2022-05-05 21:16:25 +00:00
|
|
|
});
|
2022-10-17 17:28:57 +00:00
|
|
|
|
|
|
|
const utcOffset = dayjs(startDate).tz(timeZone).utcOffset();
|
2022-05-05 21:16:25 +00:00
|
|
|
const dateStrings = rule.all().map((r) => {
|
2022-10-17 17:28:57 +00:00
|
|
|
return processDate(dayjs.utc(r).utcOffset(utcOffset), i18n);
|
2022-05-05 21:16:25 +00:00
|
|
|
});
|
|
|
|
return [dateStrings, rule.all()];
|
|
|
|
};
|
2022-07-19 19:06:23 +00:00
|
|
|
|
|
|
|
export const extractRecurringDates = (
|
2022-10-06 19:23:22 +00:00
|
|
|
booking: inferQueryOutput<"viewer.bookings">["bookings"][number] & {
|
|
|
|
eventType: { recurringEvent: RecurringEvent | null };
|
|
|
|
recurringEventId: string | null;
|
|
|
|
recurringBookings: inferQueryOutput<"viewer.bookings">["recurringInfo"];
|
|
|
|
},
|
2022-07-19 19:06:23 +00:00
|
|
|
timeZone: string | undefined,
|
|
|
|
i18n: I18n
|
|
|
|
): [string[], Date[]] => {
|
2022-10-06 19:23:22 +00:00
|
|
|
const { count = 0, ...rest } =
|
|
|
|
booking.eventType.recurringEvent !== null ? booking.eventType.recurringEvent : {};
|
|
|
|
const recurringInfo = booking.recurringBookings.find(
|
|
|
|
(val) => val.recurringEventId === booking.recurringEventId
|
|
|
|
);
|
|
|
|
const allDates = new RRule({
|
|
|
|
...rest,
|
|
|
|
count: recurringInfo?._count.recurringEventId,
|
|
|
|
dtstart: recurringInfo?._min.startTime,
|
|
|
|
}).all();
|
2022-10-17 17:28:57 +00:00
|
|
|
const utcOffset = dayjs(recurringInfo?._min.startTime).tz(timeZone).utcOffset();
|
2022-10-06 19:23:22 +00:00
|
|
|
const dateStrings = allDates.map((r) => {
|
2022-10-17 17:28:57 +00:00
|
|
|
return processDate(dayjs.utc(r).utcOffset(utcOffset), i18n);
|
2022-10-06 19:23:22 +00:00
|
|
|
});
|
2022-07-19 19:06:23 +00:00
|
|
|
return [dateStrings, allDates];
|
|
|
|
};
|