Lumen & Gesso / website · lumengesso.com / app / api / studio / print / quote / route.ts
| 1 | import { getCustomerUser } from '@/app/lib/lumen-auth'; |
| 2 | import { StudioError } from '@/app/lib/studio-media'; |
| 3 | import { emailSignupReady } from '@/app/lib/studio-config'; |
| 4 | import { createPrintQuote, parsePrintSelection, PRINT_HEADERS, printArtwork, printEnabled, printError, readPrintForm, requirePrintCustomer } from '../protocol'; |
| 5 | |
| 6 | export const runtime = 'nodejs'; |
| 7 | export const dynamic = 'force-dynamic'; |
| 8 | |
| 9 | export async function GET() { |
| 10 | const enabled = printEnabled(); |
| 11 | const signedIn = Boolean(await getCustomerUser().catch(() => null)); |
| 12 | return Response.json({ enabled, signedIn, signupAvailable: await emailSignupReady(enabled) }, { headers: PRINT_HEADERS }); |
| 13 | } |
| 14 | |
| 15 | export async function POST(req: Request) { |
| 16 | try { |
| 17 | const user = await requirePrintCustomer(req); |
| 18 | const form = await readPrintForm(req, ['image', 'selection']); |
| 19 | const raw = form.get('selection'); |
| 20 | let selection: unknown; |
| 21 | try { |
| 22 | if (typeof raw !== 'string' || raw.length > 1024) throw new Error('invalid selection'); |
| 23 | selection = JSON.parse(raw); |
| 24 | } catch { throw new StudioError('invalid_selection', 400, 'Choose an available print size, frame and finish.'); } |
| 25 | const parsed = parsePrintSelection(selection); |
| 26 | return Response.json(createPrintQuote(user.id, parsed, await printArtwork(form.get('image'), parsed)), { headers: PRINT_HEADERS }); |
| 27 | } catch (error) { return printError(error); } |
| 28 | } |