2022-06-06 17:49:56 +00:00
|
|
|
import { createEvent, DateArray } from "ics";
|
|
|
|
import { 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-06-10 00:32:34 +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 AttendeeScheduledEmail extends BaseEmail {
|
|
|
|
calEvent: CalendarEvent;
|
|
|
|
attendee: Person;
|
2022-10-18 19:41:50 +00:00
|
|
|
showAttendees: boolean | undefined;
|
2022-06-06 17:49:56 +00:00
|
|
|
t: TFunction;
|
|
|
|
|
2022-10-18 19:41:50 +00:00
|
|
|
constructor(calEvent: CalendarEvent, attendee: Person, showAttendees?: boolean | undefined) {
|
2022-06-06 17:49:56 +00:00
|
|
|
super();
|
|
|
|
this.name = "SEND_BOOKING_CONFIRMATION";
|
|
|
|
this.calEvent = calEvent;
|
|
|
|
this.attendee = attendee;
|
2022-10-18 19:41:50 +00:00
|
|
|
this.showAttendees = showAttendees;
|
2022-06-06 17:49:56 +00:00
|
|
|
this.t = attendee.language.translate;
|
2023-01-31 21:14:19 +00:00
|
|
|
|
|
|
|
if (!this.showAttendees) {
|
|
|
|
this.calEvent.attendees = [this.attendee];
|
|
|
|
}
|
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({
|
|
|
|
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",
|
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 },
|
|
|
|
attendees: this.calEvent.attendees.map((attendee: Person) => ({
|
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
})),
|
|
|
|
...{ recurrenceRule },
|
|
|
|
status: "CONFIRMED",
|
|
|
|
});
|
|
|
|
if (icsEvent.error) {
|
|
|
|
throw icsEvent.error;
|
|
|
|
}
|
|
|
|
return icsEvent.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getNodeMailerPayload(): Record<string, unknown> {
|
|
|
|
return {
|
|
|
|
icalEvent: {
|
|
|
|
filename: "event.ics",
|
|
|
|
content: this.getiCalEventAsString(),
|
|
|
|
},
|
|
|
|
to: `${this.attendee.name} <${this.attendee.email}>`,
|
|
|
|
from: `${this.calEvent.organizer.name} <${this.getMailerOptions().from}>`,
|
2022-08-26 18:51:32 +00:00
|
|
|
replyTo: [...this.calEvent.attendees.map(({ email }) => email), this.calEvent.organizer.email],
|
2023-01-25 09:44:48 +00:00
|
|
|
subject: decodeURIComponent(`${this.calEvent.title}`),
|
2022-09-27 19:48:35 +00:00
|
|
|
html: renderEmail("AttendeeScheduledEmail", {
|
2022-06-06 17:49:56 +00:00
|
|
|
calEvent: this.calEvent,
|
|
|
|
attendee: this.attendee,
|
|
|
|
}),
|
|
|
|
text: this.getTextBody(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getTextBody(title = "", subtitle = "emailed_you_and_any_other_attendees"): string {
|
|
|
|
return `
|
|
|
|
${this.t(
|
2022-06-10 00:32:34 +00:00
|
|
|
title || this.calEvent.recurringEvent?.count
|
2022-06-06 17:49:56 +00:00
|
|
|
? "your_event_has_been_scheduled_recurring"
|
|
|
|
: "your_event_has_been_scheduled"
|
|
|
|
)}
|
|
|
|
${this.t(subtitle)}
|
|
|
|
|
|
|
|
${getRichDescription(this.calEvent)}
|
|
|
|
`.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getTimezone(): string {
|
|
|
|
// Timezone is based on the first attendee in the attendee list
|
|
|
|
// as the first attendee is the one who created the booking
|
|
|
|
return this.calEvent.attendees[0].timeZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getInviteeStart(format: string) {
|
|
|
|
return this.getRecipientTime(this.calEvent.startTime, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getInviteeEnd(format: string) {
|
|
|
|
return this.getRecipientTime(this.calEvent.endTime, format);
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:49:44 +00:00
|
|
|
public getFormattedDate() {
|
2022-06-06 17:49:56 +00:00
|
|
|
return `${this.getInviteeStart("h:mma")} - ${this.getInviteeEnd("h:mma")}, ${this.t(
|
|
|
|
this.getInviteeStart("dddd").toLowerCase()
|
|
|
|
)}, ${this.t(this.getInviteeStart("MMMM").toLowerCase())} ${this.getInviteeStart("D, YYYY")}`;
|
|
|
|
}
|
|
|
|
}
|