2022-06-06 17:49:56 +00:00
|
|
|
import { createEvent, DateArray, Person } from "ics";
|
|
|
|
import { TFunction } from "next-i18next";
|
|
|
|
import rrule from "rrule";
|
|
|
|
|
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-06-10 00:32:34 +00:00
|
|
|
import type { CalendarEvent } 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;
|
2022-06-06 17:49:56 +00:00
|
|
|
|
2022-07-01 19:19:23 +00:00
|
|
|
constructor(calEvent: CalendarEvent, newSeat?: boolean) {
|
2022-06-06 17:49:56 +00:00
|
|
|
super();
|
|
|
|
this.name = "SEND_BOOKING_CONFIRMATION";
|
|
|
|
this.calEvent = calEvent;
|
|
|
|
this.t = this.calEvent.organizer.language.translate;
|
2022-07-01 19:19:23 +00:00
|
|
|
this.newSeat = newSeat;
|
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
|
|
|
|
recurrenceRule = new rrule(this.calEvent.recurringEvent).toString().replace("RRULE:", "");
|
2022-06-06 17:49:56 +00:00
|
|
|
}
|
|
|
|
const icsEvent = createEvent({
|
|
|
|
start: dayjs(this.calEvent.startTime)
|
|
|
|
.utc()
|
|
|
|
.toArray()
|
|
|
|
.slice(0, 6)
|
|
|
|
.map((v, i) => (i === 1 ? v + 1 : v)) as DateArray,
|
|
|
|
startInputType: "utc",
|
|
|
|
productId: "calendso/ics",
|
|
|
|
title: this.t("ics_event_title", {
|
|
|
|
eventType: this.calEvent.type,
|
|
|
|
name: this.calEvent.attendees[0].name,
|
|
|
|
}),
|
|
|
|
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 },
|
|
|
|
attendees: this.calEvent.attendees.map((attendee: Person) => ({
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
})),
|
|
|
|
status: "CONFIRMED",
|
|
|
|
});
|
|
|
|
if (icsEvent.error) {
|
|
|
|
throw icsEvent.error;
|
|
|
|
}
|
|
|
|
return icsEvent.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getNodeMailerPayload(): Record<string, unknown> {
|
|
|
|
const toAddresses = [this.calEvent.organizer.email];
|
|
|
|
if (this.calEvent.team) {
|
|
|
|
this.calEvent.team.members.forEach((member) => {
|
|
|
|
const memberAttendee = this.calEvent.attendees.find((attendee) => attendee.name === member);
|
|
|
|
if (memberAttendee) {
|
|
|
|
toAddresses.push(memberAttendee.email);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-01 19:19:23 +00:00
|
|
|
let subject;
|
|
|
|
if (this.newSeat) {
|
|
|
|
subject = "new_seat_subject";
|
|
|
|
} else {
|
|
|
|
subject = "confirmed_event_type_subject";
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:49:56 +00:00
|
|
|
return {
|
|
|
|
icalEvent: {
|
|
|
|
filename: "event.ics",
|
|
|
|
content: this.getiCalEventAsString(),
|
|
|
|
},
|
|
|
|
from: `Cal.com <${this.getMailerOptions().from}>`,
|
|
|
|
to: toAddresses.join(","),
|
2022-07-01 19:19:23 +00:00
|
|
|
subject: `${this.t(subject, {
|
2022-06-06 17:49:56 +00:00
|
|
|
eventType: this.calEvent.type,
|
|
|
|
name: this.calEvent.attendees[0].name,
|
|
|
|
date: this.getFormattedDate(),
|
|
|
|
})}`,
|
|
|
|
html: renderEmail("OrganizerScheduledEmail", {
|
|
|
|
calEvent: this.calEvent,
|
|
|
|
attendee: this.calEvent.organizer,
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getOrganizerStart(format: string) {
|
|
|
|
return this.getRecipientTime(this.calEvent.startTime, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getOrganizerEnd(format: string) {
|
|
|
|
return this.getRecipientTime(this.calEvent.endTime, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getFormattedDate() {
|
|
|
|
return `${this.getOrganizerStart("h:mma")} - ${this.getOrganizerEnd("h:mma")}, ${this.t(
|
|
|
|
this.getOrganizerStart("dddd").toLowerCase()
|
|
|
|
)}, ${this.t(this.getOrganizerStart("MMMM").toLowerCase())} ${this.getOrganizerStart("D, YYYY")}`;
|
|
|
|
}
|
|
|
|
}
|