2021-06-29 21:43:18 +00:00
|
|
|
import CalEventParser from "../CalEventParser";
|
|
|
|
import { stripHtml } from "./helpers";
|
2021-06-29 00:30:45 +00:00
|
|
|
import { CalendarEvent, ConferenceData } from "../calendarClient";
|
|
|
|
import { serverConfig } from "../serverConfig";
|
|
|
|
import nodemailer from "nodemailer";
|
|
|
|
|
2021-07-20 18:40:41 +00:00
|
|
|
export interface EntryPoint {
|
2021-06-29 00:30:45 +00:00
|
|
|
entryPointType?: string;
|
|
|
|
uri?: string;
|
|
|
|
label?: string;
|
|
|
|
pin?: string;
|
|
|
|
accessCode?: string;
|
|
|
|
meetingCode?: string;
|
|
|
|
passcode?: string;
|
|
|
|
password?: string;
|
|
|
|
}
|
|
|
|
|
2021-07-20 18:40:41 +00:00
|
|
|
export interface AdditionInformation {
|
2021-06-29 00:30:45 +00:00
|
|
|
conferenceData?: ConferenceData;
|
|
|
|
entryPoints?: EntryPoint[];
|
|
|
|
hangoutLink?: string;
|
|
|
|
}
|
2021-06-16 22:26:51 +00:00
|
|
|
|
|
|
|
export default abstract class EventMail {
|
|
|
|
calEvent: CalendarEvent;
|
2021-06-29 21:43:18 +00:00
|
|
|
parser: CalEventParser;
|
2021-06-16 22:26:51 +00:00
|
|
|
uid: string;
|
2021-06-29 00:30:45 +00:00
|
|
|
additionInformation?: AdditionInformation;
|
2021-06-16 22:26:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An EventMail always consists of a CalendarEvent
|
|
|
|
* that stores the very basic data of the event (like date, title etc).
|
|
|
|
* It also needs the UID of the stored booking in our database.
|
|
|
|
*
|
|
|
|
* @param calEvent
|
|
|
|
* @param uid
|
2021-07-20 18:40:41 +00:00
|
|
|
* @param additionInformation
|
2021-06-16 22:26:51 +00:00
|
|
|
*/
|
2021-06-29 00:30:45 +00:00
|
|
|
constructor(calEvent: CalendarEvent, uid: string, additionInformation: AdditionInformation = null) {
|
2021-06-16 22:26:51 +00:00
|
|
|
this.calEvent = calEvent;
|
|
|
|
this.uid = uid;
|
2021-07-25 17:15:31 +00:00
|
|
|
this.parser = new CalEventParser(calEvent, uid);
|
2021-06-29 00:30:45 +00:00
|
|
|
this.additionInformation = additionInformation;
|
2021-06-16 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the email text as HTML representation.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected abstract getHtmlRepresentation(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the email text in a plain text representation
|
|
|
|
* by stripping off the HTML tags.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getPlainTextRepresentation(): string {
|
2021-06-29 21:43:18 +00:00
|
|
|
return stripHtml(this.getHtmlRepresentation());
|
2021-06-16 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the payload object for the nodemailer.
|
|
|
|
* @protected
|
|
|
|
*/
|
2021-06-29 21:43:18 +00:00
|
|
|
protected abstract getNodeMailerPayload(): Record<string, unknown>;
|
2021-06-16 22:26:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the email to the event attendant and returns a Promise.
|
|
|
|
*/
|
|
|
|
public sendEmail(): Promise<any> {
|
2021-06-29 21:43:18 +00:00
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
nodemailer
|
|
|
|
.createTransport(this.getMailerOptions().transport)
|
|
|
|
.sendMail(this.getNodeMailerPayload(), (error, info) => {
|
|
|
|
if (error) {
|
|
|
|
this.printNodeMailerError(error);
|
|
|
|
reject(new Error(error));
|
|
|
|
} else {
|
|
|
|
resolve(info);
|
|
|
|
}
|
|
|
|
})
|
2021-06-24 17:30:39 +00:00
|
|
|
).catch((e) => console.error("sendEmail", e));
|
|
|
|
return new Promise((resolve) => resolve("send mail async"));
|
2021-06-16 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gathers the required provider information from the config.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getMailerOptions(): any {
|
|
|
|
return {
|
|
|
|
transport: serverConfig.transport,
|
|
|
|
from: serverConfig.from,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Can be used to include additional HTML or plain text
|
|
|
|
* content into the mail body. Leave it to an empty
|
|
|
|
* string if not desired.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getAdditionalBody(): string {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-06-29 00:30:45 +00:00
|
|
|
protected abstract getLocation(): string;
|
|
|
|
|
2021-06-16 22:26:51 +00:00
|
|
|
/**
|
|
|
|
* Prints out the desired information when an error
|
|
|
|
* occured while sending the mail.
|
|
|
|
* @param error
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected abstract printNodeMailerError(error: string): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a link to reschedule the given booking.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getRescheduleLink(): string {
|
2021-06-29 21:43:18 +00:00
|
|
|
return this.parser.getRescheduleLink();
|
2021-06-16 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a link to cancel the given booking.
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getCancelLink(): string {
|
2021-06-29 21:43:18 +00:00
|
|
|
return this.parser.getCancelLink();
|
2021-06-16 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a footer that will be appended to the email.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
protected getAdditionalFooter(): string {
|
2021-06-29 21:43:18 +00:00
|
|
|
return this.parser.getChangeEventFooterHtml();
|
2021-06-16 22:26:51 +00:00
|
|
|
}
|
2021-06-24 17:30:39 +00:00
|
|
|
}
|