Add booking actions

refactor/generate-ics-event
Joe Au-Yeung 2023-10-31 12:55:44 -04:00
parent a6f881c087
commit 929e76020f
1 changed files with 52 additions and 12 deletions

View File

@ -5,19 +5,28 @@ import { RRule } from "rrule";
import dayjs from "@calcom/dayjs"; import dayjs from "@calcom/dayjs";
import { getRichDescription } from "@calcom/lib/CalEventParser"; import { getRichDescription } from "@calcom/lib/CalEventParser";
import { getWhen } from "@calcom/lib/CalEventParser";
import type { CalendarEvent, Person } from "@calcom/types/Calendar"; import type { CalendarEvent, Person } from "@calcom/types/Calendar";
enum BookingAction {
Create = "create",
Cancel = "cancel",
Reschedule = "reschedule",
RequestReschedule = "request_reschedule",
}
const generateIcsString = ({ const generateIcsString = ({
event, event,
t, t,
role, role,
status, bookingAction,
}: { }: {
event: CalendarEvent; event: CalendarEvent;
t: TFunction; t: TFunction;
role: "attendee" | "organizer"; role: "attendee" | "organizer";
status: EventStatus; bookingAction: BookingAction;
}) => { }) => {
let title: string, subtitle: string, status: EventStatus;
// Taking care of recurrence rule // Taking care of recurrence rule
let recurrenceRule: string | undefined = undefined; let recurrenceRule: string | undefined = undefined;
const partstat: ParticipationStatus = "ACCEPTED"; const partstat: ParticipationStatus = "ACCEPTED";
@ -27,24 +36,55 @@ const generateIcsString = ({
recurrenceRule = new RRule(event.recurringEvent).toString().replace("RRULE:", ""); recurrenceRule = new RRule(event.recurringEvent).toString().replace("RRULE:", "");
} }
const getTextBody = (title = "", subtitle = "emailed_you_and_any_other_attendees"): string => { switch (bookingAction) {
if (!title) { case BookingAction.Create:
if (role === "organizer") { if (role === "organizer") {
title = event.recurringEvent?.count ? "new_event_scheduled_recurring" : "new_event_scheduled"; title = event.recurringEvent?.count ? "new_event_scheduled_recurring" : "new_event_scheduled";
} } else if (role === "attendee") {
if (role === "attendee") {
title = event.recurringEvent?.count title = event.recurringEvent?.count
? "your_event_has_been_scheduled_recurring" ? "your_event_has_been_scheduled_recurring"
: "your_event_has_been_scheduled"; : "your_event_has_been_scheduled";
} }
status = "CONFIRMED";
break;
case BookingAction.Cancel:
title = "event_request_cancelled";
status = "CANCELLED";
break;
case BookingAction.Reschedule:
title = "event_has_been_rescheduled";
status = "CONFIRMED";
break;
case BookingAction.RequestReschedule:
if (role === "organizer") {
title = t("request_reschedule_title_organizer", {
attendee: event.attendees[0].name,
});
subtitle = t("request_reschedule_subtitle_organizer", {
attendee: event.attendees[0].name,
});
} else if (role === "attendee") {
title = "request_reschedule_booking";
subtitle = t("request_reschedule_subtitle", {
organizer: event.organizer.name,
});
}
status = "CANCELLED";
} }
const getTextBody = (title = "", subtitle = "emailed_you_and_any_other_attendees"): string => {
if (BookingAction.RequestReschedule && role === "attendee") {
return ` return `
${t(title)} ${t(title)}
${t(subtitle)} ${getWhen(event, t)}
${t(subtitle)}`;
}
return `
${t(title)}
${t(subtitle)}
${getRichDescription(event, t)} ${getRichDescription(event, t)}
`.trim(); `.trim();
}; };
const icsEvent = createEvent({ const icsEvent = createEvent({
@ -58,7 +98,7 @@ ${getRichDescription(event, t)}
startInputType: "utc", startInputType: "utc",
productId: "calcom/ics", productId: "calcom/ics",
title: event.title, title: event.title,
description: getTextBody(), description: getTextBody(title, subtitle),
duration: { minutes: dayjs(event.endTime).diff(dayjs(event.startTime), "minute") }, duration: { minutes: dayjs(event.endTime).diff(dayjs(event.startTime), "minute") },
organizer: { name: event.organizer.name, email: event.organizer.email }, organizer: { name: event.organizer.name, email: event.organizer.email },
...{ recurrenceRule }, ...{ recurrenceRule },