rename js/x ts/x

This commit is contained in:
Thomas Ruoff
2020-08-26 22:36:27 +02:00
committed by Thomas Ruoff
parent 9853e31201
commit be1e22460d
23 changed files with 0 additions and 0 deletions

54
helpers/mail.ts Normal file
View File

@@ -0,0 +1,54 @@
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY
const SENDGRID_URL = 'https://api.sendgrid.com/v3/mail/send'
// TODO: move to env
const BOOKING_ADMIN_EMAIL = 'tomru@mail.id0.link'
const FROM_EMAIL = 'pfadibussle@mail.id0.link'
if (!SENDGRID_API_KEY) {
throw new Error('NO SENDGRID_API_KEY set!')
}
if (!SENDGRID_API_KEY) {
throw new Error('NO SENDGRID_API_KEY set!')
}
async function sendMail(data) {
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(
`Failed to send booking ${data._id} to booking admin with status ${response.status}: ${response.statusText}`
)
}
}
function getReceivedBookingText(booking) {
return `Hallo lieber Admin,
es ging folgende Buchung ein: https://${process.env.VERCEL_URL}/booking/${booking.uuid}
MfG`
}
export async function sendReceivedBookingMail(booking) {
const data = {
personalizations: [
{
to: [{ email: BOOKING_ADMIN_EMAIL }],
},
],
from: { email: FROM_EMAIL },
subject: 'Pfadi Bussle - Buchung eingegangen!',
content: [{ type: 'text/plain', value: getReceivedBookingText(booking) }],
}
await sendMail(data)
}