Files
pfadi-bussle/helpers/mail.ts
2020-11-04 00:42:14 +01:00

168 lines
4.2 KiB
TypeScript

import { BookingDocument } from '../db/booking'
import { getBaseURL } from '../helpers/url'
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY
const ADMIN_EMAIL = process.env.ADMIN_EMAIL
const FROM_EMAIL = process.env.FROM_EMAIL
const SENDGRID_URL = 'https://api.sendgrid.com/v3/mail/send'
if (!SENDGRID_API_KEY) {
throw new Error('NO SENDGRID_API_KEY set!')
}
if (!ADMIN_EMAIL) {
throw new Error('No ADMIN_EMAIL set!')
}
if (!FROM_EMAIL) {
throw new Error('No FROM_EMAIL set!')
}
const footer = `
Viele Grüße
Thomas Ruoff
Pfadi Bussle Wart
Tel. 0151/212 253 62
`
function getReceivedBookingBookerText(booking: BookingDocument) {
return `Hallo liebe/r ${booking.booker.name},
Vielen Dank für Deine Buchung. Nach Prüfung bestätigen wir die Buchung bald per E-Mail!
Du kannst sie jederzeit unter
${getBaseURL()}/booking/${booking.uuid}
einsehen und auch stornieren.
${footer}
`
}
function getBookingConfirmedText(booking: BookingDocument) {
return `Hallo liebe/r ${booking.booker.name},
deine Buchung ist bestätigt!
Bitte melde dich spätestens 7 Tage vor dem Buchungstermin per E-Mail oder Telefon
um eine Schlüsselübergabe zu vereinbaren.
Du kannst deine Buchung weiterhin unter ${getBaseURL()}/booking/${booking.uuid}
einsehen und stornieren.
${footer}
`
}
function getBookingRejectedText(booking: BookingDocument) {
return `Hallo liebe/r ${booking.booker.name},
es tut uns leid aber deine Buchung konnte leider nicht bestätigt werden.
Willst du das Bussle an einem anderen Termin buchen? Dann stelle bitte nochmal
eine Buchungsanfrage auf ${getBaseURL()}.
${footer}
`
}
function getReceivedBookingAdminText(booking: { uuid: string }) {
return `Hallo lieber Admin,
es ging folgende Buchung ein: ${getBaseURL()}/admin/booking/${booking.uuid}
MfG`
}
export async function sendReceivedBookingAdminMail(booking: BookingDocument) {
try {
await sendMail({
to: [{ email: ADMIN_EMAIL }],
from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' },
subject: `Buchung für ${booking.days} eingegangen!`,
textPlainContent: getReceivedBookingAdminText(booking),
})
} catch (error) {
console.error(`Failed in sendReceivedBookingMail for ${booking.uuid}`)
}
}
export async function sendReceivedBookingBookerMail(booking: BookingDocument) {
try {
await sendMail({
to: [{ email: booking.booker.email, name: booking.booker.name }],
from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' },
subject: `Deine Pfadi-Bussle Buchung ist eingegangen!`,
textPlainContent: getReceivedBookingBookerText(booking),
})
} catch (error) {
console.error(`Failed in sendReceivedBookingMail for ${booking.uuid}`)
}
}
export async function sendBookingConfirmed(booking: BookingDocument) {
try {
await sendMail({
to: [{ email: booking.booker.email, name: booking.booker.name }],
from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' },
subject: `Deine Pfadi-Bussle Buchung wurde bestätigt!`,
textPlainContent: getBookingConfirmedText(booking),
})
} catch (error) {
console.error(`Failed in sendBookingConfirmedMail for ${booking.uuid}`)
}
}
export async function sendBookingRejected(booking: BookingDocument) {
try {
await sendMail({
to: [{ email: booking.booker.email, name: booking.booker.name }],
from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' },
subject: `Deine Pfadi-Bussle Buchung wurde abgelehnt!`,
textPlainContent: getBookingRejectedText(booking),
})
} catch (error) {
console.error(`Failed in sendBookingRejectedMail for ${booking.uuid}`)
}
}
async function sendMail({
to,
from,
subject,
textPlainContent,
}: {
to: { email: string; name?: string }[]
from: { email: string; name?: string }
subject: string
textPlainContent: string
}) {
const data = {
personalizations: [
{
to,
},
],
from,
subject,
content: [{ type: 'text/plain', value: textPlainContent }],
}
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${SENDGRID_API_KEY}`,
},
body: JSON.stringify(data),
}
const response = await fetch(SENDGRID_URL, fetchOptions)
if (!response.ok) {
throw new Error(`Unable to send mail`)
}
}