2021-06-29 21:43:18 +00:00
|
|
|
import { createEvent } from "ics";
|
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2021-06-16 22:26:51 +00:00
|
|
|
import EventMail from "./EventMail";
|
2021-06-16 21:40:13 +00:00
|
|
|
|
2021-06-29 21:43:18 +00:00
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import toArray from "dayjs/plugin/toArray";
|
|
|
|
import localizedFormat from "dayjs/plugin/localizedFormat";
|
|
|
|
import { stripHtml } from "./helpers";
|
|
|
|
|
2021-06-21 19:30:00 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
dayjs.extend(toArray);
|
|
|
|
dayjs.extend(localizedFormat);
|
|
|
|
|
2021-06-18 00:44:41 +00:00
|
|
|
export default class EventOrganizerMail extends EventMail {
|
2021-06-16 21:40:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the instance's event as an iCal event in string representation.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getiCalEventAsString(): string {
|
|
|
|
const icsEvent = createEvent({
|
2021-06-29 21:43:18 +00:00
|
|
|
start: dayjs(this.calEvent.startTime)
|
|
|
|
.utc()
|
|
|
|
.toArray()
|
|
|
|
.slice(0, 6)
|
|
|
|
.map((v, i) => (i === 1 ? v + 1 : v)),
|
|
|
|
startInputType: "utc",
|
|
|
|
productId: "calendso/ics",
|
2021-06-16 21:40:13 +00:00
|
|
|
title: `${this.calEvent.type} with ${this.calEvent.attendees[0].name}`,
|
2021-06-29 21:43:18 +00:00
|
|
|
description:
|
|
|
|
this.calEvent.description +
|
|
|
|
stripHtml(this.getAdditionalBody()) +
|
|
|
|
stripHtml(this.getAdditionalFooter()),
|
|
|
|
duration: { minutes: dayjs(this.calEvent.endTime).diff(dayjs(this.calEvent.startTime), "minute") },
|
2021-06-20 14:37:51 +00:00
|
|
|
organizer: { name: this.calEvent.organizer.name, email: this.calEvent.organizer.email },
|
2021-07-07 10:43:13 +00:00
|
|
|
attendees: this.calEvent.attendees.map((attendee: unknown) => ({
|
2021-06-29 21:43:18 +00:00
|
|
|
name: attendee.name,
|
|
|
|
email: attendee.email,
|
|
|
|
})),
|
2021-06-16 21:40:13 +00:00
|
|
|
status: "CONFIRMED",
|
|
|
|
});
|
|
|
|
if (icsEvent.error) {
|
|
|
|
throw icsEvent.error;
|
|
|
|
}
|
|
|
|
return icsEvent.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the email text as HTML representation.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getHtmlRepresentation(): string {
|
2021-06-29 21:43:18 +00:00
|
|
|
return (
|
|
|
|
`
|
2021-06-16 21:40:13 +00:00
|
|
|
<div>
|
|
|
|
Hi ${this.calEvent.organizer.name},<br />
|
|
|
|
<br />
|
|
|
|
A new event has been scheduled.<br />
|
|
|
|
<br />
|
|
|
|
<strong>Event Type:</strong><br />
|
|
|
|
${this.calEvent.type}<br />
|
|
|
|
<br />
|
|
|
|
<strong>Invitee Email:</strong><br />
|
|
|
|
<a href="mailto:${this.calEvent.attendees[0].email}">${this.calEvent.attendees[0].email}</a><br />
|
2021-06-29 21:43:18 +00:00
|
|
|
<br />` +
|
|
|
|
this.getAdditionalBody() +
|
2021-07-13 12:38:54 +00:00
|
|
|
"<br />" +
|
2021-06-16 21:40:13 +00:00
|
|
|
`<strong>Invitee Time Zone:</strong><br />
|
2021-06-29 00:30:45 +00:00
|
|
|
${this.calEvent.attendees[0].timeZone}<br />
|
|
|
|
<br />
|
|
|
|
<strong>Additional notes:</strong><br />
|
|
|
|
${this.calEvent.description}
|
|
|
|
` +
|
2021-06-29 21:43:18 +00:00
|
|
|
this.getAdditionalFooter() +
|
2021-07-07 10:43:13 +00:00
|
|
|
`
|
2021-06-16 21:40:13 +00:00
|
|
|
</div>
|
2021-06-29 21:43:18 +00:00
|
|
|
`
|
|
|
|
);
|
2021-06-16 21:40:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
/**
|
|
|
|
* Adds the video call information to the mail body.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getLocation(): string {
|
|
|
|
if (this.additionInformation?.hangoutLink) {
|
|
|
|
return `<strong>Location:</strong> <a href="${this.additionInformation?.hangoutLink}">${this.additionInformation?.hangoutLink}</a><br />`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.additionInformation?.entryPoints && this.additionInformation?.entryPoints.length > 0) {
|
|
|
|
const locations = this.additionInformation?.entryPoints
|
|
|
|
.map((entryPoint) => {
|
|
|
|
return `
|
|
|
|
Join by ${entryPoint.entryPointType}: <br />
|
|
|
|
<a href="${entryPoint.uri}">${entryPoint.label}</a> <br />
|
|
|
|
`;
|
|
|
|
})
|
|
|
|
.join("<br />");
|
|
|
|
|
|
|
|
return `<strong>Locations:</strong><br /> ${locations}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.calEvent.location ? `<strong>Location:</strong> ${this.calEvent.location}<br /><br />` : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getAdditionalBody(): string {
|
|
|
|
return `
|
|
|
|
${this.getLocation()}
|
|
|
|
`;
|
|
|
|
}
|
2021-06-16 21:40:13 +00:00
|
|
|
/**
|
2021-06-16 22:26:51 +00:00
|
|
|
* Returns the payload object for the nodemailer.
|
2021-06-16 21:40:13 +00:00
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
2021-06-29 21:43:18 +00:00
|
|
|
protected getNodeMailerPayload(): Record<string, unknown> {
|
2021-06-16 21:40:13 +00:00
|
|
|
const organizerStart: Dayjs = <Dayjs>dayjs(this.calEvent.startTime).tz(this.calEvent.organizer.timeZone);
|
|
|
|
|
|
|
|
return {
|
2021-06-16 22:26:51 +00:00
|
|
|
icalEvent: {
|
2021-06-29 21:43:18 +00:00
|
|
|
filename: "event.ics",
|
2021-06-16 22:26:51 +00:00
|
|
|
content: this.getiCalEventAsString(),
|
|
|
|
},
|
|
|
|
from: `Calendso <${this.getMailerOptions().from}>`,
|
|
|
|
to: this.calEvent.organizer.email,
|
2021-06-29 21:43:18 +00:00
|
|
|
subject: `New event: ${this.calEvent.attendees[0].name} - ${organizerStart.format("LT dddd, LL")} - ${
|
|
|
|
this.calEvent.type
|
|
|
|
}`,
|
2021-06-16 22:26:51 +00:00
|
|
|
html: this.getHtmlRepresentation(),
|
|
|
|
text: this.getPlainTextRepresentation(),
|
2021-06-16 21:40:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-16 22:26:51 +00:00
|
|
|
protected printNodeMailerError(error: string): void {
|
|
|
|
console.error("SEND_NEW_EVENT_NOTIFICATION_ERROR", this.calEvent.organizer.email, error);
|
2021-06-16 21:40:13 +00:00
|
|
|
}
|
2021-06-29 21:43:18 +00:00
|
|
|
}
|