diff --git a/.gitignore b/.gitignore index 42d77f4..b5af02b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ preview-data/ server/graphify-out/ graphify-out/ +upload-data/ diff --git a/docs/WEBklar-Datenschutzerklaerung.pdf b/docs/WEBklar-Datenschutzerklaerung.pdf index 5ea52c0..41adad7 100644 Binary files a/docs/WEBklar-Datenschutzerklaerung.pdf and b/docs/WEBklar-Datenschutzerklaerung.pdf differ diff --git a/public/admin.html b/public/admin.html index 45f486a..a5ec056 100644 --- a/public/admin.html +++ b/public/admin.html @@ -82,6 +82,49 @@ background: rgba(255,255,255,0.06); color: inherit; font: inherit; } .chat-empty { display: flex; align-items: center; justify-content: center; flex: 1; opacity: 0.6; } + + /* Übernahmegutschriften */ + .credit-filter { display: flex; gap: 8px; margin-bottom: 12px; } + .credit-filter button { + background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.14); + color: inherit; padding: 5px 13px; border-radius: 999px; cursor: pointer; font: inherit; font-size: 13px; + } + .credit-filter button.active { background: rgba(94,234,212,0.16); border-color: rgba(94,234,212,0.5); } + tr.credit-active td { background: rgba(94,234,212,0.07); } + .credit-detail { margin-top: 18px; } + .credit-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; } + .credit-head h3 { margin: 0; } + .credit-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 22px; } + .credit-cols h4 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.7; } + .credit-cols h4:first-child { margin-top: 4px; } + .kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: 14px; margin: 0; } + .kv dt { opacity: 0.6; } + .kv dd { margin: 0; overflow-wrap: anywhere; } + .pre { white-space: pre-wrap; font-size: 14px; margin: 0; overflow-wrap: anywhere; } + .check-list { list-style: none; margin: 0; padding: 0; font-size: 14px; } + .check-list li { padding: 5px 0 5px 26px; position: relative; border-bottom: 1px solid rgba(255,255,255,0.06); } + .check-list li:last-child { border-bottom: none; } + .check-list li::before { position: absolute; left: 2px; top: 5px; font-weight: 700; } + .check-list li.ok::before { content: '✓'; color: #4ade80; } + .check-list li.warn::before { content: '!'; color: #fbbf24; left: 6px; } + .check-list li.info::before { content: 'i'; color: #7dd3fc; left: 6px; font-style: italic; } + .check-list small { display: block; opacity: 0.6; font-size: 12px; overflow-wrap: anywhere; } + .manual-checks label { display: flex; gap: 9px; align-items: flex-start; font-size: 14px; padding: 4px 0; cursor: pointer; } + .manual-checks input { margin-top: 3px; width: 16px; height: 16px; accent-color: #5eead4; flex: none; } + .calc-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; } + .calc-grid label, .credit-field label { display: block; font-size: 12px; opacity: 0.7; margin-bottom: 4px; } + .credit-input { width: 100%; box-sizing: border-box; padding: 7px 10px; border-radius: 9px; border: 1px solid rgba(255,255,255,0.2); + background: rgba(255,255,255,0.06); color: inherit; font: inherit; font-size: 14px; } + .credit-input option { color: #111; } + textarea.credit-input { min-height: 80px; resize: vertical; } + .calc-result { margin: 10px 0; padding: 9px 12px; border-radius: 10px; font-size: 14px; background: rgba(94,234,212,0.08); border: 1px solid rgba(94,234,212,0.3); } + .credit-field { margin-top: 10px; } + .credit-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; } + .mini-btn:disabled { opacity: 0.45; cursor: default; } + .mini-btn.danger { border-color: rgba(248,113,113,0.5); } + .credit-msg { margin-top: 10px; font-size: 14px; } + .history-item { font-size: 13px; padding: 6px 0; border-bottom: 1px solid rgba(255,255,255,0.06); } + .history-item:last-child { border-bottom: none; }
@@ -107,6 +150,7 @@ + @@ -169,6 +213,24 @@ + + +| Eingang | Kunde | Leistung / bisher bei | Rechnung | Bezahlt bis | Status | |
|---|---|---|---|---|---|---|
| Lade… | ||||||
Übernahmegutschrift: Der Kunde hat Domain, Hosting oder E-Mail beim bisherigen Anbieter schon bezahlt. Nach der Prüfung wird der doppelt bezahlte Anteil als Guthaben in Stripe gebucht und mit den nächsten Abo-Rechnungen verrechnet.
+Lade Antrag…
' + document.querySelectorAll('[data-credit-row]').forEach((tr) => + tr.classList.toggle('credit-active', tr.dataset.creditRow === id)) + setCreditUrl(id) + try { + const d = await api(`/api/portal-admin/takeover-credits/${encodeURIComponent(id)}`) + creditState.detail = d + renderCreditDetail(panel, d, message) + } catch (err) { + panel.innerHTML = `${escapeHtml(err.message)}
` + } + panel.scrollIntoView({ behavior: 'smooth', block: 'start' }) +} + +function calcSummary(calc) { + return `Rechnungszeitraum ${fmtDate(calc.periodStart)} – ${fmtDate(calc.periodEnd)} (${calc.periodDays} Tage). ` + + `Doppelt bezahlt ab ${fmtDate(calc.overlapStart)}: ${calc.overlapDays} Tage → Vorschlag ${euroFromCents(calc.suggestedCents)}` +} + +function creditDecisionFormHtml(d) { + const r = d.request + const suggested = d.calc ? d.calc.suggestedCents : 0 + return ` +Werte weichen von der Rechnung ab? Hier korrigieren – die Korrektur wird beim Genehmigen im Verlauf vermerkt. „Übernahme ab“ ist der Leistungsbeginn des Abos.
+Genehmigen geht, sobald alle Punkte abgehakt sind. Der Kunde bekommt jede Entscheidung per E-Mail.${d.subscription?.active ? '' : ' Achtung: Der Kunde hat kein laufendes Abo.'}
+ ` +} + +function creditDecisionSummaryHtml(d) { + const r = d.request + if (r.status === 'genehmigt') { + return ` +${escapeHtml(r.message)}
+Keine Nachweise (mehr) vorhanden.
'} +Schon bei einem anderen Anbieter für Domain, Hosting oder E-Mail bezahlt? Nach dem Abschluss können Sie eine Übernahmegutschrift beantragen – wir rechnen die doppelt bezahlte Zeit anteilig an.
' : ''}Gutschrift: ${fmtEuro(r.amountCents / 100)} – wird mit Ihren nächsten Monatsgebühren verrechnet.
+ ${r.decisionNote ? `${escapeHtml(r.decisionNote)}
` : ''}` + } else if (r.status === 'abgelehnt') { + body = `${escapeHtml(r.decisionNote)}
` + } else if (r.status === 'rueckfrage') { + body = ` +Wir prüfen Ihre Angaben und antworten Ihnen per E-Mail.
' + } + return `Wechsel ohne doppelte Kosten
+Sie haben Domain, Hosting oder E-Mail bei Ihrem bisherigen Anbieter schon im Voraus bezahlt? Den Anteil, den Sie ab Beginn Ihres Abos doppelt zahlen, schreiben wir Ihnen gut und verrechnen ihn mit Ihren nächsten Monatsgebühren. Schicken Sie uns dazu einfach die Rechnung.
+ ${notice ? `Schließen Sie das Wartungs-Abo ab, verarbeiten wir Name, E-Mail-Adresse, Rechnungsanschrift, Abo- und Zahlungsstatus sowie Ihre Rechnungen. Zahlungsdaten wie Kartennummer oder IBAN geben Sie direkt bei unserem Zahlungsdienstleister ein. Wir erhalten davon nur eingeschränkte Angaben, etwa die Art des Zahlungsmittels und die letzten vier Ziffern.
Um den Vertragsschluss nachweisen zu können, speichern wir bei der Bestellung Zeitpunkt, Version der Vertragsbedingungen, Ihre Angaben zum Leistungsbeginn sowie IP-Adresse und Browserkennung. Rechtsgrundlagen sind Art. 6 Abs. 1 lit. b, c und f DSGVO. Unser berechtigtes Interesse ist der Nachweis des Vertragsschlusses. Diese Angaben löschen wir drei Jahre nach Vertragsende.
Die Zahlungen wickelt die Stripe Payments Europe, Limited, 1 Grand Canal Street Lower, Grand Canal Dock, Dublin, D02 H210, Irland („Stripe“) ab. Stripe verarbeitet die Daten teils in unserem Auftrag und teils als eigener Verantwortlicher, etwa zur Betrugsvorbeugung und zur Erfüllung gesetzlicher Pflichten. Dabei können Daten an die Stripe, LLC in den USA übermittelt werden. Die Stripe, LLC ist nach dem EU-US Data Privacy Framework zertifiziert, für das ein Angemessenheitsbeschluss der EU-Kommission besteht. Auf der Zahlungsseite von Stripe gelten dessen Datenschutzhinweise: stripe.com/de/privacy. Rechtsgrundlage ist Art. 6 Abs. 1 lit. b DSGVO.
+Beantragen Sie im Kundenportal eine Übernahmegutschrift, verarbeiten wir Ihre Angaben zum bisherigen Anbieter (Anbieter, Leistung, Domain, Rechnungsbetrag, bezahlter Zeitraum), Ihre Nachricht, die hochgeladenen Nachweise wie Rechnung oder Kündigungsbestätigung, die E-Mail-Adresse für unsere Antwort sowie Zeitpunkt und IP-Adresse Ihrer Bestätigung. Die Nachweise speichern wir auf unserem Server; unsere Mitarbeiter erhalten per E-Mail nur einen Hinweis auf den Antrag. Zur Prüfung fragen wir die öffentlichen DNS-Einträge der angegebenen Domain ab (Nameserver, Mailserver, IP-Adresse). Eine genehmigte Gutschrift buchen wir bei Stripe als Guthaben auf Ihr Kundenkonto. Rechtsgrundlage ist Art. 6 Abs. 1 lit. b DSGVO, für den Nachweis Ihrer Bestätigung Art. 6 Abs. 1 lit. f DSGVO. Nachweise zu abgelehnten Anträgen löschen wir sechs Monate nach der Entscheidung. Bei genehmigten Anträgen sind Antrag und Nachweise Beleg für die Gutschrift und werden wie in Abschnitt 9 beschrieben aufbewahrt.
Vertragsbestätigungen, Benachrichtigungen und unsere sonstige Korrespondenz versenden und empfangen wir derzeit über den E-Mail-Dienst der Porkbun LLC, 11575 SW Pacific Hwy PMB 40649, Tigard, OR 97223, USA. Dabei werden Inhalte und Verbindungsdaten der E-Mails in den USA verarbeitet. Für Porkbun besteht kein Angemessenheitsbeschluss der EU-Kommission. Die Übermittlung erfolgt, soweit sie zur Durchführung eines Vertrags mit Ihnen oder zur Beantwortung Ihrer Anfrage erforderlich ist (Art. 49 Abs. 1 lit. b DSGVO). Wir stellen unseren E-Mail-Dienst in Kürze auf die Hetzner Online GmbH (Rechenzentrum Nürnberg, Deutschland) um.
diff --git a/scripts/setup-abo-collections.mjs b/scripts/setup-abo-collections.mjs index da1654d..0b820f2 100644 --- a/scripts/setup-abo-collections.mjs +++ b/scripts/setup-abo-collections.mjs @@ -1,7 +1,8 @@ #!/usr/bin/env node /** - * Wartungs-Abo: legt die Collections subscriptions + hourCredits in woms-database an - * und ergaenzt worksheets um das Flag countsToPlan ("zaehlt aufs Abo-Stundenkonto"). + * Wartungs-Abo: legt die Collections subscriptions, hourCredits und takeoverCredits + * (Uebernahmegutschriften) in woms-database an und ergaenzt worksheets um das Flag + * countsToPlan ("zaehlt aufs Abo-Stundenkonto"). * Idempotent: existierende Collection/Attribute/Indexe werden uebersprungen. * Usage (im Container): docker exec -w /app webklar-kundenbereich-server node scripts/setup-abo-collections.mjs */ @@ -134,6 +135,44 @@ await ensureCollection( [{ key: 'idx_customer', type: 'key', attributes: ['customerId'] }] ) +// Uebernahmegutschrift: Antrag (Kunde) -> Pruefung/Entscheidung (Admin) -> Verrechnung (Stripe-Guthaben). +// Lange Texte mit size > 16381 werden in Appwrite zu TEXT-Spalten und zaehlen nicht zur Zeilenbreite. +await ensureCollection( + 'takeoverCredits', + 'Uebernahmegutschriften', + [ + { type: 'string', key: 'customerId', size: 64 }, + { type: 'string', key: 'email', size: 255 }, + { type: 'string', key: 'services', size: 128 }, + { type: 'string', key: 'provider', size: 200 }, + { type: 'string', key: 'domain', size: 255 }, + { type: 'integer', key: 'invoiceAmountCents' }, + { type: 'integer', key: 'billingMonths' }, + { type: 'datetime', key: 'paidUntil' }, + { type: 'string', key: 'message', size: 20000 }, + { type: 'string', key: 'files', size: 20000 }, + { type: 'string', key: 'status', size: 20 }, + { type: 'string', key: 'history', size: 60000 }, + { type: 'datetime', key: 'takeoverFrom' }, + { type: 'integer', key: 'amountCents' }, + { type: 'string', key: 'calculation', size: 1000 }, + { type: 'string', key: 'checks', size: 1000 }, + { type: 'string', key: 'decisionNote', size: 20000 }, + { type: 'datetime', key: 'decidedAt' }, + { type: 'string', key: 'decidedBy', size: 255 }, + { type: 'string', key: 'stripeCustomerId', size: 64 }, + { type: 'string', key: 'stripeBalanceTransactionId', size: 64 }, + { type: 'datetime', key: 'appliedAt' }, + { type: 'string', key: 'conditionsVersion', size: 32 }, + { type: 'datetime', key: 'consentAt' }, + { type: 'string', key: 'consentIp', size: 64 }, + ], + [ + { key: 'idx_customer', type: 'key', attributes: ['customerId'] }, + { key: 'idx_status', type: 'key', attributes: ['status'] }, + ] +) + // Worksheet-Flag: nur markierte Arbeitszeit wird vom Abo-Guthaben abgezogen await ensure('worksheets.countsToPlan', () => aw(`${col('worksheets')}/attributes/boolean`, { diff --git a/server/config.js b/server/config.js index 8ba41d4..7d06f75 100644 --- a/server/config.js +++ b/server/config.js @@ -1,4 +1,5 @@ import 'dotenv/config' +import path from 'node:path' /** Einzige erlaubte Datenbank (Ticketsystem). Keine separate Portal-DB mehr. */ export const WOMS_DATABASE_ID = 'woms-database' @@ -49,6 +50,7 @@ export const config = { portalMessages: process.env.APPWRITE_COLLECTION_PORTAL_MESSAGES || 'portalMessages', subscriptions: process.env.APPWRITE_COLLECTION_SUBSCRIPTIONS || 'subscriptions', hourCredits: process.env.APPWRITE_COLLECTION_HOUR_CREDITS || 'hourCredits', + takeoverCredits: process.env.APPWRITE_COLLECTION_TAKEOVER_CREDITS || 'takeoverCredits', }, publicBaseUrl: (process.env.PORTAL_PUBLIC_URL || 'https://project.webklar.com').replace(/\/$/, ''), @@ -94,6 +96,22 @@ export const config = { testCouponId: 'WEBKLAR-TEST-050', }, + // Uebernahmegutschrift: beim bisherigen Anbieter bereits bezahlte Zeit wird mit den Abo-Gebuehren verrechnet + takeover: { + // Bei jeder Textaenderung an den Bedingungen der Gutschrift hochziehen (wird mit dem Antrag gespeichert) + conditionsVersion: '2026-09-25', + // Nachweise liegen ausserhalb von public/ (gitignored, ueberlebt Deploys wie preview-data/) + uploadDir: process.env.TAKEOVER_UPLOAD_DIR || path.join(process.cwd(), 'upload-data', 'takeover-credits'), + maxFiles: 3, + maxFileBytes: 8 * 1024 * 1024, + maxTotalBytes: 10 * 1024 * 1024, + // Plausibilitaetsgrenze fuer eine einzelne Gutschrift (Tippfehler abfangen) + maxAmountCents: 100000, + maxOpenPerCustomer: 3, + // Nachweise abgelehnter Antraege werden nach dieser Frist geloescht (Datenschutzerklaerung Abschnitt 7) + rejectedRetentionDays: 183, + }, + smtp: { host: process.env.SMTP_HOST || '', port: Number(process.env.SMTP_PORT) || 587, diff --git a/server/index.js b/server/index.js index b91366b..59a40b7 100644 --- a/server/index.js +++ b/server/index.js @@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url' import { config, assertServerConfig, WOMS_DATABASE_ID } from './config.js' import { verifyDatabaseAccess } from './services/appwriteAdmin.js' import { syncPreviewTls } from './services/previewDeploy.js' -import { sessionMiddleware } from './middleware/session.js' +import { requireSession, sessionMiddleware } from './middleware/session.js' import authRoutes from './routes/auth.js' import projectsRoutes from './routes/projects.js' import featuresRoutes from './routes/features.js' @@ -12,6 +12,8 @@ import customerDataRoutes from './routes/customerData.js' import portalAdminRoutes from './routes/portalAdmin.js' import chatRoutes from './routes/chat.js' import aboRoutes from './routes/abo.js' +import takeoverCreditRoutes from './routes/takeoverCredits.js' +import { purgeExpiredUploads } from './services/takeoverCredits.js' import giteaWebhookRoutes from './routes/webhook/gitea.js' import adminGiteaRoutes from './routes/admin/gitea.js' import adminWebsiteProjectsRoutes from './routes/admin/websiteProjects.js' @@ -28,6 +30,10 @@ assertServerConfig() const app = express() app.set('trust proxy', 1) app.use(sessionMiddleware()) +// Uebernahmegutschrift: Nachweise kommen als Base64 im JSON (max. 10 MB Dateien) -> eigenes Limit, +// muss vor dem globalen Parser stehen (der ueberspringt bereits geparste Bodies). Login-Pruefung +// zuerst, damit niemand ohne Session grosse Bodies parsen laesst. +app.use('/api/abo/takeover-credits', requireSession, express.json({ limit: '15mb' })) // rawBody wird fuer die HMAC-Verifikation des Gitea-Webhooks benoetigt app.use(express.json({ limit: '2mb', verify: (req, _res, buf) => { req.rawBody = buf } })) @@ -49,6 +55,7 @@ app.use('/api/projects', projectsRoutes) app.use('/api/features', featuresRoutes) app.use('/api', customerDataRoutes) app.use('/api/chat', chatRoutes) +app.use('/api/abo/takeover-credits', takeoverCreditRoutes) app.use('/api/abo', aboRoutes) app.use('/api/portal-admin', portalAdminRoutes) app.use('/api/admin/gitea', adminGiteaRoutes) @@ -103,6 +110,14 @@ const server = app.listen(config.port, '0.0.0.0', () => { ) if (result.reason) console.error(`[startup] Appwrite: ${result.reason}`) }) + + // Nachweise abgelehnter Uebernahmegutschriften nach Ablauf der Aufbewahrungsfrist loeschen (taeglich) + const purge = () => + purgeExpiredUploads() + .then((r) => r.purged && console.log(`[takeover] Nachweise von ${r.purged} abgelehnten Anträgen gelöscht`)) + .catch((err) => console.warn('[takeover] Aufräumen fehlgeschlagen:', err.message)) + setTimeout(purge, 60 * 1000).unref() + setInterval(purge, 24 * 60 * 60 * 1000).unref() }) server.on('error', (err) => { diff --git a/server/routes/portalAdmin.js b/server/routes/portalAdmin.js index df9af7f..4bcb1b9 100644 --- a/server/routes/portalAdmin.js +++ b/server/routes/portalAdmin.js @@ -17,11 +17,15 @@ import { listAppointments, saveAppointment } from '../services/appointments.js' import { getCustomerActivity } from '../services/customerActivity.js' import { listInvoicesForClient } from '../services/invoiceNinja.js' import { sanitizeMessage } from './chat.js' +import { takeoverAdminRouter } from './takeoverCredits.js' const router = Router() router.use(requireAdminSession) +// Uebernahmegutschriften: Pruefung und Entscheidung (Tab "Gutschriften") +router.use('/takeover-credits', takeoverAdminRouter) + function norm(value) { return String(value || '').toLowerCase() } diff --git a/server/routes/takeoverCredits.js b/server/routes/takeoverCredits.js new file mode 100644 index 0000000..95f0015 --- /dev/null +++ b/server/routes/takeoverCredits.js @@ -0,0 +1,224 @@ +import { Router } from 'express' +import { config } from '../config.js' +import { getSessionCustomerId, requireSession } from '../middleware/session.js' +import { getDocument, listDocuments, Query } from '../services/appwriteAdmin.js' +import { + answerQuestion, + applyTakeoverCredit, + calculationText, + computeTakeoverCredit, + createTakeoverRequest, + customerView, + decideTakeoverRequest, + getAdminDetail, + getTakeoverRequest, + listTakeoverRequests, + parseUploads, + readInvoiceFigures, + readUpload, + servicesLabel, + shortRef, + STATUS, + validateRequestInput, +} from '../services/takeoverCredits.js' + +/** + * Uebernahmegutschrift. + * Kunde: /api/abo/takeover-credits (Antrag stellen, Rueckfrage beantworten) + * Admin: /api/portal-admin/takeover-credits (Liste, Pruefung, Entscheidung, Nachweise) + * Der JSON-Body darf hier groesser sein (Nachweise als Base64), siehe server/index.js. + */ + +// Missbrauchsschutz: je Kunde hoechstens 20 Versuche pro Stunde (zusaetzlich max. 3 offene Antraege) +const recentByCustomer = new Map() +function rateLimited(customerId) { + const now = Date.now() + const list = (recentByCustomer.get(customerId) || []).filter((t) => now - t < 60 * 60 * 1000) + list.push(now) + recentByCustomer.set(customerId, list) + return list.length > 20 +} + +function sendError(res, err, fallback) { + const status = err.status || 500 + if (status >= 500) console.error('[takeover]', fallback, err.message) + return res.status(status).json({ error: err.message || fallback }) +} + +// --------------------------------------------------------------------------- +// Kunde (eingeloggt) +// --------------------------------------------------------------------------- + +const router = Router() +router.use(requireSession) + +router.get('/', async (req, res) => { + try { + const docs = await listTakeoverRequests({ customerId: getSessionCustomerId(req) }) + return res.json({ + requests: docs.map(customerView), + conditionsVersion: config.takeover.conditionsVersion, + limits: { + maxFiles: config.takeover.maxFiles, + maxFileBytes: config.takeover.maxFileBytes, + maxTotalBytes: config.takeover.maxTotalBytes, + }, + }) + } catch (err) { + return sendError(res, err, 'Anträge konnten nicht geladen werden') + } +}) + +router.post('/', async (req, res) => { + if (req.session.viewAs) { + return res.status(403).json({ error: 'Im "Als Kunde ansehen"-Modus kann kein Antrag gestellt werden.' }) + } + const customerId = getSessionCustomerId(req) + if (rateLimited(customerId)) { + return res.status(429).json({ error: 'Zu viele Anträge in kurzer Zeit. Bitte später erneut versuchen.' }) + } + try { + const input = validateRequestInput(req.body || {}) + const uploads = parseUploads(req.body.files, { required: true }) + const customer = await getDocument(config.collections.customers, customerId) + const doc = await createTakeoverRequest({ customer, input, uploads, consentIp: req.ip }) + return res.status(201).json({ request: customerView(doc) }) + } catch (err) { + return sendError(res, err, 'Antrag konnte nicht gespeichert werden') + } +}) + +router.post('/:id/reply', async (req, res) => { + if (req.session.viewAs) { + return res.status(403).json({ error: 'Im "Als Kunde ansehen"-Modus nicht möglich.' }) + } + const customerId = getSessionCustomerId(req) + if (rateLimited(customerId)) { + return res.status(429).json({ error: 'Zu viele Anfragen in kurzer Zeit. Bitte später erneut versuchen.' }) + } + try { + const doc = await getTakeoverRequest(req.params.id).catch(() => null) + if (!doc || doc.customerId !== customerId) return res.status(404).json({ error: 'Antrag nicht gefunden' }) + const uploads = parseUploads(req.body?.files, { required: false }) + const customer = await getDocument(config.collections.customers, customerId) + const updated = await answerQuestion({ customer, doc, message: req.body?.message, uploads }) + return res.json({ request: customerView(updated) }) + } catch (err) { + return sendError(res, err, 'Antwort konnte nicht gespeichert werden') + } +}) + +export default router + +// --------------------------------------------------------------------------- +// Admin (eingehaengt in routes/portalAdmin.js, dort hinter requireAdminSession) +// --------------------------------------------------------------------------- + +export const takeoverAdminRouter = Router() + +takeoverAdminRouter.get('/', async (req, res) => { + try { + const [docs, customers] = await Promise.all([ + listTakeoverRequests(), + listDocuments(config.collections.customers, [Query.limit(1000)]), + ]) + const byId = new Map(customers.map((c) => [c.$id, c])) + const onlyOpen = req.query.status !== 'all' + const requests = docs + .filter((d) => !onlyOpen || d.status === STATUS.open || d.status === STATUS.question) + .map((d) => { + const c = byId.get(d.customerId) + return { + id: d.$id, + ref: shortRef(d.$id), + createdAt: d.$createdAt, + customerId: d.customerId, + customerName: c?.name || c?.companyName || d.email, + customerCode: c?.code || '', + services: servicesLabel(d.services), + provider: d.provider || '', + domain: d.domain || '', + invoiceAmountCents: d.invoiceAmountCents || 0, + billingMonths: d.billingMonths || 0, + paidUntil: d.paidUntil || null, + status: d.status, + amountCents: d.amountCents ?? null, + appliedAt: d.appliedAt || null, + } + }) + return res.json({ requests, openCount: docs.filter((d) => d.status === STATUS.open).length }) + } catch (err) { + return sendError(res, err, 'Anträge konnten nicht geladen werden') + } +}) + +/** Rechner im Pruefformular: dieselbe Berechnung wie beim Genehmigen (eine Quelle der Wahrheit). */ +takeoverAdminRouter.post('/calc', (req, res) => { + try { + const figures = readInvoiceFigures(req.body, {}) + const calc = computeTakeoverCredit({ ...figures, takeoverFrom: req.body?.takeoverFrom }) + if (!calc) return res.status(400).json({ error: 'Bitte das Datum „Übernahme ab“ angeben.' }) + return res.json({ calc, text: calculationText(figures, calc, calc.suggestedCents) }) + } catch (err) { + return sendError(res, err, 'Berechnung fehlgeschlagen') + } +}) + +takeoverAdminRouter.get('/open-count', async (_req, res) => { + try { + const docs = await listTakeoverRequests() + return res.json({ open: docs.filter((d) => d.status === STATUS.open).length }) + } catch { + return res.json({ open: 0 }) + } +}) + +takeoverAdminRouter.get('/:id', async (req, res) => { + try { + return res.json(await getAdminDetail(req.params.id)) + } catch (err) { + return sendError(res, err, 'Antrag konnte nicht geladen werden') + } +}) + +takeoverAdminRouter.get('/:id/files/:fileId', async (req, res) => { + try { + const doc = await getTakeoverRequest(req.params.id) + const { file, content } = await readUpload(doc, req.params.fileId) + res.setHeader('Content-Type', file.type) + res.setHeader('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(file.name)}`) + res.setHeader('X-Content-Type-Options', 'nosniff') + res.setHeader('Cache-Control', 'private, no-store') + return res.send(content) + } catch (err) { + return res.status(err.status || 500).send(err.message || 'Datei nicht verfügbar') + } +}) + +takeoverAdminRouter.post('/:id/decision', async (req, res) => { + try { + const admin = { name: req.session.adminName || req.session.name, email: req.session.adminEmail || req.session.email } + const result = await decideTakeoverRequest(req.params.id, req.body || {}, admin) + return res.json({ + success: true, + status: result.doc.status, + applied: result.applied ?? null, + reason: result.reason || '', + mailSent: result.mail?.sent ?? false, + mailError: result.mail?.reason || '', + }) + } catch (err) { + return sendError(res, err, 'Entscheidung konnte nicht gespeichert werden') + } +}) + +/** Genehmigte, aber noch nicht verrechnete Gutschrift erneut an Stripe senden. */ +takeoverAdminRouter.post('/:id/apply', async (req, res) => { + try { + const doc = await getTakeoverRequest(req.params.id) + const result = await applyTakeoverCredit(doc) + return res.json({ success: result.applied, applied: result.applied, reason: result.reason || '' }) + } catch (err) { + return sendError(res, err, 'Verrechnung fehlgeschlagen') + } +}) diff --git a/server/services/stripeBilling.js b/server/services/stripeBilling.js index 3e7429b..e0f6901 100644 --- a/server/services/stripeBilling.js +++ b/server/services/stripeBilling.js @@ -243,7 +243,9 @@ export async function grantCreditForInvoice(invoice) { const ref = subscriptionIdOfInvoice(invoice) const subscriptionId = typeof ref === 'string' ? ref : ref?.id if (!subscriptionId) return { skipped: 'keine Abo-Rechnung' } - if (!(invoice.amount_paid > 0)) return { skipped: 'Betrag 0' } + // total statt amount_paid: Eine Monatsgebuehr, die ein Guthaben (Uebernahmegutschrift) deckt, + // ist bezahlt, obwohl nichts abgebucht wurde. Nur Probezeit-Rechnungen (0 €) zaehlen nicht. + if (!(invoice.total > 0)) return { skipped: 'Betrag 0' } const sub = await syncSubscription(subscriptionId) if (!sub) return { skipped: 'Abo ohne Kundenzuordnung' } diff --git a/server/services/takeoverCredits.js b/server/services/takeoverCredits.js new file mode 100644 index 0000000..f20dba5 --- /dev/null +++ b/server/services/takeoverCredits.js @@ -0,0 +1,971 @@ +import { createHash, randomUUID } from 'node:crypto' +import { Resolver } from 'node:dns/promises' +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises' +import path from 'node:path' +import { domainToASCII, domainToUnicode } from 'node:url' +import { config } from '../config.js' +import { createDocument, getDocument, listDocuments, Query, updateDocument } from './appwriteAdmin.js' +import { sendAdminNotice, sendCustomerMail } from './mailer.js' +import { ACTIVE_STATUSES, getCurrentSubscription, getStripe, isStripeConfigured } from './stripeBilling.js' + +/** + * Uebernahmegutschrift. + * + * Hat ein Abo-Kunde Domain, Hosting oder E-Mail beim bisherigen Anbieter schon im Voraus + * bezahlt, zahlt er ab Leistungsbeginn des Abos fuer dieselbe Zeit doppelt. Diesen Anteil + * schreiben wir ihm gut: + * Antrag (Kunde, mit Rechnungsnachweis, nur mit laufendem Abo) + * -> Pruefung (Admin: automatische Checks + Pflicht-Checkliste) + * -> Entscheidung: Genehmigen / Rueckfrage / Ablehnen, Antwort per E-Mail an den Kunden + * -> Verrechnung als Stripe-Kundenguthaben (negative Balance Transaction), das Stripe + * automatisch mit den naechsten Abo-Rechnungen verrechnet. + * Die Nachweise liegen auf dem Server (config.takeover.uploadDir), nie unter public/. + */ + +const COLLECTION = () => config.collections.takeoverCredits + +export const STATUS = { + open: 'offen', + question: 'rueckfrage', + approved: 'genehmigt', + rejected: 'abgelehnt', +} + +export const SERVICE_LABELS = { + domain: 'Domain', + hosting: 'Hosting / Website-Betrieb', + email: 'E-Mail-Postfach', + sonstiges: 'Sonstiges', +} + +export const BILLING_MONTHS = [1, 3, 6, 12, 24, 36] + +// Pflicht-Checkliste vor dem Genehmigen (wird mit Name und Zeitpunkt am Antrag gespeichert) +export const REQUIRED_CHECKS = [ + { key: 'recipient', label: 'Rechnung ist auf den Kunden ausgestellt (Name bzw. Firma stimmt)' }, + { key: 'service', label: 'Leistung und Domain auf der Rechnung stimmen mit dem Antrag überein' }, + { key: 'period', label: 'Betrag und bezahlter Zeitraum stimmen mit der Rechnung überein' }, + { key: 'replaced', label: 'Die Leistung wird durch das Wartungs-Abo ersetzt (Umzug zu WEBklar)' }, +] + +const FOOTER = 'WEBklar GbR · Am Schwimmbad 10 · 67722 Winnweiler · Telefon 0176 23726355 · support@webklar.com' +const DAY_MS = 24 * 60 * 60 * 1000 + +function httpError(status, message) { + const err = new Error(message) + err.status = status + return err +} + +// --------------------------------------------------------------------------- +// Formatierung +// --------------------------------------------------------------------------- + +const fmtEuro = (cents) => + (Number(cents || 0) / 100).toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }) + +const fmtDate = (iso) => + iso + ? new Date(iso).toLocaleDateString('de-DE', { timeZone: 'Europe/Berlin', day: '2-digit', month: '2-digit', year: 'numeric' }) + : '–' + +const fmtDateTime = (d) => + new Date(d).toLocaleString('de-DE', { timeZone: 'Europe/Berlin', dateStyle: 'medium', timeStyle: 'short' }) + +export function servicesLabel(services) { + return String(services || '') + .split(',') + .filter(Boolean) + .map((s) => SERVICE_LABELS[s] || s) + .join(', ') +} + +const monthsLabel = (m) => (Number(m) === 1 ? '1 Monat' : `${m} Monate`) + +/** Kurze, fuer Kunden lesbare Antragsnummer. */ +export const shortRef = (id) => String(id || '').slice(0, 8).toUpperCase() + +// --------------------------------------------------------------------------- +// Berechnung (reine Funktionen) +// --------------------------------------------------------------------------- + +/** Kalendertag in Berlin als UTC-Mitternacht ('YYYY-MM-DD' oder ISO-Zeitpunkt). */ +function dayOf(value) { + if (!value) return null + const raw = String(value) + const ymd = /^\d{4}-\d{2}-\d{2}$/.test(raw) + ? raw + : new Date(raw).toLocaleDateString('sv-SE', { timeZone: 'Europe/Berlin' }) + const d = new Date(`${ymd}T00:00:00Z`) + return Number.isNaN(d.getTime()) ? null : d +} + +function addDaysUtc(date, days) { + return new Date(date.getTime() + days * DAY_MS) +} + +function addMonthsUtc(date, months) { + // Monatsende abfangen (31.03. - 1 Monat = 28./29.02.) + const d = new Date(date) + const day = d.getUTCDate() + d.setUTCDate(1) + d.setUTCMonth(d.getUTCMonth() + months) + const last = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 0)).getUTCDate() + d.setUTCDate(Math.min(day, last)) + return d +} + +const daysBetween = (a, b) => Math.round((b.getTime() - a.getTime()) / DAY_MS) +const ymd = (d) => d.toISOString().slice(0, 10) + +/** + * Anteilige Gutschrift, tagesgenau: + * Rechnungszeitraum = die letzten `billingMonths` Monate bis einschliesslich `paidUntil` + * doppelt bezahlt = ab `takeoverFrom` (Leistungsbeginn des Abos) bis `paidUntil` + * Vorschlag = Rechnungsbetrag * doppelt bezahlte Tage / Tage im Rechnungszeitraum + */ +export function computeTakeoverCredit({ invoiceAmountCents, billingMonths, paidUntil, takeoverFrom }) { + const amount = Math.round(Number(invoiceAmountCents) || 0) + const months = Math.round(Number(billingMonths) || 0) + const lastPaidDay = dayOf(paidUntil) + const from = dayOf(takeoverFrom) + if (!(amount > 0) || !(months > 0) || !lastPaidDay || !from) return null + + const periodEndExclusive = addDaysUtc(lastPaidDay, 1) + const periodStart = addMonthsUtc(periodEndExclusive, -months) + const periodDays = Math.max(1, daysBetween(periodStart, periodEndExclusive)) + const overlapStart = from > periodStart ? from : periodStart + const overlapDays = Math.max(0, daysBetween(overlapStart, periodEndExclusive)) + return { + periodStart: ymd(periodStart), + periodEnd: ymd(lastPaidDay), + periodDays, + overlapStart: ymd(overlapStart), + overlapDays, + suggestedCents: Math.round((amount * overlapDays) / periodDays), + } +} + +/** Rechnungswerte aus dem Admin-Rechner (fehlende Felder: Angaben aus dem Antrag). */ +export function readInvoiceFigures(body, doc) { + const invoiceAmountCents = + body?.invoiceAmountCents == null ? doc.invoiceAmountCents : Math.round(Number(body.invoiceAmountCents)) + const billingMonths = body?.billingMonths == null ? doc.billingMonths : Number(body.billingMonths) + const paidUntil = body?.paidUntil ? dayOf(body.paidUntil) : dayOf(doc.paidUntil) + if (!(invoiceAmountCents > 0) || invoiceAmountCents > 500000) throw httpError(400, 'Bitte einen gültigen Rechnungsbetrag angeben.') + if (!BILLING_MONTHS.includes(billingMonths)) throw httpError(400, 'Bitte den Abrechnungszeitraum der Rechnung wählen.') + if (!paidUntil) throw httpError(400, 'Bitte angeben, bis wann bezahlt ist.') + return { invoiceAmountCents, billingMonths, paidUntil: paidUntil.toISOString() } +} + +export function calculationText(doc, calc, amountCents) { + if (!calc) return '' + const base = `${fmtEuro(doc.invoiceAmountCents)} für ${monthsLabel(doc.billingMonths)} (${fmtDate(calc.periodStart)} – ${fmtDate(calc.periodEnd)}), davon ${calc.overlapDays} von ${calc.periodDays} Tagen ab ${fmtDate(calc.overlapStart)} doppelt bezahlt = ${fmtEuro(calc.suggestedCents)}` + return amountCents === calc.suggestedCents ? base : `${base}; festgesetzt auf ${fmtEuro(amountCents)}` +} + +// --------------------------------------------------------------------------- +// Eingaben pruefen +// --------------------------------------------------------------------------- + +const clean = (value, max) => String(value ?? '').trim().slice(0, max) + +export function normalizeDomain(value) { + const raw = String(value || '').trim().toLowerCase() + if (!raw) return '' + const host = raw + .replace(/^[a-z]+:\/\//, '') + .replace(/[/?#].*$/, '') + .replace(/:\d+$/, '') + .replace(/^www\./, '') + .replace(/\.$/, '') + // Umlaut-Domains: Pruefung in Punycode, gespeichert wird die lesbare Schreibweise + const ascii = domainToASCII(host) + const valid = /^(?=.{3,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{1,62}$/.test(ascii) + return valid ? domainToUnicode(ascii) : null +} + +/** "119,88" / "1.234,56" / "119.88" / 119.88 -> Cent */ +export function parseEuroCents(value) { + if (typeof value === 'number') return Number.isFinite(value) ? Math.round(value * 100) : NaN + let s = String(value ?? '').replace(/[\s€]/g, '') + if (s.includes(',')) s = s.replace(/\./g, '').replace(',', '.') + if (!/^\d+(\.\d{1,2})?$/.test(s)) return NaN + return Math.round(Number(s) * 100) +} + +const isEmail = (v) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) + +export function validateRequestInput(body) { + const services = [...new Set(Array.isArray(body?.services) ? body.services : [])].filter((s) => Object.hasOwn(SERVICE_LABELS, s)) + if (!services.length) throw httpError(400, 'Bitte wählen Sie, welche Leistung Sie bisher woanders bezahlt haben.') + + const provider = clean(body.provider, 200) + if (!provider) throw httpError(400, 'Bitte nennen Sie Ihren bisherigen Anbieter.') + + const domain = normalizeDomain(body.domain) + if (domain === null) throw httpError(400, 'Die Domain sieht nicht gültig aus (Beispiel: meine-firma.de).') + if (!domain && services.includes('domain')) throw httpError(400, 'Bitte geben Sie die Domain an.') + + const invoiceAmountCents = parseEuroCents(body.invoiceAmount) + if (!(invoiceAmountCents > 0) || invoiceAmountCents > 500000) { + throw httpError(400, 'Bitte geben Sie den Rechnungsbetrag in Euro an.') + } + + const billingMonths = Number(body.billingMonths) + if (!BILLING_MONTHS.includes(billingMonths)) throw httpError(400, 'Bitte wählen Sie den Abrechnungszeitraum der Rechnung.') + + const paidUntilRaw = String(body.paidUntil || '') + const paidUntil = /^\d{4}-\d{2}-\d{2}$/.test(paidUntilRaw) ? dayOf(paidUntilRaw) : null + const now = Date.now() + if (!paidUntil || paidUntil.getTime() < now - 366 * DAY_MS || paidUntil.getTime() > now + 3 * 366 * DAY_MS) { + throw httpError(400, 'Bitte geben Sie an, bis wann Sie beim bisherigen Anbieter bezahlt haben.') + } + + const message = clean(body.message, 4000) + if (message.length < 5) throw httpError(400, 'Bitte beschreiben Sie kurz den Grund Ihres Antrags.') + + const email = clean(body.email, 255) + if (!isEmail(email)) throw httpError(400, 'Bitte geben Sie eine gültige E-Mail-Adresse für unsere Antwort an.') + + if (body.consent !== true) { + throw httpError(400, 'Bitte bestätigen Sie Ihre Angaben und die Bedingungen der Übernahmegutschrift.') + } + + return { + services, + provider, + domain, + invoiceAmountCents, + billingMonths, + paidUntil: paidUntil.toISOString(), + message, + email, + } +} + +// --------------------------------------------------------------------------- +// Nachweise (Dateien) +// --------------------------------------------------------------------------- + +const FILE_TYPES = [ + // PDF-Kopf darf laut Spezifikation innerhalb der ersten 1024 Bytes stehen + { mime: 'application/pdf', ext: 'pdf', test: (b) => b.subarray(0, 1024).includes('%PDF-') }, + { mime: 'image/png', ext: 'png', test: (b) => b.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) }, + { mime: 'image/jpeg', ext: 'jpg', test: (b) => b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff }, +] + +function safeFileName(name, ext) { + const base = String(name || '') + .split(/[\\/]/) + .pop() + .replace(/[\u0000-\u001f"<>|:*?]/g, '') + .trim() + .slice(0, 120) + if (!base) return `nachweis.${ext}` + return /\.[a-z0-9]{2,5}$/i.test(base) ? base : `${base}.${ext}` +} + +/** Base64-Uploads aus dem JSON-Body pruefen: Anzahl, Groesse, echter Dateityp (Magic Bytes). */ +export function parseUploads(list, { required }) { + const items = Array.isArray(list) ? list : [] + const { maxFiles, maxFileBytes, maxTotalBytes } = config.takeover + if (required && !items.length) throw httpError(400, 'Bitte laden Sie die Rechnung Ihres bisherigen Anbieters hoch.') + if (items.length > maxFiles) throw httpError(400, `Bitte höchstens ${maxFiles} Dateien auf einmal hochladen.`) + let total = 0 + return items.map((item) => { + const buffer = Buffer.from(String(item?.data || ''), 'base64') + const label = clean(item?.name, 120) || 'Datei' + if (!buffer.length) throw httpError(400, `„${label}“ ist leer.`) + if (buffer.length > maxFileBytes) throw httpError(413, `„${label}“ ist größer als ${maxFileBytes / 1024 / 1024} MB.`) + total += buffer.length + if (total > maxTotalBytes) throw httpError(413, `Die Dateien sind zusammen größer als ${maxTotalBytes / 1024 / 1024} MB.`) + const kind = FILE_TYPES.find((t) => t.test(buffer)) + if (!kind) throw httpError(400, `„${label}“: Bitte als PDF, JPG oder PNG hochladen.`) + return { + id: randomUUID(), + name: safeFileName(item?.name, kind.ext), + type: kind.mime, + ext: kind.ext, + size: buffer.length, + sha256: createHash('sha256').update(buffer).digest('hex'), + buffer, + } + }) +} + +function uploadPath(requestId, file) { + if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,35}$/.test(String(requestId)) || !/^[0-9a-f-]{36}$/.test(String(file?.id))) { + throw httpError(400, 'Ungültiger Dateiverweis') + } + return path.join(config.takeover.uploadDir, requestId, `${file.id}.${file.ext || 'bin'}`) +} + +async function storeUploads(requestId, uploads, uploadedAt) { + if (!uploads.length) return [] + await mkdir(path.join(config.takeover.uploadDir, requestId), { recursive: true, mode: 0o700 }) + const stored = [] + for (const { buffer, ...meta } of uploads) { + await writeFile(uploadPath(requestId, meta), buffer, { mode: 0o600 }) + stored.push({ ...meta, uploadedAt }) + } + return stored +} + +export async function readUpload(doc, fileId) { + const file = filesOf(doc).find((f) => f.id === fileId) + if (!file) throw httpError(404, 'Datei nicht gefunden') + const content = await readFile(uploadPath(doc.$id, file)).catch(() => null) + if (!content) throw httpError(404, 'Datei ist nicht mehr vorhanden') + return { file, content } +} + +// --------------------------------------------------------------------------- +// Dokument-Helfer +// --------------------------------------------------------------------------- + +function parseJson(value, fallback) { + try { + return JSON.parse(value || '') ?? fallback + } catch { + return fallback + } +} + +export const filesOf = (doc) => parseJson(doc?.files, []) +export const historyOf = (doc) => parseJson(doc?.history, []) +export const checksOf = (doc) => parseJson(doc?.checks, null) + +/** Verlauf anhaengen; sehr lange Verlaeufe kuerzen, damit das Attribut (60.000 Zeichen) nie ueberlaeuft. */ +function withHistory(doc, entry) { + const list = [...historyOf(doc), { at: new Date().toISOString(), ...entry }] + let json = JSON.stringify(list) + for (let i = 0; json.length > 58000 && i < list.length - 1; i += 1) { + list[i] = { ...list[i], text: String(list[i].text || '').slice(0, 200) } + json = JSON.stringify(list) + } + return json +} + +const serviceStartOf = (sub) => sub?.trialEnd || sub?.startedAt || null +const isActiveSub = (sub) => Boolean(sub && ACTIVE_STATUSES.has(sub.status)) + +export async function getTakeoverRequest(id) { + return getDocument(COLLECTION(), id) +} + +export async function listTakeoverRequests({ customerId } = {}) { + const queries = [Query.orderDesc('$createdAt'), Query.limit(1000)] + if (customerId) queries.unshift(Query.equal('customerId', customerId)) + return listDocuments(COLLECTION(), queries) +} + +/** Ansicht fuer den Kunden: keine Pruefdetails, keine internen Notizen. */ +export function customerView(doc) { + const history = historyOf(doc) + const lastQuestion = [...history].reverse().find((h) => h.type === 'rueckfrage') + return { + id: doc.$id, + ref: shortRef(doc.$id), + createdAt: doc.$createdAt, + services: servicesLabel(doc.services), + provider: doc.provider || '', + domain: doc.domain || '', + invoiceAmountCents: doc.invoiceAmountCents || 0, + billingMonths: doc.billingMonths || 0, + paidUntil: doc.paidUntil || null, + status: doc.status, + question: doc.status === STATUS.question ? lastQuestion?.text || '' : '', + amountCents: doc.status === STATUS.approved ? doc.amountCents || 0 : 0, + decisionNote: [STATUS.approved, STATUS.rejected].includes(doc.status) ? doc.decisionNote || '' : '', + } +} + +// --------------------------------------------------------------------------- +// Kunde: Antrag stellen, Rueckfrage beantworten +// --------------------------------------------------------------------------- + +export async function createTakeoverRequest({ customer, input, uploads, consentIp }) { + const sub = await getCurrentSubscription(customer.$id) + if (!isActiveSub(sub)) { + throw httpError(409, 'Die Übernahmegutschrift können Sie beantragen, sobald Ihr Wartungs-Abo läuft.') + } + const own = await listTakeoverRequests({ customerId: customer.$id }) + const open = own.filter((r) => r.status === STATUS.open || r.status === STATUS.question) + if (open.length >= config.takeover.maxOpenPerCustomer) { + throw httpError(429, 'Sie haben bereits mehrere offene Anträge. Bitte warten Sie unsere Antwort ab.') + } + + const id = randomUUID() + const now = new Date().toISOString() + const files = await storeUploads(id, uploads, now) + let doc + try { + doc = await createDocument( + COLLECTION(), + { + customerId: customer.$id, + email: input.email, + services: input.services.join(','), + provider: input.provider, + domain: input.domain, + invoiceAmountCents: input.invoiceAmountCents, + billingMonths: input.billingMonths, + paidUntil: input.paidUntil, + message: input.message, + files: JSON.stringify(files), + status: STATUS.open, + history: JSON.stringify([ + { at: now, type: 'eingereicht', by: 'customer', text: '', files: files.map((f) => f.id) }, + ]), + conditionsVersion: config.takeover.conditionsVersion, + consentAt: now, + consentIp: String(consentIp || '').slice(0, 64), + }, + id + ) + } catch (err) { + await rm(path.join(config.takeover.uploadDir, id), { recursive: true, force: true }).catch(() => {}) + throw err + } + + const name = customer.name || customer.companyName || input.email + const summary = [ + ` Antrag: ${shortRef(doc.$id)}`, + ` Leistung: ${servicesLabel(doc.services)}`, + ` Bisher bei: ${doc.provider}`, + doc.domain ? ` Domain: ${doc.domain}` : null, + ` Rechnung: ${fmtEuro(doc.invoiceAmountCents)} für ${monthsLabel(doc.billingMonths)}`, + ` Bezahlt bis: ${fmtDate(doc.paidUntil)}`, + ` Nachweise: ${files.map((f) => f.name).join(', ')}`, + ].filter((l) => l !== null) + + await sendCustomerMail(input.email, 'Eingangsbestätigung: Ihr Antrag auf Übernahmegutschrift – WEBklar', [ + `Guten Tag ${customer.name || ''}`.trim() + ',', + '', + `wir haben Ihren Antrag auf eine Übernahmegutschrift am ${fmtDateTime(now)} erhalten.`, + '', + ...summary, + '', + 'Wir prüfen Ihre Angaben und den Nachweis und antworten Ihnen per E-Mail an diese Adresse.', + `Den Stand Ihres Antrags sehen Sie auch im Kundenportal: ${config.publicBaseUrl}/dashboard.html`, + '', + 'Viele Grüße', + 'Ihr WEBklar-Team', + '', + FOOTER, + ]).catch((e) => console.error('[takeover] Eingangsbestaetigung fehlgeschlagen:', e.message)) + + // Nachweise bewusst nicht als Anhang: sie bleiben auf dem Server (Datenschutzerklaerung Abschnitt 7) + await sendAdminNotice(`Übernahmegutschrift beantragt: ${name}`, [ + `${name}${customer.code ? ` (Kd.-Nr. ${customer.code})` : ''} hat eine Übernahmegutschrift beantragt.`, + `Antwort an: ${input.email}`, + '', + ...summary, + '', + 'Grund / Nachricht des Kunden:', + input.message, + '', + `Nachweise ansehen, prüfen und entscheiden: ${config.publicBaseUrl}/admin.html?gutschrift=${encodeURIComponent(doc.$id)}`, + ]) + + return doc +} + +export async function answerQuestion({ customer, doc, message, uploads }) { + if (doc.customerId !== customer.$id) throw httpError(404, 'Antrag nicht gefunden') + if (doc.status !== STATUS.question) throw httpError(409, 'Zu diesem Antrag ist keine Rückfrage offen.') + const text = clean(message, 4000) + if (!text && !uploads.length) throw httpError(400, 'Bitte schreiben Sie eine Antwort oder laden Sie einen Nachweis hoch.') + + const now = new Date().toISOString() + const added = await storeUploads(doc.$id, uploads, now) + const updated = await updateDocument(COLLECTION(), doc.$id, { + status: STATUS.open, + files: JSON.stringify([...filesOf(doc), ...added]), + history: withHistory(doc, { type: 'antwort', by: 'customer', text, files: added.map((f) => f.id) }), + }) + + const name = customer.name || customer.companyName || doc.email + await sendAdminNotice(`Übernahmegutschrift: Antwort von ${name}`, [ + `${name} hat auf die Rückfrage zum Antrag ${shortRef(doc.$id)} geantwortet:`, + '', + text || '(ohne Text)', + ...(added.length ? ['', `Neue Nachweise: ${added.map((f) => f.name).join(', ')}`] : []), + '', + `Prüfen und entscheiden: ${config.publicBaseUrl}/admin.html?gutschrift=${encodeURIComponent(doc.$id)}`, + ]) + return updated +} + +// --------------------------------------------------------------------------- +// Automatische Pruefungen (Hinweise fuer den Admin, keine Entscheidung) +// --------------------------------------------------------------------------- + +// Nameserver-/MX-Hosts bekannter Anbieter -> Name; Aliase fuer den Abgleich mit der Angabe im Antrag +const PROVIDERS = [ + { name: 'IONOS (1&1)', hosts: /(^|\.)(ui-dns\.(de|com|org|biz)|ionos\.[a-z]+|kundenserver\.de|1and1\.[a-z]+|schlund\.de)$/, aliases: ['ionos', '1und1', '1and1', '11', 'united internet'] }, + { name: 'STRATO', hosts: /(^|\.)(rzone\.de|strato\.[a-z]+)$/, aliases: ['strato'] }, + { name: 'Hetzner', hosts: /(^|\.)(hetzner\.(com|de)|your-server\.de|second-ns\.(de|com))$/, aliases: ['hetzner'] }, + { name: 'ALL-INKL.COM', hosts: /(^|\.)(kasserver\.com|all-inkl\.com)$/, aliases: ['allinkl', 'kasserver'] }, + { name: 'netcup', hosts: /(^|\.)netcup\.(net|de)$/, aliases: ['netcup'] }, + { name: 'Hostinger', hosts: /(^|\.)(hostinger\.[a-z]+|dns-parking\.com)$/, aliases: ['hostinger'] }, + { name: 'GoDaddy', hosts: /(^|\.)(domaincontrol\.com|secureserver\.net)$/, aliases: ['godaddy', 'host europe', 'hosteurope'] }, + { name: 'Host Europe', hosts: /(^|\.)hosteurope\.de$/, aliases: ['hosteurope'] }, + { name: 'Cloudflare', hosts: /(^|\.)cloudflare\.com$/, aliases: ['cloudflare'] }, + { name: 'Google', hosts: /(^|\.)(google\.com|googledomains\.com)$/, aliases: ['google', 'workspace', 'gsuite'] }, + { name: 'Microsoft 365', hosts: /(^|\.)(outlook\.com|office365\.com)$/, aliases: ['microsoft', 'office', 'outlook', 'm365'] }, + { name: 'united-domains', hosts: /(^|\.)(udag\.(de|net|org)|united-domains\.de)$/, aliases: ['uniteddomains', 'udag'] }, + { name: 'domainfactory', hosts: /(^|\.)(namespace4you\.de|ispgateway\.de|df\.eu)$/, aliases: ['domainfactory', 'df'] }, + { name: 'one.com', hosts: /(^|\.)one\.com$/, aliases: ['onecom'] }, + { name: 'OVH', hosts: /(^|\.)ovh\.(net|de|com)$/, aliases: ['ovh'] }, + { name: 'checkdomain', hosts: /(^|\.)checkdomain\.de$/, aliases: ['checkdomain'] }, + { name: 'Namecheap', hosts: /(^|\.)(registrar-servers\.com|namecheap\.com)$/, aliases: ['namecheap'] }, + { name: 'Porkbun', hosts: /(^|\.)porkbun\.com$/, aliases: ['porkbun'] }, + { name: 'Wix', hosts: /(^|\.)wixdns\.net$/, aliases: ['wix'] }, + { name: 'Jimdo', hosts: /(^|\.)jimdo\.com$/, aliases: ['jimdo'] }, + { name: 'Mittwald', hosts: /(^|\.)mittwald\.de$/, aliases: ['mittwald'] }, +] + +const squash = (s) => String(s || '').toLowerCase().replace(/[^a-z0-9]/g, '') + +function detectProviders(hosts) { + const found = new Map() + for (const host of hosts) { + const h = String(host || '').toLowerCase().replace(/\.$/, '') + const p = PROVIDERS.find((x) => x.hosts.test(h)) + if (p) found.set(p.name, p) + } + return [...found.values()] +} + +function providerMatches(provider, claimed) { + const c = squash(claimed) + if (!c) return false + return [provider.name, ...provider.aliases].some((a) => { + const s = squash(a) + return s.length >= 2 && (c.includes(s) || s.includes(c)) + }) +} + +let ownIpsCache = null +async function ownServerIps(resolver) { + if (!ownIpsCache?.length) { + ownIpsCache = await resolver.resolve4(new URL(config.publicBaseUrl).hostname).catch(() => []) + } + return ownIpsCache +} + +async function dnsCheck(domain, claimedProvider) { + const resolver = new Resolver({ timeout: 3000, tries: 2 }) + const settle = (p) => p.then((v) => ({ ok: true, v }), (e) => ({ ok: false, code: e.code })) + const host = domainToASCII(domain) + const [ns, mx, a, own] = await Promise.all([ + settle(resolver.resolveNs(host)), + settle(resolver.resolveMx(host)), + settle(resolver.resolve4(host)), + ownServerIps(resolver), + ]) + const nsHosts = ns.ok ? ns.v : [] + const mxHosts = mx.ok ? mx.v.sort((x, y) => x.priority - y.priority).map((m) => m.exchange) : [] + const ips = a.ok ? a.v : [] + const detail = [ + `Nameserver: ${nsHosts.join(', ') || '–'}`, + `Mailserver: ${mxHosts.join(', ') || '–'}`, + `IP: ${ips.join(', ') || '–'}`, + ].join(' · ') + + if (!ns.ok && !a.ok && ['ENOTFOUND', 'ENODATA'].includes(ns.code)) { + return { key: 'dns', status: 'warn', label: `Domain ${domain} ist im DNS nicht auffindbar`, detail } + } + const results = [] + if (ips.length && ips.some((ip) => own.includes(ip))) { + results.push({ key: 'dns-webklar', status: 'info', label: 'Die Domain zeigt bereits auf den WEBklar-Server', detail: `IP: ${ips.join(', ')}` }) + } + const detected = detectProviders([...nsHosts, ...mxHosts]) + const match = detected.find((p) => providerMatches(p, claimedProvider)) + if (match) { + results.push({ key: 'dns', status: 'ok', label: `DNS passt zum angegebenen Anbieter (${match.name})`, detail }) + } else if (detected.length) { + results.push({ + key: 'dns', + status: 'info', + label: `DNS zeigt auf ${detected.map((p) => p.name).join(' / ')}, im Antrag steht „${claimedProvider}“ – mit der Rechnung abgleichen`, + detail, + }) + } else { + results.push({ key: 'dns', status: 'info', label: 'Anbieter aus dem DNS nicht erkennbar – Einträge mit der Rechnung abgleichen', detail }) + } + return results +} + +/** + * Hinweise fuer die Pruefung: doppelte Nachweise, schon gutgeschriebene Domain, Zeitraum, + * Abo-Status, Plausibilitaet des Betrags und ein DNS-Abgleich der Domain mit dem Anbieter. + */ +export async function runAutoChecks(doc, { allRequests, subscription, calc }) { + const checks = [] + const others = allRequests.filter((r) => r.$id !== doc.$id) + const ref = (r) => `${shortRef(r.$id)} (${r.status}, ${fmtDate(r.$createdAt)})` + + const hashes = new Set(filesOf(doc).map((f) => f.sha256)) + const dupFiles = others.filter((r) => filesOf(r).some((f) => hashes.has(f.sha256))) + checks.push( + dupFiles.length + ? { key: 'duplicate-file', status: 'warn', label: 'Derselbe Nachweis wurde schon mit einem anderen Antrag eingereicht', detail: dupFiles.map(ref).join(', ') } + : { key: 'duplicate-file', status: 'ok', label: 'Nachweis wurde noch nicht mit einem anderen Antrag eingereicht' } + ) + + if (doc.domain) { + const sameDomain = others.filter((r) => r.domain === doc.domain && r.status === STATUS.approved) + checks.push( + sameDomain.length + ? { key: 'duplicate-domain', status: 'warn', label: `Für ${doc.domain} wurde schon eine Gutschrift genehmigt`, detail: sameDomain.map(ref).join(', ') } + : { key: 'duplicate-domain', status: 'ok', label: `Für ${doc.domain} gibt es noch keine genehmigte Gutschrift` } + ) + } + + if (!subscription) { + checks.push({ key: 'abo', status: 'warn', label: 'Kunde hat kein Wartungs-Abo – eine Gutschrift kann nicht verrechnet werden' }) + } else if (!isActiveSub(subscription)) { + checks.push({ key: 'abo', status: 'warn', label: `Wartungs-Abo ist beendet (Status ${subscription.status}) – keine Verrechnung möglich` }) + } else { + checks.push({ + key: 'abo', + status: subscription.cancelAtPeriodEnd ? 'info' : 'ok', + label: `Wartungs-Abo läuft, Leistungsbeginn ${fmtDate(serviceStartOf(subscription))}${subscription.cancelAtPeriodEnd ? `, gekündigt zum ${fmtDate(subscription.cancelAt || subscription.currentPeriodEnd)}` : ''}`, + }) + } + + if (calc) { + checks.push( + calc.overlapDays > 0 + ? { key: 'period', status: 'ok', label: `Doppelt bezahlt: ${calc.overlapDays} Tage (${fmtDate(calc.overlapStart)} – ${fmtDate(calc.periodEnd)})` } + : { key: 'period', status: 'warn', label: 'Der bezahlte Zeitraum endet vor dem Leistungsbeginn des Abos – keine Doppelzahlung' } + ) + } + + const monthly = doc.invoiceAmountCents / Math.max(1, doc.billingMonths) + if (monthly > config.stripe.priceAmountCents) { + checks.push({ + key: 'amount', + status: 'info', + label: `Beim bisherigen Anbieter ${fmtEuro(monthly)} pro Monat – mehr als das Abo. Enthält die Rechnung weitere Leistungen?`, + }) + } + + if (doc.domain) { + try { + checks.push(...[].concat(await dnsCheck(doc.domain, doc.provider))) + } catch (err) { + checks.push({ key: 'dns', status: 'info', label: 'DNS-Abfrage fehlgeschlagen', detail: err.message }) + } + } else { + checks.push({ key: 'dns', status: 'info', label: 'Keine Domain angegeben – kein DNS-Abgleich möglich' }) + } + return checks +} + +// --------------------------------------------------------------------------- +// Admin: Detailansicht, Entscheidung, Verrechnung +// --------------------------------------------------------------------------- + +function defaultTakeoverFrom(doc, subscription) { + return doc.takeoverFrom || serviceStartOf(subscription) || new Date().toISOString() +} + +async function stripeCreditBalance(stripeCustomerId) { + if (!stripeCustomerId || !isStripeConfigured()) return null + try { + const customer = await getStripe().customers.retrieve(stripeCustomerId) + if (customer.deleted) return null + // Stripe: negativer Saldo = Guthaben des Kunden + return Math.max(0, -(customer.balance || 0)) + } catch (err) { + console.warn('[takeover] Stripe-Guthaben nicht abrufbar:', err.message) + return null + } +} + +export async function getAdminDetail(id) { + const doc = await getTakeoverRequest(id) + const [customer, subscription, allRequests] = await Promise.all([ + getDocument(config.collections.customers, doc.customerId).catch(() => null), + getCurrentSubscription(doc.customerId).catch(() => null), + listTakeoverRequests(), + ]) + const takeoverFrom = defaultTakeoverFrom(doc, subscription) + const calc = computeTakeoverCredit({ ...doc, takeoverFrom }) + const [checks, creditBalanceCents] = await Promise.all([ + runAutoChecks(doc, { allRequests, subscription, calc }), + stripeCreditBalance(subscription?.stripeCustomerId), + ]) + const testMode = /^(sk|rk)_test_/.test(config.stripe.secretKey) + return { + request: { + id: doc.$id, + ref: shortRef(doc.$id), + createdAt: doc.$createdAt, + email: doc.email, + services: servicesLabel(doc.services), + serviceKeys: String(doc.services || '').split(',').filter(Boolean), + provider: doc.provider, + domain: doc.domain, + invoiceAmountCents: doc.invoiceAmountCents, + billingMonths: doc.billingMonths, + paidUntil: doc.paidUntil, + // Kalendertage fuer im Rechner + paidUntilDate: ymd(dayOf(doc.paidUntil)), + takeoverFromDate: ymd(dayOf(takeoverFrom)), + message: doc.message, + status: doc.status, + files: filesOf(doc).map(({ id: fileId, name, type, size, uploadedAt }) => ({ id: fileId, name, type, size, uploadedAt })), + history: historyOf(doc), + takeoverFrom, + amountCents: doc.amountCents ?? null, + calculation: doc.calculation || '', + checks: checksOf(doc), + decisionNote: doc.decisionNote || '', + decidedAt: doc.decidedAt || null, + decidedBy: doc.decidedBy || '', + appliedAt: doc.appliedAt || null, + stripeBalanceTransactionId: doc.stripeBalanceTransactionId || '', + conditionsVersion: doc.conditionsVersion || '', + consentAt: doc.consentAt || null, + }, + customer: customer + ? { id: customer.$id, code: customer.code || '', name: customer.name || '', companyName: customer.companyName || '', email: customer.email || '' } + : { id: doc.customerId, code: '', name: '(Kunde nicht gefunden)', companyName: '', email: '' }, + subscription: subscription + ? { + status: subscription.status, + active: isActiveSub(subscription), + startedAt: subscription.startedAt, + serviceStart: serviceStartOf(subscription), + currentPeriodEnd: subscription.currentPeriodEnd, + cancelAt: subscription.cancelAtPeriodEnd ? subscription.cancelAt || subscription.currentPeriodEnd : null, + stripeCustomerId: subscription.stripeCustomerId, + stripeUrl: subscription.stripeCustomerId + ? `https://dashboard.stripe.com/${testMode ? 'test/' : ''}customers/${subscription.stripeCustomerId}` + : '', + creditBalanceCents, + } + : null, + calc, + autoChecks: checks, + requiredChecks: REQUIRED_CHECKS, + otherRequests: allRequests + .filter((r) => r.customerId === doc.customerId && r.$id !== doc.$id) + .map((r) => ({ id: r.$id, ref: shortRef(r.$id), createdAt: r.$createdAt, status: r.status, amountCents: r.amountCents ?? null, services: servicesLabel(r.services) })), + } +} + +async function findBalanceTransaction(stripeCustomerId, requestId) { + const list = await getStripe().customers.listBalanceTransactions(stripeCustomerId, { limit: 100 }) + return list.data.find((t) => t.metadata?.webklarTakeoverCreditId === requestId) || null +} + +/** + * Genehmigte Gutschrift als Stripe-Kundenguthaben buchen. Doppelt abgesichert gegen + * Doppelbuchung: vorhandene Buchung mit dieser Antrags-ID wird wiederverwendet, und der + * Idempotency-Key faengt parallele Klicks ab. + */ +export async function applyTakeoverCredit(doc) { + if (doc.status !== STATUS.approved) throw httpError(409, 'Nur genehmigte Anträge können verrechnet werden.') + if (doc.appliedAt) return { applied: true, doc } + if (!isStripeConfigured()) return { applied: false, doc, reason: 'Stripe ist nicht eingerichtet.' } + const sub = await getCurrentSubscription(doc.customerId) + if (!isActiveSub(sub) || !sub.stripeCustomerId) { + return { applied: false, doc, reason: 'Der Kunde hat kein laufendes Wartungs-Abo.' } + } + try { + const txn = + (await findBalanceTransaction(sub.stripeCustomerId, doc.$id)) || + (await getStripe().customers.createBalanceTransaction( + sub.stripeCustomerId, + { + amount: -doc.amountCents, + currency: 'eur', + description: `Übernahmegutschrift ${shortRef(doc.$id)} (${servicesLabel(doc.services)})`, + metadata: { webklarTakeoverCreditId: doc.$id, webklarCustomerId: doc.customerId }, + }, + { idempotencyKey: `webklar-takeover-credit-${doc.$id}` } + )) + const updated = await updateDocument(COLLECTION(), doc.$id, { + appliedAt: new Date(txn.created * 1000).toISOString(), + stripeCustomerId: sub.stripeCustomerId, + stripeBalanceTransactionId: txn.id, + history: withHistory(doc, { type: 'verrechnet', by: 'system', text: `Stripe-Guthaben ${fmtEuro(doc.amountCents)} gebucht (${txn.id})` }), + }) + return { applied: true, doc: updated } + } catch (err) { + console.error(`[takeover] Verrechnung ${doc.$id} fehlgeschlagen:`, err.message) + return { applied: false, doc, reason: `Stripe: ${err.message}` } + } +} + +function customerGreeting(doc, customer) { + return `Guten Tag ${customer?.name || ''}`.trim() + ',' +} + +async function mailDecision(doc, customer, action) { + const ref = shortRef(doc.$id) + const submitted = fmtDate(doc.$createdAt) + const note = String(doc.decisionNote || '').trim() + const footer = ['', 'Viele Grüße', 'Ihr WEBklar-Team', '', FOOTER] + if (action === 'approve') { + return sendCustomerMail(doc.email, 'Ihre Übernahmegutschrift ist genehmigt – WEBklar', [ + customerGreeting(doc, customer), + '', + `wir haben Ihren Antrag auf Übernahmegutschrift vom ${submitted} geprüft und genehmigt.`, + '', + ` Antrag: ${ref}`, + ` Leistung: ${servicesLabel(doc.services)} (bisher bei ${doc.provider})`, + ` Gutschrift: ${fmtEuro(doc.amountCents)}`, + ` Berechnung: ${doc.calculation}`, + ...(note ? ['', note] : []), + '', + 'Die Gutschrift verrechnen wir automatisch mit den nächsten Monatsgebühren Ihres Wartungs-Abos.', + 'Auf Ihren Rechnungen erscheint sie als angerechnetes Guthaben. Eine Auszahlung ist nicht möglich;', + 'ein bei Vertragsende noch nicht verrechneter Rest entfällt.', + ...footer, + ]) + } + if (action === 'reject') { + return sendCustomerMail(doc.email, 'Ihr Antrag auf Übernahmegutschrift – WEBklar', [ + customerGreeting(doc, customer), + '', + `wir haben Ihren Antrag auf Übernahmegutschrift vom ${submitted} (${ref}) geprüft und können ihn leider nicht genehmigen.`, + '', + note, + '', + 'Bei Fragen antworten Sie einfach auf diese E-Mail.', + ...footer, + ]) + } + const question = [...historyOf(doc)].reverse().find((h) => h.type === 'rueckfrage')?.text || '' + return sendCustomerMail(doc.email, 'Rückfrage zu Ihrem Antrag auf Übernahmegutschrift – WEBklar', [ + customerGreeting(doc, customer), + '', + `zu Ihrem Antrag auf Übernahmegutschrift vom ${submitted} (${ref}) haben wir eine Rückfrage:`, + '', + question, + '', + `Bitte antworten Sie im Kundenportal unter ${config.publicBaseUrl}/dashboard.html – dort können Sie`, + 'auch weitere Nachweise hochladen. Alternativ antworten Sie einfach auf diese E-Mail.', + ...footer, + ]) +} + +/** + * Entscheidung des Admins. + * action: 'approve' (amountCents, takeoverFrom, checks, note optional) | 'question' (note) | 'reject' (note) + */ +export async function decideTakeoverRequest(id, body, admin) { + const doc = await getTakeoverRequest(id) + if (![STATUS.open, STATUS.question].includes(doc.status)) { + throw httpError(409, 'Über diesen Antrag wurde bereits entschieden.') + } + const action = String(body?.action || '') + const note = clean(body?.note, 4000) + const by = admin?.name ? `${admin.name}${admin.email ? ` <${admin.email}>` : ''}` : admin?.email || 'Admin' + const now = new Date().toISOString() + const customer = await getDocument(config.collections.customers, doc.customerId).catch(() => null) + + if (action === 'question' || action === 'reject') { + if (note.length < 5) { + throw httpError(400, action === 'question' ? 'Bitte formulieren Sie die Rückfrage an den Kunden.' : 'Bitte begründen Sie die Ablehnung für den Kunden.') + } + const data = + action === 'question' + ? { status: STATUS.question, history: withHistory(doc, { type: 'rueckfrage', by, text: note }) } + : { + status: STATUS.rejected, + decisionNote: note, + decidedAt: now, + decidedBy: by, + history: withHistory(doc, { type: 'abgelehnt', by, text: note }), + } + const updated = await updateDocument(COLLECTION(), doc.$id, data) + const mail = await mailDecision(updated, customer, action).then( + () => ({ sent: true }), + (e) => ({ sent: false, reason: e.message }) + ) + return { doc: updated, mail } + } + + if (action !== 'approve') throw httpError(400, 'Unbekannte Aktion') + + const amountCents = Math.round(Number(body?.amountCents)) + if (!(amountCents > 0) || amountCents > config.takeover.maxAmountCents) { + throw httpError(400, `Bitte einen Gutschriftsbetrag zwischen 0,01 € und ${fmtEuro(config.takeover.maxAmountCents)} angeben.`) + } + const missing = REQUIRED_CHECKS.filter((c) => body?.checks?.[c.key] !== true) + if (missing.length) { + throw httpError(400, `Bitte zuerst prüfen und abhaken: ${missing.map((c) => c.label).join('; ')}`) + } + const takeoverFrom = dayOf(body?.takeoverFrom) + if (!takeoverFrom) throw httpError(400, 'Bitte das Datum „Übernahme ab“ angeben.') + // Werte laut Rechnung: der Admin kann Tippfehler des Kunden im Rechner korrigieren + const verified = readInvoiceFigures(body, doc) + const sub = await getCurrentSubscription(doc.customerId) + if (!isActiveSub(sub)) { + throw httpError(409, 'Der Kunde hat kein laufendes Wartungs-Abo – die Gutschrift kann nicht verrechnet werden.') + } + + const corrections = [ + verified.invoiceAmountCents !== doc.invoiceAmountCents && + `Rechnungsbetrag ${fmtEuro(doc.invoiceAmountCents)} → ${fmtEuro(verified.invoiceAmountCents)}`, + verified.billingMonths !== doc.billingMonths && + `Zeitraum ${monthsLabel(doc.billingMonths)} → ${monthsLabel(verified.billingMonths)}`, + ymd(dayOf(verified.paidUntil)) !== ymd(dayOf(doc.paidUntil)) && + `bezahlt bis ${fmtDate(doc.paidUntil)} → ${fmtDate(verified.paidUntil)}`, + ].filter(Boolean) + const calc = computeTakeoverCredit({ ...verified, takeoverFrom: takeoverFrom.toISOString() }) + const approved = await updateDocument(COLLECTION(), doc.$id, { + ...verified, + status: STATUS.approved, + amountCents, + takeoverFrom: takeoverFrom.toISOString(), + calculation: calculationText(verified, calc, amountCents).slice(0, 1000), + checks: JSON.stringify({ items: Object.fromEntries(REQUIRED_CHECKS.map((c) => [c.key, true])), by, at: now }), + decisionNote: note, + decidedAt: now, + decidedBy: by, + history: withHistory(doc, { + type: 'genehmigt', + by, + text: [ + fmtEuro(amountCents), + corrections.length ? `Angaben nach Prüfung korrigiert: ${corrections.join(', ')}` : '', + note, + ].filter(Boolean).join(' – '), + }), + }) + const result = await applyTakeoverCredit(approved) + const mail = await mailDecision(result.doc, customer, 'approve').then( + () => ({ sent: true }), + (e) => ({ sent: false, reason: e.message }) + ) + return { doc: result.doc, applied: result.applied, reason: result.reason, mail } +} + +// --------------------------------------------------------------------------- +// Aufbewahrung: Nachweise abgelehnter Antraege nach Ablauf der Frist loeschen +// --------------------------------------------------------------------------- + +export async function purgeExpiredUploads() { + const cutoff = Date.now() - config.takeover.rejectedRetentionDays * DAY_MS + const docs = await listDocuments(COLLECTION(), [Query.equal('status', STATUS.rejected), Query.limit(1000)]) + let purged = 0 + for (const doc of docs) { + if (!filesOf(doc).length || !doc.decidedAt || new Date(doc.decidedAt).getTime() > cutoff) continue + await rm(path.join(config.takeover.uploadDir, doc.$id), { recursive: true, force: true }) + await updateDocument(COLLECTION(), doc.$id, { + files: '[]', + history: withHistory(doc, { type: 'nachweise-geloescht', by: 'system', text: 'Aufbewahrungsfrist abgelaufen' }), + }) + purged += 1 + } + return { purged } +}