attach hangouts location to invitee and organizer email notification

This commit is contained in:
femyeda 2021-06-28 19:30:45 -05:00
parent e714bd5b8e
commit 726d211f27
4 changed files with 191 additions and 82 deletions

View file

@ -111,7 +111,7 @@ interface Person {
timeZone: string; timeZone: string;
} }
interface CalendarEvent { export interface CalendarEvent {
type: string; type: string;
title: string; title: string;
startTime: string; startTime: string;
@ -123,18 +123,18 @@ interface CalendarEvent {
conferenceData?: ConferenceData; conferenceData?: ConferenceData;
} }
interface ConferenceData { export interface ConferenceData {
createRequest: any; createRequest: any;
} }
interface IntegrationCalendar { export interface IntegrationCalendar {
integration: string; integration: string;
primary: boolean; primary: boolean;
externalId: string; externalId: string;
name: string; name: string;
} }
interface CalendarApiAdapter { export interface CalendarApiAdapter {
createEvent(event: CalendarEvent): Promise<any>; createEvent(event: CalendarEvent): Promise<any>;
updateEvent(uid: string, event: CalendarEvent); updateEvent(uid: string, event: CalendarEvent);
@ -512,8 +512,22 @@ const createEvent = async (credential, calEvent: CalendarEvent): Promise<any> =>
const creationResult = credential ? await calendars([credential])[0].createEvent(calEvent) : null; const creationResult = credential ? await calendars([credential])[0].createEvent(calEvent) : null;
const organizerMail = new EventOrganizerMail(calEvent, uid); const maybeHangoutLink = creationResult?.hangoutLink;
const attendeeMail = new EventAttendeeMail(calEvent, uid); const maybeEntryPoints = creationResult?.entryPoints;
const maybeConferenceData = creationResult?.conferenceData;
const organizerMail = new EventOrganizerMail(calEvent, uid, {
hangoutLink: maybeHangoutLink,
conferenceData: maybeConferenceData,
entryPoints: maybeEntryPoints,
});
const attendeeMail = new EventAttendeeMail(calEvent, uid, {
hangoutLink: maybeHangoutLink,
conferenceData: maybeConferenceData,
entryPoints: maybeEntryPoints,
});
try { try {
await organizerMail.sendEmail(); await organizerMail.sendEmail();
} catch (e) { } catch (e) {
@ -571,12 +585,4 @@ const deleteEvent = (credential, uid: string): Promise<any> => {
return Promise.resolve({}); return Promise.resolve({});
}; };
export { export { getBusyCalendarTimes, createEvent, updateEvent, deleteEvent, listCalendars };
getBusyCalendarTimes,
createEvent,
updateEvent,
deleteEvent,
CalendarEvent,
listCalendars,
IntegrationCalendar,
};

View file

@ -1,9 +1,9 @@
import dayjs, {Dayjs} from "dayjs"; import dayjs, { Dayjs } from "dayjs";
import EventMail from "./EventMail"; import EventMail from "./EventMail";
import utc from 'dayjs/plugin/utc'; import utc from "dayjs/plugin/utc";
import timezone from 'dayjs/plugin/timezone'; import timezone from "dayjs/plugin/timezone";
import localizedFormat from 'dayjs/plugin/localizedFormat'; import localizedFormat from "dayjs/plugin/localizedFormat";
dayjs.extend(utc); dayjs.extend(utc);
dayjs.extend(timezone); dayjs.extend(timezone);
dayjs.extend(localizedFormat); dayjs.extend(localizedFormat);
@ -15,20 +15,60 @@ export default class EventAttendeeMail extends EventMail {
* @protected * @protected
*/ */
protected getHtmlRepresentation(): string { protected getHtmlRepresentation(): string {
return ` return (
`
<div> <div>
Hi ${this.calEvent.attendees[0].name},<br /> Hi ${this.calEvent.attendees[0].name},<br />
<br /> <br />
Your ${this.calEvent.type} with ${this.calEvent.organizer.name} at ${this.getInviteeStart().format('h:mma')} Your ${this.calEvent.type} with ${this.calEvent.organizer.name} at ${this.getInviteeStart().format(
(${this.calEvent.attendees[0].timeZone}) on ${this.getInviteeStart().format('dddd, LL')} is scheduled.<br /> "h:mma"
<br />` + this.getAdditionalBody() + ( )}
this.calEvent.location ? `<strong>Location:</strong> ${this.calEvent.location}<br /><br />` : '' (${this.calEvent.attendees[0].timeZone}) on ${this.getInviteeStart().format(
) + "dddd, LL"
)} is scheduled.<br />
<br />` +
this.getAdditionalBody() +
"<br />" +
`<strong>Additional notes:</strong><br /> `<strong>Additional notes:</strong><br />
${this.calEvent.description}<br /> ${this.calEvent.description}<br />
` + this.getAdditionalFooter() + ` ` +
this.getAdditionalFooter() +
`
</div> </div>
`; `
);
}
/**
* 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()}
`;
} }
/** /**
@ -36,12 +76,14 @@ export default class EventAttendeeMail extends EventMail {
* *
* @protected * @protected
*/ */
protected getNodeMailerPayload(): Object { protected getNodeMailerPayload() {
return { return {
to: `${this.calEvent.attendees[0].name} <${this.calEvent.attendees[0].email}>`, to: `${this.calEvent.attendees[0].name} <${this.calEvent.attendees[0].email}>`,
from: `${this.calEvent.organizer.name} <${this.getMailerOptions().from}>`, from: `${this.calEvent.organizer.name} <${this.getMailerOptions().from}>`,
replyTo: this.calEvent.organizer.email, replyTo: this.calEvent.organizer.email,
subject: `Confirmed: ${this.calEvent.type} with ${this.calEvent.organizer.name} on ${this.getInviteeStart().format('dddd, LL')}`, subject: `Confirmed: ${this.calEvent.type} with ${
this.calEvent.organizer.name
} on ${this.getInviteeStart().format("dddd, LL")}`,
html: this.getHtmlRepresentation(), html: this.getHtmlRepresentation(),
text: this.getPlainTextRepresentation(), text: this.getPlainTextRepresentation(),
}; };

View file

@ -1,10 +1,28 @@
import {CalendarEvent} from "../calendarClient"; import { CalendarEvent, ConferenceData } from "../calendarClient";
import {serverConfig} from "../serverConfig"; import { serverConfig } from "../serverConfig";
import nodemailer from 'nodemailer'; import nodemailer from "nodemailer";
interface EntryPoint {
entryPointType?: string;
uri?: string;
label?: string;
pin?: string;
accessCode?: string;
meetingCode?: string;
passcode?: string;
password?: string;
}
interface AdditionInformation {
conferenceData?: ConferenceData;
entryPoints?: EntryPoint[];
hangoutLink?: string;
}
export default abstract class EventMail { export default abstract class EventMail {
calEvent: CalendarEvent; calEvent: CalendarEvent;
uid: string; uid: string;
additionInformation?: AdditionInformation;
/** /**
* An EventMail always consists of a CalendarEvent * An EventMail always consists of a CalendarEvent
@ -14,9 +32,10 @@ export default abstract class EventMail {
* @param calEvent * @param calEvent
* @param uid * @param uid
*/ */
constructor(calEvent: CalendarEvent, uid: string) { constructor(calEvent: CalendarEvent, uid: string, additionInformation: AdditionInformation = null) {
this.calEvent = calEvent; this.calEvent = calEvent;
this.uid = uid; this.uid = uid;
this.additionInformation = additionInformation;
} }
/** /**
@ -43,31 +62,30 @@ export default abstract class EventMail {
* @protected * @protected
*/ */
protected stripHtml(html: string): string { protected stripHtml(html: string): string {
return html return html.replace("<br />", "\n").replace(/<[^>]+>/g, "");
.replace('<br />', "\n")
.replace(/<[^>]+>/g, '');
} }
/** /**
* Returns the payload object for the nodemailer. * Returns the payload object for the nodemailer.
* @protected * @protected
*/ */
protected abstract getNodeMailerPayload(): Object; protected abstract getNodeMailerPayload();
/** /**
* Sends the email to the event attendant and returns a Promise. * Sends the email to the event attendant and returns a Promise.
*/ */
public sendEmail(): Promise<any> { public sendEmail(): Promise<any> {
new Promise((resolve, reject) => nodemailer.createTransport(this.getMailerOptions().transport).sendMail( new Promise((resolve, reject) =>
this.getNodeMailerPayload(), nodemailer
(error, info) => { .createTransport(this.getMailerOptions().transport)
if (error) { .sendMail(this.getNodeMailerPayload(), (error, info) => {
this.printNodeMailerError(error); if (error) {
reject(new Error(error)); this.printNodeMailerError(error);
} else { reject(new Error(error));
resolve(info); } else {
} resolve(info);
}) }
})
).catch((e) => console.error("sendEmail", e)); ).catch((e) => console.error("sendEmail", e));
return new Promise((resolve) => resolve("send mail async")); return new Promise((resolve) => resolve("send mail async"));
} }
@ -95,6 +113,8 @@ export default abstract class EventMail {
return ""; return "";
} }
protected abstract getLocation(): string;
/** /**
* Prints out the desired information when an error * Prints out the desired information when an error
* occured while sending the mail. * occured while sending the mail.
@ -109,7 +129,7 @@ export default abstract class EventMail {
* @protected * @protected
*/ */
protected getRescheduleLink(): string { protected getRescheduleLink(): string {
return process.env.BASE_URL + '/reschedule/' + this.uid; return process.env.BASE_URL + "/reschedule/" + this.uid;
} }
/** /**
@ -118,10 +138,9 @@ export default abstract class EventMail {
* @protected * @protected
*/ */
protected getCancelLink(): string { protected getCancelLink(): string {
return process.env.BASE_URL + '/cancel/' + this.uid; return process.env.BASE_URL + "/cancel/" + this.uid;
} }
/** /**
* Defines a footer that will be appended to the email. * Defines a footer that will be appended to the email.
* @protected * @protected

View file

@ -1,11 +1,11 @@
import {createEvent} from "ics"; import { createEvent } from "ics";
import dayjs, {Dayjs} from "dayjs"; import dayjs, { Dayjs } from "dayjs";
import EventMail from "./EventMail"; import EventMail from "./EventMail";
import utc from 'dayjs/plugin/utc'; import utc from "dayjs/plugin/utc";
import timezone from 'dayjs/plugin/timezone'; import timezone from "dayjs/plugin/timezone";
import toArray from 'dayjs/plugin/toArray'; import toArray from "dayjs/plugin/toArray";
import localizedFormat from 'dayjs/plugin/localizedFormat'; import localizedFormat from "dayjs/plugin/localizedFormat";
dayjs.extend(utc); dayjs.extend(utc);
dayjs.extend(timezone); dayjs.extend(timezone);
dayjs.extend(toArray); dayjs.extend(toArray);
@ -18,14 +18,24 @@ export default class EventOrganizerMail extends EventMail {
*/ */
protected getiCalEventAsString(): string { protected getiCalEventAsString(): string {
const icsEvent = createEvent({ const icsEvent = createEvent({
start: dayjs(this.calEvent.startTime).utc().toArray().slice(0, 6).map((v, i) => i === 1 ? v + 1 : v), start: dayjs(this.calEvent.startTime)
startInputType: 'utc', .utc()
productId: 'calendso/ics', .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}`, title: `${this.calEvent.type} with ${this.calEvent.attendees[0].name}`,
description: this.calEvent.description + this.stripHtml(this.getAdditionalBody()) + this.stripHtml(this.getAdditionalFooter()), description:
duration: { minutes: dayjs(this.calEvent.endTime).diff(dayjs(this.calEvent.startTime), 'minute') }, this.calEvent.description +
this.stripHtml(this.getAdditionalBody()) +
this.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 }, organizer: { name: this.calEvent.organizer.name, email: this.calEvent.organizer.email },
attendees: this.calEvent.attendees.map( (attendee: any) => ({ name: attendee.name, email: attendee.email }) ), attendees: this.calEvent.attendees.map((attendee: any) => ({
name: attendee.name,
email: attendee.email,
})),
status: "CONFIRMED", status: "CONFIRMED",
}); });
if (icsEvent.error) { if (icsEvent.error) {
@ -40,7 +50,8 @@ export default class EventOrganizerMail extends EventMail {
* @protected * @protected
*/ */
protected getHtmlRepresentation(): string { protected getHtmlRepresentation(): string {
return ` return (
`
<div> <div>
Hi ${this.calEvent.organizer.name},<br /> Hi ${this.calEvent.organizer.name},<br />
<br /> <br />
@ -51,40 +62,71 @@ export default class EventOrganizerMail extends EventMail {
<br /> <br />
<strong>Invitee Email:</strong><br /> <strong>Invitee Email:</strong><br />
<a href="mailto:${this.calEvent.attendees[0].email}">${this.calEvent.attendees[0].email}</a><br /> <a href="mailto:${this.calEvent.attendees[0].email}">${this.calEvent.attendees[0].email}</a><br />
<br />` + this.getAdditionalBody() + <br />` +
( this.getAdditionalBody() +
this.calEvent.location ? ` "<br />" +
<strong>Location:</strong><br />
${this.calEvent.location}<br />
<br />
` : ''
) +
`<strong>Invitee Time Zone:</strong><br /> `<strong>Invitee Time Zone:</strong><br />
${this.calEvent.attendees[0].timeZone}<br /> ${this.calEvent.attendees[0].timeZone}<br />
<br /> <br />
<strong>Additional notes:</strong><br /> <strong>Additional notes:</strong><br />
${this.calEvent.description} ${this.calEvent.description}
` + this.getAdditionalFooter() + ` ` +
this.getAdditionalFooter() +
`
</div> </div>
`; `
);
} }
/**
* 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()}
`;
}
/** /**
* Returns the payload object for the nodemailer. * Returns the payload object for the nodemailer.
* *
* @protected * @protected
*/ */
protected getNodeMailerPayload(): Object { protected getNodeMailerPayload() {
const organizerStart: Dayjs = <Dayjs>dayjs(this.calEvent.startTime).tz(this.calEvent.organizer.timeZone); const organizerStart: Dayjs = <Dayjs>dayjs(this.calEvent.startTime).tz(this.calEvent.organizer.timeZone);
return { return {
icalEvent: { icalEvent: {
filename: 'event.ics', filename: "event.ics",
content: this.getiCalEventAsString(), content: this.getiCalEventAsString(),
}, },
from: `Calendso <${this.getMailerOptions().from}>`, from: `Calendso <${this.getMailerOptions().from}>`,
to: this.calEvent.organizer.email, to: this.calEvent.organizer.email,
subject: `New event: ${this.calEvent.attendees[0].name} - ${organizerStart.format('LT dddd, LL')} - ${this.calEvent.type}`, subject: `New event: ${this.calEvent.attendees[0].name} - ${organizerStart.format("LT dddd, LL")} - ${
this.calEvent.type
}`,
html: this.getHtmlRepresentation(), html: this.getHtmlRepresentation(),
text: this.getPlainTextRepresentation(), text: this.getPlainTextRepresentation(),
}; };