v1.31: PDF-Lightbox mit Druck/E-Mail/Download, Mail-API Anhang-Support
This commit is contained in:
+76
-9
@@ -797,18 +797,85 @@ app.post('/api/documents/:id/upload', auth, adminOnly, async (req, res, next) =>
|
||||
})
|
||||
})
|
||||
|
||||
// Lädt Dokument samt Berechtigungs- und Dateiprüfung (Download/Preview/E-Mail).
|
||||
async function dokumentMitDatei(req, res) {
|
||||
const { rows } = await pool.query('SELECT * FROM documents WHERE id = $1', [req.params.id])
|
||||
if (!rows.length) { res.status(404).json({ fehler: 'Dokument nicht gefunden.' }); return null }
|
||||
const d = rows[0]
|
||||
const erlaubt = req.user.rolle === 'verwaltung' || d.unit_id === null || d.unit_id === req.user.einheit_id
|
||||
if (!erlaubt) { res.status(403).json({ fehler: 'Kein Zugriff auf dieses Dokument.' }); return null }
|
||||
if (!d.file_path) { res.status(404).json({ fehler: 'Zu diesem Eintrag ist noch keine Datei hinterlegt.' }); return null }
|
||||
const vollerPfad = path.join(UPLOAD_DIR, path.basename(d.file_path)) // basename: Pfad-Traversal unterbinden
|
||||
if (!fs.existsSync(vollerPfad)) { res.status(404).json({ fehler: 'Datei nicht gefunden.' }); return null }
|
||||
return { dokument: d, pfad: vollerPfad }
|
||||
}
|
||||
|
||||
// PDF-Download: Verwaltung immer, Mieter nur bei eigener Wohnung oder Hausdokument
|
||||
app.get('/api/documents/:id/download', auth, async (req, res, next) => {
|
||||
try {
|
||||
const { rows } = await pool.query('SELECT * FROM documents WHERE id = $1', [req.params.id])
|
||||
if (!rows.length) return res.status(404).json({ fehler: 'Dokument nicht gefunden.' })
|
||||
const d = rows[0]
|
||||
const erlaubt = req.user.rolle === 'verwaltung' || d.unit_id === null || d.unit_id === req.user.einheit_id
|
||||
if (!erlaubt) return res.status(403).json({ fehler: 'Kein Zugriff auf dieses Dokument.' })
|
||||
if (!d.file_path) return res.status(404).json({ fehler: 'Zu diesem Eintrag ist noch keine Datei hinterlegt.' })
|
||||
const vollerPfad = path.join(UPLOAD_DIR, path.basename(d.file_path)) // basename: Pfad-Traversal unterbinden
|
||||
if (!fs.existsSync(vollerPfad)) return res.status(404).json({ fehler: 'Datei nicht gefunden.' })
|
||||
res.download(vollerPfad, d.file_name || `${d.title}.pdf`)
|
||||
const gefunden = await dokumentMitDatei(req, res)
|
||||
if (!gefunden) return
|
||||
res.download(gefunden.pfad, gefunden.dokument.file_name || `${gefunden.dokument.title}.pdf`)
|
||||
} catch (e) { next(e) }
|
||||
})
|
||||
|
||||
// PDF-Preview für die Lightbox: gleiche Rechte wie Download, aber inline (v1.31)
|
||||
app.get('/api/documents/:id/preview', auth, async (req, res, next) => {
|
||||
try {
|
||||
const gefunden = await dokumentMitDatei(req, res)
|
||||
if (!gefunden) return
|
||||
res.setHeader('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(gefunden.dokument.file_name || gefunden.dokument.title)}`)
|
||||
res.setHeader('Content-Type', 'application/pdf')
|
||||
res.sendFile(gefunden.pfad)
|
||||
} catch (e) { next(e) }
|
||||
})
|
||||
|
||||
// PDF per E-Mail versenden: Empfänger frei wählbar oder Mieter der Wohnung,
|
||||
// immer mit CC an die Verwaltung (v1.31)
|
||||
app.post('/api/documents/:id/email', auth, adminOnly, async (req, res, next) => {
|
||||
try {
|
||||
const gefunden = await dokumentMitDatei(req, res)
|
||||
if (!gefunden) return
|
||||
const d = gefunden.dokument
|
||||
const EMAIL_RE = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
|
||||
let empfaenger = String((req.body || {}).an || '').trim()
|
||||
let empfaengerName = ''
|
||||
if (!empfaenger && d.unit_id) {
|
||||
const { rows: mieter } = await pool.query(
|
||||
`SELECT name, email FROM users WHERE rolle = 'mieter' AND aktiv AND einheit_id = $1 AND email <> '' ORDER BY name LIMIT 1`,
|
||||
[d.unit_id],
|
||||
)
|
||||
if (mieter.length) { empfaenger = mieter[0].email; empfaengerName = mieter[0].name }
|
||||
}
|
||||
if (!empfaenger || !EMAIL_RE.test(empfaenger)) {
|
||||
return res.status(400).json({ fehler: 'Kein gültiger Empfänger – E-Mail-Adresse angeben oder Mieter-E-Mail hinterlegen.' })
|
||||
}
|
||||
const nachricht = String((req.body || {}).nachricht || '').trim()
|
||||
const betreff = String((req.body || {}).betreff || '').trim() || `Dokument: ${d.title}`
|
||||
const dateiname = d.file_name || `${d.title}.pdf`
|
||||
const cc = await mail.verwaltungEmails()
|
||||
const gruss = empfaengerName ? `Guten Tag ${empfaengerName},` : 'Guten Tag,'
|
||||
const text = `${gruss}\n\nim Anhang finden Sie das Dokument „${d.title}" aus dem Mieterportal Hinterstein Immobilien.${nachricht ? `\n\n${nachricht}` : ''}\n\nMit freundlichen Grüßen\nHinterstein Immobilien`
|
||||
const html = mail.baueHtmlMail({
|
||||
titel: d.title,
|
||||
untertitel: 'Dokument aus dem Mieterportal',
|
||||
einleitung: gruss,
|
||||
zeilen: [
|
||||
['Dokument', d.title],
|
||||
['Kategorie', d.category],
|
||||
...(d.unit_id ? [['Wohnung', mail.einheitLabel((await pool.query('SELECT * FROM units WHERE id = $1', [d.unit_id])).rows[0])]] : []),
|
||||
...(nachricht ? [['Nachricht', nachricht]] : []),
|
||||
],
|
||||
statusBox: { text: 'Das PDF ist an diese E-Mail angehängt.', hintergrund: '#ecfdf5', farbe: '#047857' },
|
||||
fussnote: 'Bei Rückfragen antworten Sie bitte direkt auf diese E-Mail oder nutzen Sie das Nachrichten-Modul im Portal.',
|
||||
})
|
||||
await mail.sendeMail(empfaenger, betreff, text, html, cc, {
|
||||
dateiname,
|
||||
inhaltB64: fs.readFileSync(gefunden.pfad).toString('base64'),
|
||||
mime: 'application/pdf',
|
||||
})
|
||||
console.log(`[mail] Dokument „${d.title}" an ${empfaenger} versendet (CC: ${cc || '–'})`)
|
||||
res.json({ ok: true, an: empfaenger })
|
||||
} catch (e) { next(e) }
|
||||
})
|
||||
|
||||
|
||||
+7
-1
@@ -108,7 +108,7 @@ function buttonHtml(text, url) {
|
||||
return `<p style="margin:24px 0 0;"><a href="${htmlEscape(url)}" style="display:inline-block;background:${FARBE_BUTTON};color:${FARBE_BUTTON_TEXT};text-decoration:none;font-size:14px;font-weight:700;padding:12px 22px;border-radius:10px;">${htmlEscape(text)}</a></p>`
|
||||
}
|
||||
|
||||
async function sendeMail(an, betreff, text, html, cc) {
|
||||
async function sendeMail(an, betreff, text, html, cc, anhang) {
|
||||
if (!MAIL_API_URL || !MAIL_API_TOKEN || !an) return
|
||||
try {
|
||||
const res = await fetch(`${MAIL_API_URL}/send`, {
|
||||
@@ -118,6 +118,12 @@ async function sendeMail(an, betreff, text, html, cc) {
|
||||
to: an, subject: betreff, text: `${text}\n\n${FUSSZEILE_TEXT}`,
|
||||
...(html ? { html } : {}),
|
||||
...(cc ? { cc } : {}), // v1.22: Komma-getrennte CC-Empfänger
|
||||
// v1.31: optionaler Anhang (z. B. PDF-Abrechnung)
|
||||
...(anhang ? {
|
||||
attachment_b64: anhang.inhaltB64,
|
||||
attachment_filename: anhang.dateiname,
|
||||
attachment_mime: anhang.mime || 'application/pdf',
|
||||
} : {}),
|
||||
}),
|
||||
})
|
||||
if (!res.ok) console.warn(`[mail] Versand an ${an} fehlgeschlagen: HTTP ${res.status}`)
|
||||
|
||||
Reference in New Issue
Block a user