import { NextRequest, NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/lib/auth'; import { db } from '@/lib/db'; import { z } from 'zod'; const updateQRSchema = z.object({ title: z.string().min(1).optional(), content: z.any().optional(), tags: z.array(z.string()).optional(), style: z.any().optional(), status: z.enum(['ACTIVE', 'PAUSED']).optional(), }); // GET /api/qrs/[id] - Get a single QR code export async function GET( request: NextRequest, { params }: { params: { id: string } } ) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const qrCode = await db.qRCode.findFirst({ where: { id: params.id, userId: session.user.id, }, include: { scans: { orderBy: { ts: 'desc' }, take: 100, }, }, }); if (!qrCode) { return NextResponse.json({ error: 'QR code not found' }, { status: 404 }); } return NextResponse.json(qrCode); } catch (error) { console.error('Error fetching QR code:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } } // PATCH /api/qrs/[id] - Update a QR code export async function PATCH( request: NextRequest, { params }: { params: { id: string } } ) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const body = await request.json(); const data = updateQRSchema.parse(body); // Check ownership const existing = await db.qRCode.findFirst({ where: { id: params.id, userId: session.user.id, }, }); if (!existing) { return NextResponse.json({ error: 'QR code not found' }, { status: 404 }); } // Static QR codes cannot be edited if (existing.type === 'STATIC' && data.content) { return NextResponse.json( { error: 'Static QR codes cannot be edited' }, { status: 400 } ); } // Update QR code const updated = await db.qRCode.update({ where: { id: params.id }, data: { ...(data.title && { title: data.title }), ...(data.content && { content: data.content }), ...(data.tags && { tags: data.tags }), ...(data.style && { style: data.style }), ...(data.status && { status: data.status }), }, }); return NextResponse.json(updated); } catch (error) { if (error instanceof z.ZodError) { return NextResponse.json( { error: 'Invalid input', details: error.errors }, { status: 400 } ); } console.error('Error updating QR code:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } } // DELETE /api/qrs/[id] - Delete a QR code export async function DELETE( request: NextRequest, { params }: { params: { id: string } } ) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } // Check ownership const existing = await db.qRCode.findFirst({ where: { id: params.id, userId: session.user.id, }, }); if (!existing) { return NextResponse.json({ error: 'QR code not found' }, { status: 404 }); } // Delete QR code (cascades to scans) await db.qRCode.delete({ where: { id: params.id }, }); return NextResponse.json({ success: true }); } catch (error) { console.error('Error deleting QR code:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } }