2023-02-27 20:45:40 +00:00
|
|
|
import type { DateArray } from "ics";
|
|
|
|
import { createEvent } from "ics";
|
2023-03-14 04:19:05 +00:00
|
|
|
import { cloneDeep } from "lodash";
|
2023-02-27 20:45:40 +00:00
|
|
|
import type { TFunction } from "next-i18next";
|
2022-08-17 17:38:21 +00:00
|
|
|
import { RRule } from "rrule";
|
2022-06-06 17:49:56 +00:00
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-06-06 17:49:56 +00:00
|
|
|
import { getRichDescription } from "@calcom/lib/CalEventParser";
|
2022-11-30 21:52:56 +00:00
|
|
|
import { APP_NAME } from "@calcom/lib/constants";
|
2023-07-28 14:22:19 +00:00
|
|
|
import { TimeFormat } from "@calcom/lib/timeFormat";
|
2023-02-27 20:45:40 +00:00
|
|
|
import type { CalendarEvent, Person } from "@calcom/types/Calendar";
|
2022-06-06 17:49:56 +00:00
|
|
|
|
|
|
|
import { renderEmail } from "../";
|
|
|
|
import BaseEmail from "./_base-email";
|
|
|
|
|
|
|
|
export default class OrganizerScheduledEmail extends BaseEmail {
|
|
|
|
calEvent: CalendarEvent;
|
|
|
|
t: TFunction;
|
2022-07-01 19:19:23 +00:00
|
|
|
newSeat?: boolean;
|
2023-02-27 20:45:40 +00:00
|
|
|
teamMember?: Person;
|
2022-06-06 17:49:56 +00:00
|
|
|
|
2023-02-27 20:45:40 +00:00
|
|
|
constructor(input: { calEvent: CalendarEvent; newSeat?: boolean; teamMember?: Person }) {
|
2022-06-06 17:49:56 +00:00
|
|
|
super();
|
|
|
|
this.name = "SEND_BOOKING_CONFIRMATION";
|
2023-02-27 20:45:40 +00:00
|
|
|
this.calEvent = input.calEvent;
|
2022-06-06 17:49:56 +00:00
|
|
|
this.t = this.calEvent.organizer.language.translate;
|
2023-02-27 20:45:40 +00:00
|
|
|
this.newSeat = input.newSeat;
|
|
|
|
this.teamMember = input.teamMember;
|
2022-06-06 17:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected getiCalEventAsString(): string | undefined {
|
2022-06-10 20:38:06 +00:00
|
|
|
// Taking care of recurrence rule
|
2022-06-06 17:49:56 +00:00
|
|
|
let recurrenceRule: string | undefined = undefined;
|
2022-06-10 00:32:34 +00:00
|
|
|
if (this.calEvent.recurringEvent?.count) {
|
|
|
|
// ics appends "RRULE:" already, so removing it from RRule generated string
|
2022-08-17 17:38:21 +00:00
|
|
|
recurrenceRule = new RRule(this.calEvent.recurringEvent).toString().replace("RRULE:", "");
|
2022-06-06 17:49:56 +00:00
|
|
|
}
|
|
|
|
const icsEvent = createEvent({
|
2023-04-03 17:13:57 +00:00
|
|
|
uid: this.calEvent.iCalUID || this.calEvent.uid!,
|
2022-06-06 17:49:56 +00:00
|
|
|
start: dayjs(this.calEvent.startTime)
|
|
|
|
.utc()
|
|
|
|
.toArray()
|
|
|
|
.slice(0, 6)
|
|
|
|
.map((v, i) => (i === 1 ? v + 1 : v)) as DateArray,
|
|
|
|
startInputType: "utc",
|
2023-04-03 17:13:57 +00:00
|
|
|
productId: "calcom/ics",
|
2023-01-10 18:32:50 +00:00
|
|
|
title: this.calEvent.title,
|
2022-06-06 17:49:56 +00:00
|
|
|
description: this.getTextBody(),
|
|
|
|
duration: { minutes: dayjs(this.calEvent.endTime).diff(dayjs(this.calEvent.startTime), "minute") },
|
|
|
|
organizer: { name: this.calEvent.organizer.name, email: this.calEvent.organizer.email },
|
|
|
|
...{ recurrenceRule },
|
2023-02-27 20:45:40 +00:00
|
|
|
attendees: [
|
|
|
|
...this.calEvent.attendees.map((attendee: Person) => ({
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
})),
|
|
|
|
...(this.calEvent.team?.members
|
|
|
|
? this.calEvent.team?.members.map((member: Person) => ({
|
|
|
|
name: member.name,
|
|
|
|
email: member.email,
|
|
|
|
}))
|
|
|
|
: []),
|
|
|
|
],
|
2022-06-06 17:49:56 +00:00
|
|
|
status: "CONFIRMED",
|
|
|
|
});
|
|
|
|
if (icsEvent.error) {
|
|
|
|
throw icsEvent.error;
|
|
|
|
}
|
|
|
|
return icsEvent.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getNodeMailerPayload(): Record<string, unknown> {
|
2023-03-14 04:19:05 +00:00
|
|
|
const clonedCalEvent = cloneDeep(this.calEvent);
|
2023-07-20 23:29:08 +00:00
|
|
|
const toAddresses = [this.teamMember?.email || this.calEvent.organizer.email];
|
2022-06-06 17:49:56 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
icalEvent: {
|
|
|
|
filename: "event.ics",
|
|
|
|
content: this.getiCalEventAsString(),
|
|
|
|
},
|
2022-11-30 21:52:56 +00:00
|
|
|
from: `${APP_NAME} <${this.getMailerOptions().from}>`,
|
2022-06-06 17:49:56 +00:00
|
|
|
to: toAddresses.join(","),
|
2023-03-03 16:35:11 +00:00
|
|
|
subject: `${this.newSeat ? this.t("new_attendee") + ":" : ""} ${this.calEvent.title}`,
|
2022-06-06 17:49:56 +00:00
|
|
|
html: renderEmail("OrganizerScheduledEmail", {
|
2023-03-14 04:19:05 +00:00
|
|
|
calEvent: clonedCalEvent,
|
2022-06-06 17:49:56 +00:00
|
|
|
attendee: this.calEvent.organizer,
|
2023-02-27 20:45:40 +00:00
|
|
|
teamMember: this.teamMember,
|
2022-07-01 19:19:23 +00:00
|
|
|
newSeat: this.newSeat,
|
2022-06-06 17:49:56 +00:00
|
|
|
}),
|
|
|
|
text: this.getTextBody(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getTextBody(
|
|
|
|
title = "",
|
|
|
|
subtitle = "emailed_you_and_any_other_attendees",
|
|
|
|
extraInfo = "",
|
|
|
|
callToAction = ""
|
|
|
|
): string {
|
|
|
|
return `
|
2022-06-10 00:32:34 +00:00
|
|
|
${this.t(
|
|
|
|
title || this.calEvent.recurringEvent?.count ? "new_event_scheduled_recurring" : "new_event_scheduled"
|
|
|
|
)}
|
2022-06-06 17:49:56 +00:00
|
|
|
${this.t(subtitle)}
|
|
|
|
${extraInfo}
|
|
|
|
${getRichDescription(this.calEvent)}
|
|
|
|
${callToAction}
|
|
|
|
`.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getTimezone(): string {
|
|
|
|
return this.calEvent.organizer.timeZone;
|
|
|
|
}
|
|
|
|
|
2023-07-28 14:22:19 +00:00
|
|
|
protected getLocale(): string {
|
|
|
|
return this.calEvent.organizer.language.locale;
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:49:56 +00:00
|
|
|
protected getOrganizerStart(format: string) {
|
2023-07-28 14:22:19 +00:00
|
|
|
return this.getFormattedRecipientTime({
|
|
|
|
time: this.calEvent.startTime,
|
|
|
|
format,
|
|
|
|
});
|
2022-06-06 17:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected getOrganizerEnd(format: string) {
|
2023-07-28 14:22:19 +00:00
|
|
|
return this.getFormattedRecipientTime({
|
|
|
|
time: this.calEvent.endTime,
|
|
|
|
format,
|
|
|
|
});
|
2022-06-06 17:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected getFormattedDate() {
|
2023-07-28 14:22:19 +00:00
|
|
|
const organizerTimeFormat = this.calEvent.organizer.timeFormat || TimeFormat.TWELVE_HOUR;
|
|
|
|
return `${this.getOrganizerStart(organizerTimeFormat)} - ${this.getOrganizerEnd(
|
|
|
|
organizerTimeFormat
|
|
|
|
)}, ${this.t(this.getOrganizerStart("dddd").toLowerCase())}, ${this.t(
|
|
|
|
this.getOrganizerStart("MMMM").toLowerCase()
|
|
|
|
)} ${this.getOrganizerStart("D, YYYY")}`;
|
2022-06-06 17:49:56 +00:00
|
|
|
}
|
|
|
|
}
|