feat(privat/CV): sync to latest upstream (cv-upstream/main, 30f9608b)
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import crypto from 'crypto'
|
||||
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const DATA_DIR = join(__dirname, '..', 'data')
|
||||
const TOKENS_FILE = join(DATA_DIR, 'pending-tokens.json')
|
||||
|
||||
// Ensure data directory exists
|
||||
mkdirSync(DATA_DIR, { recursive: true })
|
||||
|
||||
function loadTokens() {
|
||||
if (!existsSync(TOKENS_FILE)) return []
|
||||
try { return JSON.parse(readFileSync(TOKENS_FILE, 'utf-8')) }
|
||||
catch { return [] }
|
||||
}
|
||||
|
||||
function saveTokens(tokens) {
|
||||
writeFileSync(TOKENS_FILE, JSON.stringify(tokens, null, 2), 'utf-8')
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a confirmation token.
|
||||
* @param {string} type - 'contact' | 'talk-request' | 'newsletter'
|
||||
* @param {object} data - The form data to store
|
||||
* @param {number} expiresInHours - Token expiration (default: 48h)
|
||||
* @returns {string} The generated token
|
||||
*/
|
||||
export function createToken(type, data, expiresInHours = 48) {
|
||||
const token = crypto.randomUUID()
|
||||
const tokens = loadTokens()
|
||||
tokens.push({
|
||||
token,
|
||||
type,
|
||||
data,
|
||||
createdAt: new Date().toISOString(),
|
||||
expiresAt: new Date(Date.now() + expiresInHours * 60 * 60 * 1000).toISOString(),
|
||||
confirmed: false,
|
||||
})
|
||||
saveTokens(tokens)
|
||||
return token
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify and consume a token.
|
||||
* @returns {object|null} The stored data if valid, null if expired/invalid
|
||||
*/
|
||||
export function verifyToken(token) {
|
||||
const tokens = loadTokens()
|
||||
const idx = tokens.findIndex(t => t.token === token && !t.confirmed)
|
||||
if (idx === -1) return null
|
||||
|
||||
const entry = tokens[idx]
|
||||
if (new Date(entry.expiresAt) < new Date()) {
|
||||
// Expired — remove it
|
||||
tokens.splice(idx, 1)
|
||||
saveTokens(tokens)
|
||||
return null
|
||||
}
|
||||
|
||||
// Mark as confirmed
|
||||
tokens[idx].confirmed = true
|
||||
saveTokens(tokens)
|
||||
return entry
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up expired tokens (run periodically).
|
||||
*/
|
||||
export function cleanupExpiredTokens() {
|
||||
const tokens = loadTokens()
|
||||
const now = new Date()
|
||||
const valid = tokens.filter(t => new Date(t.expiresAt) > now || t.confirmed)
|
||||
if (valid.length !== tokens.length) {
|
||||
saveTokens(valid)
|
||||
}
|
||||
}
|
||||
|
||||
// Run cleanup every hour
|
||||
setInterval(cleanupExpiredTokens, 60 * 60 * 1000)
|
||||
@@ -0,0 +1,126 @@
|
||||
import nodemailer from 'nodemailer'
|
||||
|
||||
const SMTP_HOST = process.env.SMTP_HOST
|
||||
const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587', 10)
|
||||
const SMTP_USER = process.env.SMTP_USER
|
||||
const SMTP_PASS = process.env.SMTP_PASS
|
||||
const STAKEHOLDER_EMAIL = process.env.STAKEHOLDER_EMAIL || 'knie@d-hive.de'
|
||||
const BASE_URL = process.env.BASE_URL || 'https://andreknie.de'
|
||||
|
||||
let transporter = null
|
||||
|
||||
function getTransporter() {
|
||||
if (!transporter && SMTP_HOST && SMTP_USER && SMTP_PASS) {
|
||||
transporter = nodemailer.createTransport({
|
||||
host: SMTP_HOST,
|
||||
port: SMTP_PORT,
|
||||
secure: SMTP_PORT === 465,
|
||||
auth: { user: SMTP_USER, pass: SMTP_PASS },
|
||||
})
|
||||
}
|
||||
return transporter
|
||||
}
|
||||
|
||||
export async function sendConfirmationEmail(to, type, token) {
|
||||
const t = getTransporter()
|
||||
if (!t) {
|
||||
console.log(`[Mailer] SMTP not configured. Would send ${type} confirmation to ${to}`)
|
||||
return
|
||||
}
|
||||
|
||||
const confirmUrl = `${BASE_URL}/api/${type}/confirm/${token}`
|
||||
const subjects = {
|
||||
contact: 'Bitte bestätige deine Kontaktanfrage',
|
||||
'talk-request': 'Bitte bestätige deine Vortragsanfrage',
|
||||
newsletter: 'Bitte bestätige dein Newsletter-Abo',
|
||||
}
|
||||
|
||||
await t.sendMail({
|
||||
from: SMTP_USER,
|
||||
to,
|
||||
subject: subjects[type] || 'Bestätigung erforderlich',
|
||||
text: `Bitte bestätige deine Anfrage:\n\n${confirmUrl}\n\nDieser Link ist 48 Stunden gültig.`,
|
||||
})
|
||||
}
|
||||
|
||||
export async function sendStakeholderNotification(type, data) {
|
||||
const t = getTransporter()
|
||||
if (!t) {
|
||||
console.log(`[Mailer] SMTP not configured. Would notify stakeholder about ${type}`)
|
||||
return
|
||||
}
|
||||
|
||||
const subjects = {
|
||||
contact: `Neue Kontaktanfrage von ${data.name}`,
|
||||
'talk-request': `Neue Vortragsanfrage von ${data.name}`,
|
||||
}
|
||||
|
||||
const body = Object.entries(data)
|
||||
.map(([key, val]) => `${key}: ${val}`)
|
||||
.join('\n')
|
||||
|
||||
await t.sendMail({
|
||||
from: SMTP_USER,
|
||||
to: STAKEHOLDER_EMAIL,
|
||||
subject: subjects[type] || `Neue Anfrage (${type})`,
|
||||
text: body,
|
||||
})
|
||||
}
|
||||
|
||||
export { STAKEHOLDER_EMAIL, BASE_URL }
|
||||
|
||||
/**
|
||||
* Send the speaker CV PDF as an attachment to the requester, and notify the
|
||||
* stakeholder about the lead. If the PDF is missing, the stakeholder is still
|
||||
* notified so the CV can be sent manually.
|
||||
* @returns {{ sent: boolean, pdfMissing: boolean }}
|
||||
*/
|
||||
export async function sendSpeakerCv(to, lead, pdfPath) {
|
||||
const { existsSync } = await import('fs')
|
||||
const t = getTransporter()
|
||||
const pdfMissing = !pdfPath || !existsSync(pdfPath)
|
||||
|
||||
if (!t) {
|
||||
console.log(`[Mailer] SMTP not configured. Would send speaker CV to ${to} (pdfMissing=${pdfMissing})`)
|
||||
await notifyStakeholderSpeakerCv(lead, pdfMissing)
|
||||
return { sent: false, pdfMissing }
|
||||
}
|
||||
|
||||
if (!pdfMissing) {
|
||||
await t.sendMail({
|
||||
from: SMTP_USER,
|
||||
to,
|
||||
subject: 'Dein Speaker CV von Dr. Andre Knie',
|
||||
text: `Hallo ${lead.name},\n\nvielen Dank für dein Interesse. Im Anhang findest du meinen Speaker CV.\n\nBeste Grüße\nAndre Knie`,
|
||||
attachments: [{ filename: 'Speaker-CV-Andre-Knie.pdf', path: pdfPath }],
|
||||
})
|
||||
}
|
||||
|
||||
await notifyStakeholderSpeakerCv(lead, pdfMissing)
|
||||
return { sent: !pdfMissing, pdfMissing }
|
||||
}
|
||||
|
||||
async function notifyStakeholderSpeakerCv(lead, pdfMissing) {
|
||||
const t = getTransporter()
|
||||
const body = [
|
||||
`Name: ${lead.name}`,
|
||||
`E-Mail: ${lead.email}`,
|
||||
`Nachricht: ${lead.message}`,
|
||||
`Zeitpunkt: ${lead.timestamp}`,
|
||||
pdfMissing
|
||||
? 'ACHTUNG: PDF fehlte — CV wurde NICHT automatisch versandt, bitte manuell senden.'
|
||||
: 'CV wurde automatisch an den Anfragenden gesendet.',
|
||||
].join('\n')
|
||||
|
||||
if (!t) {
|
||||
console.log(`[Mailer] SMTP not configured. Would notify stakeholder about speaker-cv lead:\n${body}`)
|
||||
return
|
||||
}
|
||||
|
||||
await t.sendMail({
|
||||
from: SMTP_USER,
|
||||
to: STAKEHOLDER_EMAIL,
|
||||
subject: `Speaker-CV-Anfrage von ${lead.name}`,
|
||||
text: body,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user