feat(demo): Mail-Benachrichtigungen bei Demo-Ablauf

Neue Mail-Templates (lib/mail.js):
- sendDemoExpiryReminderMail: 7 Tage vor Ablauf
- sendDemoLastDayMail: 1 Tag vor Ablauf (Letzter-Tag-Reminder)
- sendDemoExpiredMail: am Ablauftag (setzt Tenant-Status abgelaufen)

Cron-Job (lib/expiryJob.js):
- node-cron taeglich 08:00 UTC (anpassbar via CRON_EXPIRY_AT)
- 3 Windows pruefen, idempotent ueber notified_*_at Timestamp-Spalten
- BETWEEN-Fenster [n-1, n] Tage fuer Robustheit

DB-Migration (bootstrap.js):
- Beim Backend-Start automatisches ALTER TABLE fuer
  notified_7d_at, notified_1d_at, notified_expired_at (idempotent)

Manueller Test: docker exec pfandsystem-demo-backend node -e "
  import(\"./lib/expiryJob.js\").then(m => m.runExpiryCheck())"
This commit is contained in:
christian
2026-05-26 15:59:25 +00:00
parent d9c10211d8
commit 3228fe4ab3
5 changed files with 218 additions and 33 deletions

View File

@@ -8,11 +8,8 @@ if (process.env.SMTP_HOST && process.env.SMTP_USER && process.env.SMTP_PASS) {
transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT || '587'),
secure: (process.env.SMTP_SECURE || 'false') === 'true', // 587 = STARTTLS (secure=false)
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
secure: (process.env.SMTP_SECURE || 'false') === 'true',
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
});
console.log('[mail] SMTP transport aktiv:', process.env.SMTP_HOST);
}
@@ -23,20 +20,31 @@ async function sendMail({ to, subject, html }) {
if (transporter) {
const info = await transporter.sendMail({ from: FROM, to, subject, html });
console.log('[mail] SMTP sent:', to, 'msgId:', info.messageId);
return { provider: 'smtp', messageId: info.messageId, accepted: info.accepted, rejected: info.rejected };
return { provider: 'smtp', messageId: info.messageId };
}
if (resend) {
const r = await resend.emails.send({ from: FROM, to, subject, html });
console.log('[mail] Resend sent:', to, 'id:', r.data?.id);
return { provider: 'resend', id: r.data?.id, error: r.error };
return { provider: 'resend', id: r.data?.id };
}
console.log('[mail-dev] (kein Provider konfiguriert) ->', to, subject);
return { dev: true };
}
const wrap = (inner) => `
<div style="font-family:system-ui,Segoe UI,Helvetica,Arial,sans-serif;max-width:560px;margin:auto;color:#0f172a">
${inner}
<hr style="border:none;border-top:1px solid #e2e8f0;margin:24px 0">
<p style="color:#64748b;font-size:12px">
Dockly-Pfandsystem Demo · <a href="https://pfandsystem.dockly.de" style="color:#1a4148">pfandsystem.dockly.de</a>
</p>
</div>`;
export async function sendDemoInviteMail({ to, name, slug, password, loginUrl, days }) {
const html = `
<div style="font-family:system-ui,Segoe UI,Helvetica,Arial,sans-serif;max-width:560px;margin:auto">
return sendMail({
to,
subject: `Dein Dockly-Pfandsystem-Demo-Zugang (${days} Tage)`,
html: wrap(`
<h2 style="color:#051e23">Willkommen im Dockly-Pfandsystem-Demo</h2>
<p>Hallo ${name},</p>
<p>dein <strong>${days}-Tage Demo-Zugang</strong> ist bereit. Du kannst alle Funktionen
@@ -48,20 +56,87 @@ export async function sendDemoInviteMail({ to, name, slug, password, loginUrl, d
<p style="margin:4px 0"><strong>Tenant-Kennung:</strong> ${slug}</p>
</div>
<p>Bitte ändere dein Passwort nach dem ersten Login.</p>
<p style="color:#64748b;font-size:12px">Diese Mail wurde automatisch versandt vom Dockly-Pfandsystem-Demo-Portal.</p>
</div>`;
return sendMail({ to, subject: `Dein Dockly-Pfandsystem-Demo-Zugang (${days} Tage)`, html });
`),
});
}
// Test-Endpoint Helper
// 7 Tage vor Ablauf
export async function sendDemoExpiryReminderMail({ to, name, slug, daysLeft, expiresAt }) {
const loginUrl = `https://pfandsystem.dockly.de/login?tenant=${slug}`;
const dateStr = new Date(expiresAt).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' });
return sendMail({
to,
subject: `Erinnerung: Dein Demo-Zugang läuft in ${daysLeft} Tagen ab`,
html: wrap(`
<h2 style="color:#051e23">Dein Demo-Zugang läuft bald ab</h2>
<p>Hallo ${name},</p>
<p>nur noch <strong>${daysLeft} Tag${daysLeft === 1 ? '' : 'e'}</strong> &mdash; danach wird dein
Demo-Zugang am <strong>${dateStr}</strong> automatisch deaktiviert.</p>
<p>Du willst weiter testen oder das System für deinen Betrieb live nehmen?
Antworte einfach auf diese Mail &mdash; wir verlängern den Zugang oder
besprechen die Live-Schaltung mit dir.</p>
<p style="margin-top:24px">
<a href="${loginUrl}"
style="background:#051e23;color:#fff;padding:10px 18px;border-radius:8px;text-decoration:none;font-weight:600">
Jetzt einloggen
</a>
</p>
`),
});
}
// 1 Tag vor Ablauf (Letzter-Tag-Variante)
export async function sendDemoLastDayMail({ to, name, slug, expiresAt }) {
const loginUrl = `https://pfandsystem.dockly.de/login?tenant=${slug}`;
const dateStr = new Date(expiresAt).toLocaleDateString('de-DE');
return sendMail({
to,
subject: 'Letzter Tag: Dein Demo-Zugang läuft morgen ab',
html: wrap(`
<h2 style="color:#051e23">Letzter Tag für dein Demo</h2>
<p>Hallo ${name},</p>
<p>morgen (<strong>${dateStr}</strong>) endet dein Demo-Zugang. Falls du noch Daten exportieren oder etwas
abschließend testen möchtest, schau heute nochmal rein.</p>
<p>Wir würden uns sehr freuen, wenn wir gemeinsam weitermachen.
Eine kurze Antwort auf diese Mail genügt &mdash; wir melden uns sofort zurück.</p>
<p style="margin-top:24px">
<a href="${loginUrl}"
style="background:#051e23;color:#fff;padding:10px 18px;border-radius:8px;text-decoration:none;font-weight:600">
Jetzt einloggen
</a>
</p>
`),
});
}
// Demo ist abgelaufen
export async function sendDemoExpiredMail({ to, name }) {
return sendMail({
to,
subject: 'Dein Demo-Zugang ist abgelaufen',
html: wrap(`
<h2 style="color:#051e23">Dein Demo ist abgelaufen</h2>
<p>Hallo ${name},</p>
<p>dein Demo-Zugang zum Dockly-Pfandsystem ist heute abgelaufen.
Deine Daten sind noch <strong>14 Tage</strong> bei uns gespeichert &mdash;
falls du jetzt verlängern oder live gehen möchtest, antworte einfach auf
diese Mail. Danach werden die Demo-Daten automatisch gelöscht.</p>
<p>Vielen Dank fürs Testen!</p>
`),
});
}
// SMTP-Selbsttest (zum Debuggen)
export async function sendTestMail(to) {
const html = `
<div style="font-family:system-ui,Segoe UI,Helvetica,Arial,sans-serif;max-width:560px;margin:auto">
<h2 style="color:#051e23">Dockly-Pfandsystem Demo - SMTP-Test</h2>
return sendMail({
to,
subject: 'Dockly-Pfandsystem - SMTP-Test',
html: wrap(`
<h2 style="color:#051e23">SMTP-Test</h2>
<p>Diese Test-Mail bestätigt, dass der Mail-Versand über
<code>${process.env.SMTP_HOST || 'kein SMTP'}</code> funktioniert.</p>
<p>Absender: <code>${FROM}</code></p>
<p>Zeitstempel: ${new Date().toISOString()}</p>
</div>`;
return sendMail({ to, subject: 'Dockly-Pfandsystem Demo - SMTP-Test', html });
`),
});
}