* refactor: Remove intervalLimits from @calcom/lib and export directly (#19710) * refactor: Remove intervalLimits from @calcom/lib and export directly * Tackle other places that use parseBookingLimit/parseDurationLimit * More type fixups that were hidden by previous fails * Fixed up booking-limits file * Remove server-only ## What does this PR do? <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> - Fixes #XXXX (GitHub issue number) - Fixes CAL-XXXX (Linear issue number - should be visible at the bottom of the GitHub issue description) ## Visual Demo (For contributors especially) A visual demonstration is strongly recommended, for both the original and new change **(video / image - any one)**. #### Video Demo (if applicable): - Show screen recordings of the issue or feature. - Demonstrate how to reproduce the issue, the behavior before and after the change. #### Image Demo (if applicable): - Add side-by-side screenshots of the original and updated change. - Highlight any significant change(s). ## Mandatory Tasks (DO NOT REMOVE) - [ ] I have self-reviewed the code (A decent size PR without self-review might be rejected). - [ ] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). If N/A, write N/A here and check the checkbox. - [ ] I confirm automated tests are in place that prove my fix is effective or that my feature works. ## How should this be tested? <!-- Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. Write details that help to start the tests --> - Are there environment variables that should be set? - What are the minimal test data to have? - What is expected (happy path) to have (input and output)? - Any other important info that could help to test that PR ## Checklist <!-- Remove bullet points below that don't apply to you --> - I haven't read the [contributing guide](https://github.com/calcom/cal.com/blob/main/CONTRIBUTING.md) - My code doesn't follow the style guidelines of this project - I haven't commented my code, particularly in hard-to-understand areas - I haven't checked if my changes generate no new warnings * Add unit tests and e2e * fix ts errors * Add comment * bump platform libs * fix: yarn-lock * fix yarn.lock * doc update * Add isString --------- Co-authored-by: Morgan Vernay <[email protected]>
256 lines
7.0 KiB
TypeScript
256 lines
7.0 KiB
TypeScript
import type { Prisma } from "@prisma/client";
|
|
import type { TFunction } from "i18next";
|
|
import { createEvent } from "ics";
|
|
import { RRule } from "rrule";
|
|
import type { z } from "zod";
|
|
|
|
import type { Dayjs } from "@calcom/dayjs";
|
|
import dayjs from "@calcom/dayjs";
|
|
import type { nameObjectSchema } from "@calcom/lib/event";
|
|
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
|
import type { RecurringEvent } from "@calcom/types/Calendar";
|
|
|
|
import { getEventName } from "../event";
|
|
|
|
type RecurringEventOrPrismaJsonObject = RecurringEvent | Prisma.JsonObject | null | undefined;
|
|
export const enum CalendarLinkType {
|
|
GOOGLE_CALENDAR = "googleCalendar",
|
|
MICROSOFT_OFFICE = "microsoftOffice",
|
|
MICROSOFT_OUTLOOK = "microsoftOutlook",
|
|
ICS = "ics",
|
|
}
|
|
|
|
const buildICalLink = ({
|
|
startTime,
|
|
endTime,
|
|
title,
|
|
description,
|
|
location,
|
|
}: {
|
|
startTime: Dayjs;
|
|
endTime: Dayjs;
|
|
title: string;
|
|
description: string | null;
|
|
location: string | null;
|
|
}) => {
|
|
const durationInMinutes = endTime.diff(startTime, "minutes");
|
|
|
|
const iCalEvent = createEvent({
|
|
start: [
|
|
startTime.toDate().getUTCFullYear(),
|
|
(startTime.toDate().getUTCMonth() as number) + 1,
|
|
startTime.toDate().getUTCDate(),
|
|
startTime.toDate().getUTCHours(),
|
|
startTime.toDate().getUTCMinutes(),
|
|
],
|
|
startInputType: "utc",
|
|
title,
|
|
duration: {
|
|
minutes: durationInMinutes,
|
|
},
|
|
...(description ? { description } : {}),
|
|
...(location ? { location } : {}),
|
|
});
|
|
|
|
if (iCalEvent.error) {
|
|
throw iCalEvent.error;
|
|
}
|
|
|
|
return `data:text/calendar,${encodeURIComponent(iCalEvent.value ? iCalEvent.value : false)}`;
|
|
};
|
|
|
|
const buildGoogleCalendarLink = ({
|
|
startTime,
|
|
endTime,
|
|
eventName,
|
|
eventDescription,
|
|
bookingLocation,
|
|
recurringEvent,
|
|
}: {
|
|
startTime: Dayjs;
|
|
endTime: Dayjs;
|
|
eventName: string;
|
|
eventDescription: string | null;
|
|
bookingLocation: string | null;
|
|
recurringEvent: RecurringEventOrPrismaJsonObject;
|
|
}) => {
|
|
const startTimeInUtcFormat = startTime.utc().format("YYYYMMDDTHHmmss[Z]");
|
|
const endTimeInUtcFormat = endTime.utc().format("YYYYMMDDTHHmmss[Z]");
|
|
const recurrence = recurringEvent ? encodeURIComponent(new RRule(recurringEvent).toString()) : "";
|
|
|
|
const location = bookingLocation ? encodeURIComponent(bookingLocation) : "";
|
|
const description = encodeURIComponent(eventDescription ?? "");
|
|
|
|
const googleCalendarLink = `https://calendar.google.com/calendar/r/eventedit?dates=${startTimeInUtcFormat}/${endTimeInUtcFormat}&text=${eventName}&details=${description}${
|
|
location ? `&location=${location}` : ""
|
|
}${recurrence ? `&recur=${recurrence}` : ""}`;
|
|
|
|
return googleCalendarLink;
|
|
};
|
|
|
|
const buildMicrosoftOfficeLink = ({
|
|
startTime,
|
|
endTime,
|
|
eventName,
|
|
eventDescription,
|
|
bookingLocation,
|
|
}: {
|
|
startTime: Dayjs;
|
|
endTime: Dayjs;
|
|
eventName: string;
|
|
eventDescription: string | null;
|
|
bookingLocation: string | null;
|
|
}) => {
|
|
const startTimeInUtcFormat = startTime.utc().format();
|
|
const endTimeInUtcFormat = endTime.utc().format();
|
|
const location = bookingLocation ? encodeURIComponent(bookingLocation) : "";
|
|
const description = encodeURIComponent(eventDescription ?? "");
|
|
|
|
// TODO: Why do we need to encode URI this href but not the google calendar link?
|
|
const microsoftOfficeLink = `https://outlook.office.com/calendar/0/deeplink/compose?body=${description}&enddt=${endTimeInUtcFormat}&path=%2Fcalendar%2Faction%2Fcompose&rru=addevent&startdt=${startTimeInUtcFormat}&subject=${eventName}${
|
|
location ? `&location=${location}` : ""
|
|
}`;
|
|
return microsoftOfficeLink;
|
|
};
|
|
|
|
const buildMicrosoftOutlookLink = ({
|
|
startTime,
|
|
endTime,
|
|
eventName,
|
|
eventDescription,
|
|
bookingLocation,
|
|
}: {
|
|
startTime: Dayjs;
|
|
endTime: Dayjs;
|
|
eventName: string;
|
|
eventDescription: string | null;
|
|
bookingLocation: string | null;
|
|
}) => {
|
|
const startTimeInUtcFormat = startTime.utc().format();
|
|
const endTimeInUtcFormat = endTime.utc().format();
|
|
const location = bookingLocation ? encodeURIComponent(bookingLocation) : "";
|
|
const microsoftOutlookLink =
|
|
encodeURI(
|
|
`https://outlook.live.com/calendar/0/deeplink/compose?body=${eventDescription}&enddt=${endTimeInUtcFormat}&path=%2Fcalendar%2Faction%2Fcompose&rru=addevent&startdt=${startTimeInUtcFormat}&subject=${eventName}`
|
|
) + (location ? `&location=${location}` : "");
|
|
return microsoftOutlookLink;
|
|
};
|
|
|
|
export const getCalendarLinks = ({
|
|
booking,
|
|
eventType,
|
|
t,
|
|
}: {
|
|
booking: {
|
|
startTime: Date;
|
|
endTime: Date;
|
|
location: string | null;
|
|
title: string;
|
|
responses: Prisma.JsonObject;
|
|
metadata: Prisma.JsonObject | null;
|
|
};
|
|
eventType: {
|
|
recurringEvent: RecurringEventOrPrismaJsonObject;
|
|
description?: string | null;
|
|
eventName?: string | null;
|
|
isDynamic: boolean;
|
|
length: number;
|
|
team?: {
|
|
name: string;
|
|
} | null;
|
|
users: {
|
|
name: string | null;
|
|
}[];
|
|
title: string;
|
|
};
|
|
t: TFunction;
|
|
}) => {
|
|
let evtName = eventType.eventName;
|
|
const videoCallUrl = bookingMetadataSchema.parse(booking?.metadata || {})?.videoCallUrl ?? null;
|
|
|
|
if (eventType.isDynamic && booking.responses.title) {
|
|
evtName = booking.responses.title as string;
|
|
}
|
|
const eventNameObject = {
|
|
attendeeName: booking.responses.name as z.infer<typeof nameObjectSchema> | string,
|
|
eventType: eventType.title,
|
|
eventName: evtName,
|
|
host: eventType.team?.name || eventType.users[0]?.name || "Nameless",
|
|
location: booking.location,
|
|
bookingFields: booking.responses,
|
|
eventDuration: eventType.length,
|
|
t,
|
|
};
|
|
|
|
// Create event name and description
|
|
const eventName = getEventName(eventNameObject, true);
|
|
const eventDescription = eventType.description || "";
|
|
|
|
// Calculate start and end times
|
|
const startTime = dayjs(booking.startTime);
|
|
const endTime = dayjs(booking.endTime);
|
|
const recurringEvent = eventType.recurringEvent;
|
|
|
|
const googleCalendarLink = buildGoogleCalendarLink({
|
|
startTime,
|
|
endTime,
|
|
eventName,
|
|
eventDescription,
|
|
bookingLocation: videoCallUrl ?? null,
|
|
recurringEvent,
|
|
});
|
|
|
|
const microsoftOfficeLink = buildMicrosoftOfficeLink({
|
|
startTime,
|
|
endTime,
|
|
eventName,
|
|
eventDescription,
|
|
bookingLocation: videoCallUrl,
|
|
});
|
|
|
|
const microsoftOutlookLink = buildMicrosoftOutlookLink({
|
|
startTime,
|
|
endTime,
|
|
eventName,
|
|
eventDescription,
|
|
bookingLocation: videoCallUrl,
|
|
});
|
|
|
|
// Generate ICS file link
|
|
let icsFileLink = "";
|
|
try {
|
|
icsFileLink = buildICalLink({
|
|
startTime,
|
|
endTime,
|
|
title: eventName,
|
|
description: eventDescription ?? null,
|
|
location: videoCallUrl ?? null,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error generating ICS file", error);
|
|
}
|
|
|
|
return [
|
|
{
|
|
label: "Google Calendar",
|
|
id: CalendarLinkType.GOOGLE_CALENDAR,
|
|
link: googleCalendarLink,
|
|
},
|
|
{
|
|
label: "Microsoft Office",
|
|
id: CalendarLinkType.MICROSOFT_OFFICE,
|
|
link: microsoftOfficeLink,
|
|
},
|
|
{
|
|
label: "Microsoft Outlook",
|
|
id: CalendarLinkType.MICROSOFT_OUTLOOK,
|
|
link: microsoftOutlookLink,
|
|
},
|
|
{
|
|
label: "ICS",
|
|
id: CalendarLinkType.ICS,
|
|
link: icsFileLink,
|
|
},
|
|
];
|
|
};
|