import dayjs, { Dayjs } from "dayjs"; import localizedFormat from "dayjs/plugin/localizedFormat"; import timezone from "dayjs/plugin/timezone"; import toArray from "dayjs/plugin/toArray"; import utc from "dayjs/plugin/utc"; import { createEvent, DateArray } from "ics"; import nodemailer from "nodemailer"; import { getAppName } from "@calcom/app-store/utils"; import { getCancelLink, getRichDescription } from "@calcom/lib/CalEventParser"; import { getErrorFromUnknown } from "@calcom/lib/errors"; import type { Person, CalendarEvent } from "@calcom/types/Calendar"; import { serverConfig } from "@lib/serverConfig"; import { emailHead, emailSchedulingBodyHeader, emailBodyLogo, emailScheduledBodyHeaderContent, emailSchedulingBodyDivider, linkIcon, } from "./common"; dayjs.extend(utc); dayjs.extend(timezone); dayjs.extend(localizedFormat); dayjs.extend(toArray); export default class AttendeeScheduledEmail { calEvent: CalendarEvent; attendee: Person; constructor(calEvent: CalendarEvent, attendee: Person) { this.calEvent = calEvent; this.attendee = attendee; } public sendEmail() { new Promise((resolve, reject) => nodemailer .createTransport(this.getMailerOptions().transport) .sendMail(this.getNodeMailerPayload(), (_err, info) => { if (_err) { const err = getErrorFromUnknown(_err); this.printNodeMailerError(err); reject(err); } else { resolve(info); } }) ).catch((e) => console.error("sendEmail", e)); return new Promise((resolve) => resolve("send mail async")); } protected getiCalEventAsString(): string | undefined { 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.calEvent.attendees[0].language.translate("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 }, 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 { return { icalEvent: { filename: "event.ics", content: this.getiCalEventAsString(), }, to: `${this.attendee.name} <${this.attendee.email}>`, from: `${this.calEvent.organizer.name} <${this.getMailerOptions().from}>`, replyTo: this.calEvent.organizer.email, subject: `${this.calEvent.attendees[0].language.translate("confirmed_event_type_subject", { eventType: this.calEvent.type, name: this.calEvent.team?.name || this.calEvent.organizer.name, date: `${this.getInviteeStart().format("h:mma")} - ${this.getInviteeEnd().format( "h:mma" )}, ${this.calEvent.attendees[0].language.translate( this.getInviteeStart().format("dddd").toLowerCase() )}, ${this.calEvent.attendees[0].language.translate( this.getInviteeStart().format("MMMM").toLowerCase() )} ${this.getInviteeStart().format("D")}, ${this.getInviteeStart().format("YYYY")}`, })}`, html: this.getHtmlBody(), text: this.getTextBody(), }; } protected getMailerOptions() { return { transport: serverConfig.transport, from: serverConfig.from, }; } protected getTextBody(): string { return ` ${this.calEvent.attendees[0].language.translate("your_event_has_been_scheduled")} ${this.calEvent.attendees[0].language.translate("emailed_you_and_any_other_attendees")} ${getRichDescription(this.calEvent)} `.trim(); } protected printNodeMailerError(error: Error): void { console.error("SEND_BOOKING_CONFIRMATION_ERROR", this.attendee.email, error); } protected getHtmlBody(): string { const headerContent = this.calEvent.attendees[0].language.translate("confirmed_event_type_subject", { eventType: this.calEvent.type, name: this.calEvent.team?.name || this.calEvent.organizer.name, date: `${this.getInviteeStart().format("h:mma")} - ${this.getInviteeEnd().format( "h:mma" )}, ${this.calEvent.attendees[0].language.translate( this.getInviteeStart().format("dddd").toLowerCase() )}, ${this.calEvent.attendees[0].language.translate( this.getInviteeStart().format("MMMM").toLowerCase() )} ${this.getInviteeStart().format("D")}, ${this.getInviteeStart().format("YYYY")}`, }); return ` ${emailHead(headerContent)}
${emailSchedulingBodyHeader("checkCircle")} ${emailScheduledBodyHeaderContent( this.calEvent.attendees[0].language.translate("your_event_has_been_scheduled"), this.calEvent.attendees[0].language.translate("emailed_you_and_any_other_attendees") )} ${emailSchedulingBodyDivider()}
${this.getWhat()} ${this.getWhen()} ${this.getWho()} ${this.getLocation()} ${this.getAdditionalNotes()}
${emailSchedulingBodyDivider()}
${this.getManageLink()}
${emailBodyLogo()}
`; } protected getManageLink(): string { // Only the original attendee can make changes to the event // Guests cannot if (this.attendee === this.calEvent.attendees[0]) { const manageText = this.calEvent.attendees[0].language.translate("manage_this_event"); return `

${this.calEvent.attendees[0].language.translate( "need_to_reschedule_or_cancel" )}

${manageText}

`; } return ""; } protected getWhat(): string { return `

${this.calEvent.attendees[0].language.translate("what")}

${this.calEvent.type}

`; } protected getWhen(): string { return `

${this.calEvent.attendees[0].language.translate("when")}

${this.calEvent.attendees[0].language.translate( this.getInviteeStart().format("dddd").toLowerCase() )}, ${this.calEvent.attendees[0].language.translate( this.getInviteeStart().format("MMMM").toLowerCase() )} ${this.getInviteeStart().format("D")}, ${this.getInviteeStart().format( "YYYY" )} | ${this.getInviteeStart().format("h:mma")} - ${this.getInviteeEnd().format( "h:mma" )} (${this.getTimezone()})

`; } protected getWho(): string { const attendees = this.calEvent.attendees .map((attendee) => { return `
${ attendee?.name || `${this.calEvent.attendees[0].language.translate("guest")}` } ${ attendee.email }
`; }) .join(""); const organizer = `
${ this.calEvent.organizer.name } - ${this.calEvent.attendees[0].language.translate( "organizer" )} ${this.calEvent.organizer.email}
`; return `

${this.calEvent.attendees[0].language.translate("who")}

${organizer + attendees}
`; } protected getAdditionalNotes(): string { if (!this.calEvent.description) return ""; return `

${this.calEvent.attendees[0].language.translate("additional_notes")}

${ this.calEvent.description }

`; } protected getRejectionReason(): string { if (!this.calEvent.rejectionReason) return ""; return `

${this.calEvent.attendees[0].language.translate("rejection_reason")}

${this.calEvent.rejectionReason}

`; } protected getLocation(): string { let providerName = this.calEvent.location ? getAppName(this.calEvent.location) : ""; if (this.calEvent.location && this.calEvent.location.includes("integrations:")) { const location = this.calEvent.location.split(":")[1]; providerName = location[0].toUpperCase() + location.slice(1); } if (this.calEvent.videoCallData) { const meetingId = this.calEvent.videoCallData.id; const meetingPassword = this.calEvent.videoCallData.password; const meetingUrl = this.calEvent.videoCallData.url; return `

${this.calEvent.attendees[0].language.translate("where")}

${providerName} ${ meetingUrl && `` }

${ meetingId && `
${this.calEvent.attendees[0].language.translate( "meeting_id" )}: ${meetingId}
` } ${ meetingPassword && `
${this.calEvent.attendees[0].language.translate( "meeting_password" )}: ${meetingPassword}
` } ${ meetingUrl && `
${this.calEvent.attendees[0].language.translate( "meeting_url" )}: ${meetingUrl}
` }
`; } if (this.calEvent.additionInformation?.hangoutLink) { const hangoutLink: string = this.calEvent.additionInformation.hangoutLink; return `

${this.calEvent.attendees[0].language.translate("where")}

${providerName} ${ hangoutLink && `` }

${hangoutLink}
`; } return `

${this.calEvent.attendees[0].language.translate("where")}

${ providerName || this.calEvent.location }

`; } 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(): Dayjs { return dayjs(this.calEvent.startTime).tz(this.getTimezone()); } protected getInviteeEnd(): Dayjs { return dayjs(this.calEvent.endTime).tz(this.getTimezone()); } }