Initial commit - SaveTheMoment Atlas Basis-Setup

This commit is contained in:
Dennis Forte
2025-11-12 20:21:32 +01:00
commit 0b6e429329
167 changed files with 30843 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { generateContractPDF, generateSignedContractPDF } from '@/lib/pdf-service';
import { writeFile, mkdir } from 'fs/promises';
import path from 'path';
export async function POST(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = params;
const booking = await prisma.booking.findUnique({
where: { id },
include: {
location: true,
photobox: true,
},
});
if (!booking) {
return NextResponse.json({ error: 'Booking not found' }, { status: 404 });
}
// Generate PDF
const pdfBuffer = await generateContractPDF(booking, booking.location, booking.photobox);
// Save PDF to public/contracts folder
const contractsDir = path.join(process.cwd(), 'public', 'contracts');
await mkdir(contractsDir, { recursive: true });
const filename = `contract-${booking.bookingNumber}.pdf`;
const filepath = path.join(contractsDir, filename);
await writeFile(filepath, pdfBuffer);
const contractUrl = `/contracts/${filename}`;
// Update booking
await prisma.booking.update({
where: { id },
data: {
contractGenerated: true,
contractGeneratedAt: new Date(),
contractPdfUrl: contractUrl,
},
});
return NextResponse.json({
success: true,
contractUrl,
filename,
});
} catch (error: any) {
console.error('Contract generation error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to generate contract' },
{ status: 500 }
);
}
}
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const { id } = params;
const booking = await prisma.booking.findUnique({
where: { id },
include: {
location: true,
photobox: true,
},
});
if (!booking) {
return NextResponse.json({ error: 'Booking not found' }, { status: 404 });
}
// Generate PDF in memory for download
const pdfBuffer = await generateSignedContractPDF(
booking,
booking.location,
booking.photobox,
booking.contractSignatureData || '',
booking.contractSignedBy || '',
booking.contractSignedAt || new Date(),
booking.contractSignedIp || ''
);
return new NextResponse(new Uint8Array(pdfBuffer), {
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': `attachment; filename="Vertrag-${booking.bookingNumber}.pdf"`,
},
});
} catch (error: any) {
console.error('Contract download error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to download contract' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,75 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { sendContractEmail } from '@/lib/email-service';
export async function POST(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = params;
const booking = await prisma.booking.findUnique({
where: { id },
include: {
location: true,
photobox: true,
},
});
if (!booking) {
return NextResponse.json({ error: 'Booking not found' }, { status: 404 });
}
if (!booking.contractGenerated || !booking.contractPdfUrl) {
return NextResponse.json(
{ error: 'Contract not generated yet. Please generate contract first.' },
{ status: 400 }
);
}
try {
await sendContractEmail(booking, booking.contractPdfUrl);
await prisma.booking.update({
where: { id },
data: {
contractSentAt: new Date(),
},
});
return NextResponse.json({
success: true,
message: `Contract sent to ${booking.customerEmail}`,
});
} catch (emailError: any) {
console.error('Email send error:', emailError);
if (emailError.message?.includes('SMTP not configured')) {
return NextResponse.json({
success: false,
error: 'E-Mail-Service nicht konfiguriert. Bitte SMTP-Einstellungen in .env hinzufügen.',
}, { status: 503 });
}
return NextResponse.json({
success: false,
error: emailError.message || 'Failed to send email',
}, { status: 500 });
}
} catch (error: any) {
console.error('Contract send error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to send contract' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,73 @@
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { writeFile, mkdir } from 'fs/promises';
import path from 'path';
// Note: Google Vision API can be added later for automatic signature detection
// For now, we trust admin verification
export async function POST(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions);
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { id } = params;
const formData = await request.formData();
const file = formData.get('file') as File;
if (!file) {
return NextResponse.json({ error: 'No file provided' }, { status: 400 });
}
const booking = await prisma.booking.findUnique({
where: { id },
});
if (!booking) {
return NextResponse.json({ error: 'Booking not found' }, { status: 404 });
}
// Save uploaded file
const buffer = Buffer.from(await file.arrayBuffer());
const contractsDir = path.join(process.cwd(), 'public', 'contracts');
await mkdir(contractsDir, { recursive: true });
const filename = `contract-uploaded-${booking.bookingNumber}-${Date.now()}.pdf`;
const filepath = path.join(contractsDir, filename);
await writeFile(filepath, buffer);
const contractUrl = `/contracts/${filename}`;
// Update booking
await prisma.booking.update({
where: { id },
data: {
contractSigned: true,
contractSignedAt: new Date(),
contractSignedOnline: false,
contractPdfUrl: contractUrl,
contractSignedBy: booking.customerName,
contractUploadedBy: session.user.id,
},
});
return NextResponse.json({
success: true,
message: 'Contract uploaded successfully',
});
} catch (error: any) {
console.error('Contract upload error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to upload contract' },
{ status: 500 }
);
}
}