import { NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; export async function GET(req: Request, { params }: { params: { id: string } }) { try { const { id } = params; const order = await prisma.order.findUnique({ where: { id }, include: { items: true }, }); if (!order) { return NextResponse.json({ error: 'Order not found' }, { status: 404 }); } const item = order.items[0]; if (!item) { return NextResponse.json({ error: 'No items in order' }, { status: 400 }); } const design = JSON.parse(item.designJson) as { front: any[]; back: any[] }; const product = await prisma.product.findUnique({ where: { id: item.productId }, }); if (!product) { return NextResponse.json({ error: 'Product not found' }, { status: 404 }); } // Generate HTML/CSS for a printable spec sheet const printAreaWidthMm = product.printAreaWidthMm || 200; const printAreaHeightMm = product.printAreaHeightMm || 280; // Calculate scale factor: pixels to millimeters const DISPLAY = 560; // Same as in DesignCanvas const printArea = JSON.parse(product.printArea) as { xPct: number; yPct: number; wPct: number; hPct: number; }; const printAreaStartX = DISPLAY * (printArea.xPct / 100); const printAreaStartY = DISPLAY * (printArea.yPct / 100); const printAreaWidthPx = DISPLAY * (printArea.wPct / 100); const printAreaHeightPx = DISPLAY * (printArea.hPct / 100); // Scale: how many millimeters per pixel const mmPerPx = printAreaWidthMm / printAreaWidthPx; // Convert pixel measurements to CM // First: subtract the print area offset to get position within print area // Then: multiply by mmPerPx to get millimeters // Finally: divide by 10 to get centimeters const convertToCm = (px: number, elementPx: number | null = null) => { if (elementPx !== null) { // For element positions: subtract offset and scale const relativeToStart = elementPx - px; const mm = Math.max(0, relativeToStart * mmPerPx); return (mm / 10).toFixed(1); } else { // For dimensions: just scale const mm = px * mmPerPx; return (mm / 10).toFixed(1); } }; const generateDesignSection = (designElements: any[], side: 'Front' | 'Back', placement: string | null, designPreview: string | null) => { if (!designElements || designElements.length === 0) return ''; return `
Design on product with measurement guides
━━ Red dashed line: Print area boundary
━ Blue line: X-axis (horizontal, 0cm at left)
━ Green line: Y-axis (vertical, 0cm at top)
● Orange circles: Element positions (E1, E2, etc.)
| Element | Type | Position (CM) | Size (CM) | Details |
|---|---|---|---|---|
| E${idx + 1} | ${el.type === 'text' ? '📝 Text' : '🖼️ Image'} | X: ${convertToCm(printAreaStartX, el.x)} | Y: ${convertToCm(printAreaStartY, el.y)} | ${el.type === 'image' ? `${convertToCm(el.width)} × ${convertToCm(el.height)}` : 'N/A'} |
${el.type === 'text' ? `
Font: ${el.fontFamily}, ${Math.round(el.fontSize * mmPerPx / 10)}pt Color: ${el.fill} Text: "${el.text}" Rotation: ${Math.round(el.rotation)}° ` : ` Rotation: ${Math.round(el.rotation)}° `} |
Order ID: ${order.id}
Product: ${item.productName}
Color: ${item.color} ${item.size ? `| Size: ${item.size}` : ''}
Order Date: ${new Date(order.createdAt).toLocaleDateString()}
Notes: