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 { getCancelLink } from "@lib/CalEventParser"; import { CalendarEvent, Person } from "@lib/calendarClient"; import { getErrorFromUnknown } from "@lib/errors"; import { getIntegrationName } from "@lib/integrations"; import { serverConfig } from "@lib/serverConfig"; import { emailHead } from "./common/head"; import { emailSchedulingBodyHeader } from "./common/scheduling-body-head"; dayjs.extend(utc); dayjs.extend(timezone); dayjs.extend(localizedFormat); dayjs.extend(toArray); export default class OrganizerScheduledEmail { calEvent: CalendarEvent; constructor(calEvent: CalendarEvent) { this.calEvent = calEvent; } 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.language("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 { 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); } }); } return { icalEvent: { filename: "event.ics", content: this.getiCalEventAsString(), }, from: `Cal.com <${this.getMailerOptions().from}>`, to: toAddresses.join(","), subject: `${this.calEvent.language("confirmed_event_type_subject", { eventType: this.calEvent.type, name: this.calEvent.attendees[0].name, date: `${this.getOrganizerStart().format("h:mma")} - ${this.getOrganizerEnd().format( "h:mma" )}, ${this.calEvent.language( this.getOrganizerStart().format("dddd").toLowerCase() )}, ${this.calEvent.language( this.getOrganizerStart().format("MMMM").toLowerCase() )} ${this.getOrganizerStart().format("D")}, ${this.getOrganizerStart().format("YYYY")}`, })}`, html: this.getHtmlBody(), text: this.getTextBody(), }; } protected getMailerOptions() { return { transport: serverConfig.transport, from: serverConfig.from, }; } protected getTextBody(): string { return ` ${this.calEvent.language("new_event_scheduled")} ${this.calEvent.language("emailed_you_and_any_other_attendees")} ${this.getWhat()} ${this.getWhen()} ${this.getLocation()} ${this.getAdditionalNotes()} ${this.calEvent.language("need_to_reschedule_or_cancel")} ${getCancelLink(this.calEvent)} `.replace(/(<([^>]+)>)/gi, ""); } protected printNodeMailerError(error: Error): void { console.error("SEND_BOOKING_CONFIRMATION_ERROR", this.calEvent.organizer.email, error); } protected getHtmlBody(): string { const headerContent = this.calEvent.language("confirmed_event_type_subject", { eventType: this.calEvent.type, name: this.calEvent.attendees[0].name, date: `${this.getOrganizerStart().format("h:mma")} - ${this.getOrganizerEnd().format( "h:mma" )}, ${this.calEvent.language( this.getOrganizerStart().format("dddd").toLowerCase() )}, ${this.calEvent.language( this.getOrganizerStart().format("MMMM").toLowerCase() )} ${this.getOrganizerStart().format("D")}, ${this.getOrganizerStart().format("YYYY")}`, }); return ` ${emailHead(headerContent)}
${emailSchedulingBodyHeader("checkCircle")}
${this.calEvent.language( "new_event_scheduled" )}
${this.calEvent.language( "emailed_you_and_any_other_attendees" )}

${this.getWhat()} ${this.getWhen()} ${this.getWho()} ${this.getLocation()} ${this.getAdditionalNotes()}

${this.getManageLink()}
`; } protected getManageLink(): string { const manageText = this.calEvent.language("manage_this_event"); return `

${this.calEvent.language( "need_to_reschedule_or_cancel" )}

${manageText}

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

${this.calEvent.language("what")}

${this.calEvent.type}

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

${this.calEvent.language("when")}

${this.calEvent.language( this.getOrganizerStart().format("dddd").toLowerCase() )}, ${this.calEvent.language( this.getOrganizerStart().format("MMMM").toLowerCase() )} ${this.getOrganizerStart().format("D")}, ${this.getOrganizerStart().format( "YYYY" )} | ${this.getOrganizerStart().format("h:mma")} - ${this.getOrganizerEnd().format( "h:mma" )} (${this.getTimezone()})

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

${this.calEvent.language("who")}

${organizer + attendees}
`; } protected getAdditionalNotes(): string { return `

${this.calEvent.language("additional_notes")}

${this.calEvent.description}

`; } protected getLocation(): string { let providerName = this.calEvent.location ? getIntegrationName(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.language("where")}

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

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

${this.calEvent.language("where")}

${ hangoutLink && `` }

${hangoutLink}
`; } return `

${this.calEvent.language("where")}

${providerName}

`; } protected getTimezone(): string { return this.calEvent.organizer.timeZone; } protected getOrganizerStart(): Dayjs { return dayjs(this.calEvent.startTime).tz(this.getTimezone()); } protected getOrganizerEnd(): Dayjs { return dayjs(this.calEvent.endTime).tz(this.getTimezone()); } }