Skip to content

Commit

Permalink
Append reschedule/cancel also to event description
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico-J committed Jun 12, 2021
1 parent a231ee6 commit 8227e73
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
24 changes: 10 additions & 14 deletions lib/emails/confirm-booked.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import nodemailer from 'nodemailer';
import { serverConfig } from "../serverConfig";
import { CalendarEvent } from "../calendarClient";
import dayjs, { Dayjs } from "dayjs";
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";
Expand All @@ -11,8 +10,8 @@ dayjs.extend(localizedFormat);
dayjs.extend(utc);
dayjs.extend(timezone);

export default function createConfirmBookedEmail(calEvent: CalendarEvent, uid: String, options: any = {}) {
return sendEmail(calEvent, uid, {
export default function createConfirmBookedEmail(calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string, options: any = {}) {
return sendEmail(calEvent, cancelLink, rescheduleLink, {
provider: {
transport: serverConfig.transport,
from: serverConfig.from,
Expand All @@ -21,7 +20,7 @@ export default function createConfirmBookedEmail(calEvent: CalendarEvent, uid: S
});
}

const sendEmail = (calEvent: CalendarEvent, uid: String, {
const sendEmail = (calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string, {
provider,
}) => new Promise( (resolve, reject) => {

Expand All @@ -34,8 +33,8 @@ const sendEmail = (calEvent: CalendarEvent, uid: String, {
from: `${calEvent.organizer.name} <${from}>`,
replyTo: calEvent.organizer.email,
subject: `Confirmed: ${calEvent.type} with ${calEvent.organizer.name} on ${inviteeStart.format('dddd, LL')}`,
html: html(calEvent, uid),
text: text(calEvent, uid),
html: html(calEvent, cancelLink, rescheduleLink),
text: text(calEvent, cancelLink, rescheduleLink),
},
(error, info) => {
if (error) {
Expand All @@ -47,10 +46,7 @@ const sendEmail = (calEvent: CalendarEvent, uid: String, {
)
});

const html = (calEvent: CalendarEvent, uid: String) => {
const cancelLink = process.env.BASE_URL + '/cancel/' + uid;
const rescheduleLink = process.env.BASE_URL + '/reschedule/' + uid;

const html = (calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string) => {
const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone);
return `
<div>
Expand All @@ -71,4 +67,4 @@ const html = (calEvent: CalendarEvent, uid: String) => {
`;
};

const text = (evt: CalendarEvent, uid: String) => html(evt, uid).replace('<br />', "\n").replace(/<[^>]+>/g, '');
const text = (evt: CalendarEvent, cancelLink: string, rescheduleLink: string) => html(evt, cancelLink, rescheduleLink).replace('<br />', "\n").replace(/<[^>]+>/g, '');
19 changes: 15 additions & 4 deletions pages/api/book/[user].ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
]
};

const hashUID = translator.fromUUID(uuidv5(JSON.stringify(evt), uuidv5.URL));
const hashUID: string = translator.fromUUID(uuidv5(JSON.stringify(evt), uuidv5.URL));
const cancelLink: string = process.env.BASE_URL + '/cancel/' + hashUID;
const rescheduleLink:string = process.env.BASE_URL + '/reschedule/' + hashUID;
const appendLinksToEvents = (event: CalendarEvent) => {
const eventCopy = {...event};
eventCopy.description += "\n\n"
+ "Need to change this event?\n"
+ "Cancel: " + cancelLink + "\n"
+ "Reschedule:" + rescheduleLink;

return eventCopy;
}

const eventType = await prisma.eventType.findFirst({
where: {
Expand Down Expand Up @@ -75,7 +86,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// Use all integrations
results = await async.mapLimit(currentUser.credentials, 5, async (credential) => {
const bookingRefUid = booking.references.filter((ref) => ref.type === credential.type)[0].uid;
return await updateEvent(credential, bookingRefUid, evt)
return await updateEvent(credential, bookingRefUid, appendLinksToEvents(evt))
});

// Clone elements
Expand Down Expand Up @@ -106,7 +117,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
} else {
// Schedule event
results = await async.mapLimit(currentUser.credentials, 5, async (credential) => {
const response = await createEvent(credential, evt);
const response = await createEvent(credential, appendLinksToEvents(evt));
return {
type: credential.type,
response
Expand Down Expand Up @@ -144,7 +155,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// If one of the integrations allows email confirmations or no integrations are added, send it.
if (currentUser.credentials.length === 0 || !results.every((result) => result.disableConfirmationEmail)) {
await createConfirmBookedEmail(
evt, hashUID
evt, cancelLink, rescheduleLink
);
}

Expand Down

0 comments on commit 8227e73

Please sign in to comment.