Append reschedule/cancel also to event description

pull/260/head
nicolas 2021-06-13 00:37:35 +02:00
parent a231ee6c0d
commit 8227e733e6
2 changed files with 25 additions and 18 deletions

View File

@ -1,8 +1,7 @@
import nodemailer from 'nodemailer'; import nodemailer from 'nodemailer';
import { serverConfig } from "../serverConfig"; import {serverConfig} from "../serverConfig";
import { CalendarEvent } from "../calendarClient"; import {CalendarEvent} from "../calendarClient";
import dayjs, { Dayjs } from "dayjs"; import dayjs, {Dayjs} from "dayjs";
import localizedFormat from "dayjs/plugin/localizedFormat"; import localizedFormat from "dayjs/plugin/localizedFormat";
import utc from "dayjs/plugin/utc"; import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone"; import timezone from "dayjs/plugin/timezone";
@ -11,8 +10,8 @@ dayjs.extend(localizedFormat);
dayjs.extend(utc); dayjs.extend(utc);
dayjs.extend(timezone); dayjs.extend(timezone);
export default function createConfirmBookedEmail(calEvent: CalendarEvent, uid: String, options: any = {}) { export default function createConfirmBookedEmail(calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string, options: any = {}) {
return sendEmail(calEvent, uid, { return sendEmail(calEvent, cancelLink, rescheduleLink, {
provider: { provider: {
transport: serverConfig.transport, transport: serverConfig.transport,
from: serverConfig.from, from: serverConfig.from,
@ -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, provider,
}) => new Promise( (resolve, reject) => { }) => new Promise( (resolve, reject) => {
@ -34,8 +33,8 @@ const sendEmail = (calEvent: CalendarEvent, uid: String, {
from: `${calEvent.organizer.name} <${from}>`, from: `${calEvent.organizer.name} <${from}>`,
replyTo: calEvent.organizer.email, replyTo: calEvent.organizer.email,
subject: `Confirmed: ${calEvent.type} with ${calEvent.organizer.name} on ${inviteeStart.format('dddd, LL')}`, subject: `Confirmed: ${calEvent.type} with ${calEvent.organizer.name} on ${inviteeStart.format('dddd, LL')}`,
html: html(calEvent, uid), html: html(calEvent, cancelLink, rescheduleLink),
text: text(calEvent, uid), text: text(calEvent, cancelLink, rescheduleLink),
}, },
(error, info) => { (error, info) => {
if (error) { if (error) {
@ -47,10 +46,7 @@ const sendEmail = (calEvent: CalendarEvent, uid: String, {
) )
}); });
const html = (calEvent: CalendarEvent, uid: String) => { const html = (calEvent: CalendarEvent, cancelLink: string, rescheduleLink: string) => {
const cancelLink = process.env.BASE_URL + '/cancel/' + uid;
const rescheduleLink = process.env.BASE_URL + '/reschedule/' + uid;
const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone); const inviteeStart: Dayjs = <Dayjs>dayjs(calEvent.startTime).tz(calEvent.attendees[0].timeZone);
return ` return `
<div> <div>
@ -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, '');

View File

@ -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({ const eventType = await prisma.eventType.findFirst({
where: { where: {
@ -75,7 +86,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// Use all integrations // Use all integrations
results = await async.mapLimit(currentUser.credentials, 5, async (credential) => { results = await async.mapLimit(currentUser.credentials, 5, async (credential) => {
const bookingRefUid = booking.references.filter((ref) => ref.type === credential.type)[0].uid; 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 // Clone elements
@ -106,7 +117,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
} else { } else {
// Schedule event // Schedule event
results = await async.mapLimit(currentUser.credentials, 5, async (credential) => { results = await async.mapLimit(currentUser.credentials, 5, async (credential) => {
const response = await createEvent(credential, evt); const response = await createEvent(credential, appendLinksToEvents(evt));
return { return {
type: credential.type, type: credential.type,
response response
@ -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 one of the integrations allows email confirmations or no integrations are added, send it.
if (currentUser.credentials.length === 0 || !results.every((result) => result.disableConfirmationEmail)) { if (currentUser.credentials.length === 0 || !results.every((result) => result.disableConfirmationEmail)) {
await createConfirmBookedEmail( await createConfirmBookedEmail(
evt, hashUID evt, cancelLink, rescheduleLink
); );
} }