mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +01:00
attach cal entry to mail
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Booking } from '../db/booking'
|
||||
import { getBaseURL } from '../helpers/url'
|
||||
import { daysFormatFrontend } from './date'
|
||||
import { generateCalendarEntry } from './ical'
|
||||
|
||||
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY
|
||||
const ADMIN_EMAIL = process.env.ADMIN_EMAIL
|
||||
@@ -93,7 +94,10 @@ export async function sendReceivedBookingAdminMail(booking: Booking) {
|
||||
textPlainContent: getReceivedBookingAdminText(booking),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Failed in sendReceivedBookingMail for ${booking.uuid}`)
|
||||
console.error(
|
||||
`Failed in sendReceivedBookingMail for ${booking.uuid}`,
|
||||
error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +110,10 @@ export async function sendReceivedBookingBookerMail(booking: Booking) {
|
||||
textPlainContent: getReceivedBookingBookerText(booking),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Failed in sendReceivedBookingMail for ${booking.uuid}`)
|
||||
console.error(
|
||||
`Failed in sendReceivedBookingMail for ${booking.uuid}`,
|
||||
error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,9 +124,21 @@ export async function sendBookingConfirmed(booking: Booking) {
|
||||
from: { email: FROM_EMAIL, name: 'Pfadi-Bussle Wart' },
|
||||
subject: `Deine Pfadi-Bussle Buchung wurde bestätigt!`,
|
||||
textPlainContent: getBookingConfirmedText(booking),
|
||||
attachments: [
|
||||
{
|
||||
content: Buffer.from(generateCalendarEntry(booking)).toString(
|
||||
'base64'
|
||||
),
|
||||
type: 'text/calendar',
|
||||
filename: 'pfadibussle-buchung.ics',
|
||||
},
|
||||
],
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Failed in sendBookingConfirmedMail for ${booking.uuid}`)
|
||||
console.error(
|
||||
`Failed in sendBookingConfirmedMail for ${booking.uuid}`,
|
||||
error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +151,10 @@ export async function sendBookingRejected(booking: Booking) {
|
||||
textPlainContent: getBookingRejectedText(booking),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Failed in sendBookingRejectedMail for ${booking.uuid}`)
|
||||
console.error(
|
||||
`Failed in sendBookingRejectedMail for ${booking.uuid}`,
|
||||
error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,11 +163,17 @@ async function sendMail({
|
||||
from,
|
||||
subject,
|
||||
textPlainContent,
|
||||
attachments,
|
||||
}: {
|
||||
to: { email: string; name?: string }[]
|
||||
from: { email: string; name?: string }
|
||||
subject: string
|
||||
textPlainContent: string
|
||||
attachments: {
|
||||
content: string
|
||||
type?: string
|
||||
filename: string
|
||||
}[]
|
||||
}) {
|
||||
const data = {
|
||||
personalizations: [
|
||||
@@ -156,6 +184,7 @@ async function sendMail({
|
||||
from,
|
||||
subject,
|
||||
content: [{ type: 'text/plain', value: textPlainContent }],
|
||||
attachments,
|
||||
}
|
||||
|
||||
const fetchOptions = {
|
||||
@@ -166,9 +195,12 @@ async function sendMail({
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
}
|
||||
const response = await fetch(SENDGRID_URL, fetchOptions)
|
||||
const resp = await fetch(SENDGRID_URL, fetchOptions)
|
||||
const bodyText = await resp.text()
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Unable to send mail`)
|
||||
if (!resp.ok) {
|
||||
throw new Error(
|
||||
`Unable to send mail, status ${resp.status} ${resp.statusText}, ${bodyText}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user