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 } from "ics"; import EventMail from "./EventMail"; import { stripHtml } from "./helpers"; dayjs.extend(utc); dayjs.extend(timezone); dayjs.extend(toArray); dayjs.extend(localizedFormat); export default class EventOrganizerMail extends EventMail { /** * Returns the instance's event as an iCal event in string representation. * @protected */ protected getiCalEventAsString(): string { const icsEvent = createEvent({ start: dayjs(this.calEvent.startTime) .utc() .toArray() .slice(0, 6) .map((v, i) => (i === 1 ? v + 1 : v)), startInputType: "utc", productId: "calendso/ics", title: `${this.calEvent.type} with ${this.calEvent.attendees[0].name}`, description: this.calEvent.description + stripHtml(this.getAdditionalBody()) + stripHtml(this.getAdditionalFooter()), 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: unknown) => ({ name: attendee.name, email: attendee.email, })), status: "CONFIRMED", }); if (icsEvent.error) { throw icsEvent.error; } return icsEvent.value; } protected getBodyHeader(): string { return "A new event has been scheduled."; } protected getAdditionalFooter(): string { return `

Need to make a change? Manage my bookings

`; } protected getImage(): string { return ` `; } /** * Returns the email text as HTML representation. * * @protected */ protected getHtmlRepresentation(): string { return ( `
${this.getImage()}

${this.getBodyHeader()}


What ${this.calEvent.type}
When ${this.getOrganizerStart().format("dddd, LL")}
${this.getOrganizerStart().format("h:mma")} (${ this.calEvent.organizer.timeZone })
Who ${this.calEvent.attendees[0].name}
${this.calEvent.attendees[0].email}
Where ${this.getLocation()}
Notes ${this.calEvent.description}
` + this.getAdditionalBody() + "
" + `
` + this.getAdditionalFooter() + `
Cal.com Logo
` ); } /** * Adds the video call information to the mail body. * * @protected */ protected getLocation(): string { if (this.additionInformation?.hangoutLink) { return `${this.additionInformation?.hangoutLink}
`; } if (this.additionInformation?.entryPoints && this.additionInformation?.entryPoints.length > 0) { const locations = this.additionInformation?.entryPoints .map((entryPoint) => { return ` Join by ${entryPoint.entryPointType}:
${entryPoint.label}
`; }) .join("
"); return `${locations}`; } return this.calEvent.location ? `${this.calEvent.location}

` : ""; } protected getAdditionalBody(): string { return ``; } /** * Returns the payload object for the nodemailer. * * @protected */ 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.getSubject(), html: this.getHtmlRepresentation(), text: this.getPlainTextRepresentation(), }; } protected getSubject(): string { return `New event: ${this.calEvent.attendees[0].name} - ${this.getOrganizerStart().format( "LT dddd, LL" )} - ${this.calEvent.type}`; } protected printNodeMailerError(error: string): void { console.error("SEND_NEW_EVENT_NOTIFICATION_ERROR", this.calEvent.organizer.email, error); } /** * Returns the organizerStart value used at multiple points. * * @private */ protected getOrganizerStart(): Dayjs { return dayjs(this.calEvent.startTime).tz(this.calEvent.organizer.timeZone); } }