import { NextRequest, NextResponse } from 'next/server'; import { cookies } from 'next/headers'; import { db } from '@/lib/db'; import { generateSlug } from '@/lib/hash'; // GET /api/qrs - List user's QR codes export async function GET(request: NextRequest) { try { const userId = cookies().get('userId')?.value; if (!userId) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const qrCodes = await db.qRCode.findMany({ where: { userId }, include: { _count: { select: { scans: true }, }, }, orderBy: { createdAt: 'desc' }, }); // Transform the data const transformed = qrCodes.map(qr => ({ ...qr, scans: qr._count.scans, _count: undefined, })); return NextResponse.json(transformed); } catch (error) { console.error('Error fetching QR codes:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } } // POST /api/qrs - Create a new QR code export async function POST(request: NextRequest) { try { const userId = cookies().get('userId')?.value; console.log('POST /api/qrs - userId from cookie:', userId); if (!userId) { return NextResponse.json({ error: 'Unauthorized - no userId cookie' }, { status: 401 }); } // Check if user exists const userExists = await db.user.findUnique({ where: { id: userId } }); console.log('User exists:', !!userExists); if (!userExists) { return NextResponse.json({ error: `User not found: ${userId}` }, { status: 404 }); } const body = await request.json(); console.log('Request body:', body); // Check if this is a static QR request const isStatic = body.isStatic === true; let enrichedContent = body.content; // For STATIC QR codes, calculate what the QR should contain if (isStatic) { let qrContent = ''; switch (body.contentType) { case 'URL': qrContent = body.content.url; break; case 'PHONE': qrContent = `tel:${body.content.phone}`; break; case 'EMAIL': qrContent = `mailto:${body.content.email}${body.content.subject ? `?subject=${encodeURIComponent(body.content.subject)}` : ''}`; break; case 'SMS': qrContent = `sms:${body.content.phone}${body.content.message ? `?body=${encodeURIComponent(body.content.message)}` : ''}`; break; case 'TEXT': qrContent = body.content.text; break; case 'WIFI': qrContent = `WIFI:T:${body.content.security || 'WPA'};S:${body.content.ssid};P:${body.content.password || ''};H:false;;`; break; case 'WHATSAPP': qrContent = `https://wa.me/${body.content.phone}${body.content.message ? `?text=${encodeURIComponent(body.content.message)}` : ''}`; break; default: qrContent = body.content.url || 'https://example.com'; } // Add qrContent to the content object enrichedContent = { ...body.content, qrContent // This is what the QR code should actually contain }; } // Generate slug for the QR code const slug = generateSlug(body.title); // Create QR code const qrCode = await db.qRCode.create({ data: { userId, title: body.title, type: isStatic ? 'STATIC' : 'DYNAMIC', contentType: body.contentType, content: enrichedContent, tags: body.tags || [], style: body.style || { foregroundColor: '#000000', backgroundColor: '#FFFFFF', cornerStyle: 'square', size: 200, }, slug, status: 'ACTIVE', }, }); return NextResponse.json(qrCode); } catch (error) { console.error('Error creating QR code:', error); return NextResponse.json( { error: 'Internal server error', details: String(error) }, { status: 500 } ); } }