2021-05-27 22:10:20 +00:00
|
|
|
|
|
|
|
import nodemailer from 'nodemailer';
|
|
|
|
import { serverConfig } from "../serverConfig";
|
|
|
|
import { CalendarEvent } from "../calendarClient";
|
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
|
|
|
import localizedFormat from "dayjs/plugin/localizedFormat";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
|
|
|
|
dayjs.extend(localizedFormat);
|
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
2021-06-08 15:24:06 +00:00
|
|
|
export default function createConfirmBookedEmail(calEvent: CalendarEvent, uid: String, options: any = {}) {
|
|
|
|
return sendEmail(calEvent, uid, {
|
2021-05-27 22:10:20 +00:00
|
|
|
provider: {
|
|
|
|
transport: serverConfig.transport,
|
|
|
|
from: serverConfig.from,
|
|
|
|
},
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:24:06 +00:00
|
|
|
const sendEmail = (calEvent: CalendarEvent, uid: String, {
|
2021-05-27 22:10:20 +00:00
|
|
|
provider,
|
|
|
|
}) => new Promise( (resolve, reject) => {
|
|
|
|
|
|
|
|
const { from, transport } = provider;
|
|
|
|
const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone);
|
|
|
|
|
|
|
|
nodemailer.createTransport(transport).sendMail(
|
|
|
|
{
|
|
|
|
to: `${calEvent.attendees[0].name} <${calEvent.attendees[0].email}>`,
|
2021-05-28 11:16:19 +00:00
|
|
|
from: `${calEvent.organizer.name} <${from}>`,
|
2021-06-10 18:04:07 +00:00
|
|
|
replyTo: calEvent.organizer.email,
|
2021-05-27 22:10:20 +00:00
|
|
|
subject: `Confirmed: ${calEvent.type} with ${calEvent.organizer.name} on ${inviteeStart.format('dddd, LL')}`,
|
2021-06-08 15:24:06 +00:00
|
|
|
html: html(calEvent, uid),
|
|
|
|
text: text(calEvent, uid),
|
2021-05-27 22:10:20 +00:00
|
|
|
},
|
|
|
|
(error, info) => {
|
|
|
|
if (error) {
|
|
|
|
console.error("SEND_BOOKING_CONFIRMATION_ERROR", calEvent.attendees[0].email, error);
|
|
|
|
return reject(new Error(error));
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2021-06-08 15:24:06 +00:00
|
|
|
const html = (calEvent: CalendarEvent, uid: String) => {
|
|
|
|
const cancelLink = process.env.BASE_URL + '/cancel/' + uid;
|
|
|
|
const rescheduleLink = process.env.BASE_URL + '/reschedule/' + uid;
|
2021-06-06 01:51:24 +00:00
|
|
|
|
2021-05-27 22:10:20 +00:00
|
|
|
const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone);
|
|
|
|
return `
|
|
|
|
<div>
|
|
|
|
Hi ${calEvent.attendees[0].name},<br />
|
|
|
|
<br />
|
2021-05-28 11:16:19 +00:00
|
|
|
Your ${calEvent.type} with ${calEvent.organizer.name} at ${inviteeStart.format('h:mma')}
|
2021-05-27 22:10:20 +00:00
|
|
|
(${calEvent.attendees[0].timeZone}) on ${inviteeStart.format('dddd, LL')} is scheduled.<br />
|
2021-05-28 23:34:28 +00:00
|
|
|
<br />` + (
|
|
|
|
calEvent.location ? `<strong>Location:</strong> ${calEvent.location}<br /><br />` : ''
|
|
|
|
) +
|
|
|
|
`Additional notes:<br />
|
2021-06-06 01:51:24 +00:00
|
|
|
${calEvent.description}<br />
|
|
|
|
<br />
|
|
|
|
Need to change this event?<br />
|
2021-06-08 15:24:06 +00:00
|
|
|
Cancel: <a href="${cancelLink}">${cancelLink}</a><br />
|
|
|
|
Reschedule: <a href="${rescheduleLink}">${rescheduleLink}</a>
|
2021-05-27 22:10:20 +00:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
2021-06-08 15:24:06 +00:00
|
|
|
const text = (evt: CalendarEvent, uid: String) => html(evt, uid).replace('<br />', "\n").replace(/<[^>]+>/g, '');
|