import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/db' import { getSession } from '@/lib/auth' import { getProjectWithTenantCheck } from '@/lib/tenant' import { sendEmail } from '@/lib/email' export async function POST(req: NextRequest, { params }: { params: { id: string } }) { try { const user = await getSession() if (!user) return NextResponse.json({ error: 'Nicht autorisiert' }, { status: 401 }) const project = await getProjectWithTenantCheck(params.id, user) if (!project) return NextResponse.json({ error: 'Projekt nicht gefunden' }, { status: 404 }) // Load tenant logo let tenantLogoUrl = '' let tenantName = '' if ((project as any).tenantId) { const tenant = await (prisma as any).tenant.findUnique({ where: { id: (project as any).tenantId }, select: { logoUrl: true, name: true }, }) if (tenant?.logoUrl) tenantLogoUrl = tenant.logoUrl if (tenant?.name) tenantName = tenant.name } const body = await req.json() const { recipientEmail } = body if (!recipientEmail) { return NextResponse.json({ error: 'Empfänger-E-Mail erforderlich' }, { status: 400 }) } // Load journal data const entries = await (prisma as any).journalEntry.findMany({ where: { projectId: params.id }, orderBy: [{ time: 'asc' }, { sortOrder: 'asc' }], }) const checkItems = await (prisma as any).journalCheckItem.findMany({ where: { projectId: params.id }, orderBy: { sortOrder: 'asc' }, }) const pendenzen = await (prisma as any).journalPendenz.findMany({ where: { projectId: params.id }, orderBy: { sortOrder: 'asc' }, }) // Build HTML report const formatTime = (d: Date) => new Date(d).toLocaleTimeString('de-CH', { hour: '2-digit', minute: '2-digit' }) const formatDate = (d: Date) => new Date(d).toLocaleDateString('de-CH', { day: '2-digit', month: '2-digit', year: 'numeric' }) const p = project as any let entriesHtml = '' for (const e of entries) { const correctedStyle = e.isCorrected ? 'text-decoration:line-through;opacity:0.5;' : '' const correctionStyle = e.correctionOfId ? 'color:#b45309;font-style:italic;' : '' const doneIcon = e.done ? '✅' : '' const doneAtStr = e.done && e.doneAt ? ` (erledigt ${formatTime(e.doneAt)})` : '' entriesHtml += `
${p.title || 'Ohne Titel'}${tenantName ? ` — ${tenantName}` : ''}
| Zeit | Was | Wer | Ok |
|---|---|---|---|
| Keine Einträge | |||
| Punkt | Bestätigt | Ok |
|---|
| Was | Wer | Wann/Wie | Erledigt |
|---|
Gesendet von Lageplan am ${new Date().toLocaleString('de-CH')} durch ${user.name || user.email}