mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
204 lines
4.8 KiB
TypeScript
204 lines
4.8 KiB
TypeScript
import { IBooking } from '../db/booking'
|
|
import { getBaseURL } from '../helpers/url'
|
|
import { daysFormatFrontend } from './date'
|
|
import { generateCalendarEntry } from './ical'
|
|
import nodemailer from 'nodemailer';
|
|
|
|
const smtpUser = process.env.SMTP_USER
|
|
const smtpPass = process.env.SMTP_PASS
|
|
const adminEmail = process.env.ADMIN_EMAIL
|
|
const fromEmail = process.env.FROM_EMAIL
|
|
|
|
if (!smtpUser) {
|
|
throw new Error('NO SMTP_USER set!')
|
|
}
|
|
|
|
if (!smtpPass) {
|
|
throw new Error('NO SMTP_PASS set!')
|
|
}
|
|
|
|
if (!adminEmail) {
|
|
throw new Error('No ADMIN_EMAIL set!')
|
|
}
|
|
|
|
if (!fromEmail) {
|
|
throw new Error('No FROM_EMAIL set!')
|
|
}
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
host: "wirtanen.uberspace.de",
|
|
port: 465,
|
|
secure: true,
|
|
auth: {
|
|
user: smtpUser,
|
|
pass: smtpPass,
|
|
},
|
|
logger: true,
|
|
//debug: true,
|
|
});
|
|
|
|
const footer = `
|
|
|
|
Viele Grüße
|
|
|
|
--
|
|
Thomas Ruoff
|
|
Pfadi Bussle Wart
|
|
|
|
Tel. 0151/212 253 62
|
|
${getBaseURL()}
|
|
`
|
|
|
|
function getReceivedBookingBookerText(booking: IBooking): string {
|
|
return `Hallo liebe/r ${booking.name},
|
|
|
|
Vielen Dank für Deine Buchungsanfrage zum ${daysFormatFrontend(booking.days)}!
|
|
|
|
Nach Prüfung bestätigen wir die Buchung bald per E-Mail!
|
|
|
|
Du kannst sie jederzeit unter
|
|
|
|
${getBaseURL()}/bookings/${booking.uuid}
|
|
|
|
einsehen und auch stornieren.
|
|
|
|
${footer}
|
|
`
|
|
}
|
|
|
|
function getBookingConfirmedText(booking: IBooking): string {
|
|
return `Hallo liebe/r ${booking.name},
|
|
|
|
deine Buchunganfrage zum ${daysFormatFrontend(
|
|
booking.days
|
|
)} bestätigen wir gerne!
|
|
|
|
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()}/bookings/${booking.uuid}
|
|
einsehen und stornieren.
|
|
|
|
${footer}
|
|
`
|
|
}
|
|
|
|
function getBookingRejectedText(booking: IBooking): string {
|
|
return `Hallo liebe/r ${booking.name},
|
|
|
|
es tut uns leid, aber deine Buchungsanfrage zum ${daysFormatFrontend(
|
|
booking.days
|
|
)} konnten wir leider nicht bestätigen.
|
|
|
|
Willst du das Bussle an einem anderen Termin buchen? Dann stelle bitte nochmal
|
|
eine Buchungsanfrage auf ${getBaseURL()}.
|
|
|
|
${footer}
|
|
`
|
|
}
|
|
|
|
function getBookingCanceledText(booking: IBooking): string {
|
|
return `Hallo liebe/r ${booking.name},
|
|
|
|
deine Buchungsanfrage zum ${daysFormatFrontend(booking.days)} wurde storniert.
|
|
|
|
Willst du das Bussle an einem anderen Termin buchen? Dann stelle bitte nochmal
|
|
eine Buchungsanfrage auf ${getBaseURL()}.
|
|
|
|
${footer}
|
|
`
|
|
}
|
|
|
|
function getReceivedBookingAdminText(booking: { uuid: string }): string {
|
|
return `Hallo lieber Admin,
|
|
|
|
es ging folgende Buchung ein: ${getBaseURL()}/admin/bookings/${booking.uuid}
|
|
|
|
MfG`
|
|
}
|
|
|
|
export async function sendReceivedBookingAdminMail(
|
|
booking: IBooking
|
|
): Promise<void> {
|
|
await sendMail({
|
|
to: [{ address: adminEmail, name: 'Pfadi-Bussle Wart' }],
|
|
from: { address: fromEmail, name: 'Pfadi-Bussle Wart' },
|
|
subject: `Buchung für ${booking.days} eingegangen!`,
|
|
text: getReceivedBookingAdminText(booking),
|
|
})
|
|
}
|
|
|
|
export async function sendReceivedBookingBookerMail(
|
|
booking: IBooking
|
|
): Promise<void> {
|
|
await sendMail({
|
|
to: [{ address: booking.email, name: booking.name }],
|
|
from: { address: fromEmail, name: 'Pfadi-Bussle Wart' },
|
|
subject: `Deine Pfadi-Bussle Buchung ist eingegangen!`,
|
|
text: getReceivedBookingBookerText(booking),
|
|
})
|
|
}
|
|
|
|
export async function sendBookingConfirmed(booking: IBooking): Promise<void> {
|
|
await sendMail({
|
|
to: [{ address: booking.email, name: booking.name }],
|
|
from: { address: fromEmail, name: 'Pfadi-Bussle Wart' },
|
|
subject: `Deine Pfadi-Bussle Buchung wurde bestätigt!`,
|
|
text: getBookingConfirmedText(booking),
|
|
attachments: [
|
|
{
|
|
content: Buffer.from(generateCalendarEntry(booking)).toString(
|
|
'base64'
|
|
),
|
|
type: 'text/calendar',
|
|
filename: 'pfadibussle-buchung.ics',
|
|
},
|
|
],
|
|
})
|
|
}
|
|
|
|
export async function sendBookingRejected(booking: IBooking): Promise<void> {
|
|
await sendMail({
|
|
to: [{ address: booking.email, name: booking.name }],
|
|
from: { address: fromEmail, name: 'Pfadi-Bussle Wart' },
|
|
subject: `Deine Pfadi-Bussle Buchung wurde abgelehnt!`,
|
|
text: getBookingRejectedText(booking),
|
|
})
|
|
}
|
|
|
|
export async function sendBookingCanceled(booking: IBooking): Promise<void> {
|
|
await sendMail({
|
|
to: [{ address: booking.email, name: booking.name }],
|
|
from: { address: fromEmail, name: 'Pfadi-Bussle Wart' },
|
|
subject: `Deine Pfadi-Bussle Buchung wurde storniert!`,
|
|
text: getBookingCanceledText(booking),
|
|
})
|
|
}
|
|
|
|
async function sendMail({
|
|
to,
|
|
from,
|
|
subject,
|
|
text,
|
|
attachments,
|
|
}: {
|
|
to: { address: string; name: string }[]
|
|
from: { address: string; name: string }
|
|
subject: string
|
|
text: string
|
|
attachments?: {
|
|
content: string
|
|
type?: string
|
|
filename: string
|
|
}[]
|
|
}): Promise<void> {
|
|
|
|
// send mail with defined transport object
|
|
await transporter.sendMail({
|
|
to,
|
|
from,
|
|
subject,
|
|
text,
|
|
attachments,
|
|
});
|
|
} |