import { NextResponse, type NextRequest } from 'next/server'; import { getCustomerUser } from '@/app/lib/lumen-auth'; import { isAdmin } from '@/app/lib/admin'; import { createPin } from '@/app/lib/pinterest'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // Admin-gated. Body: { boardId, imageUrl, title?, description?, link?, altText? }. // Returns the created pin ({ id, url? }) or { error }. export async function POST(req: NextRequest) { const user = await getCustomerUser(); if (!user || !isAdmin(user.email)) { return NextResponse.json({ error: 'forbidden' }, { status: 403 }); } let body: { boardId?: unknown; imageUrl?: unknown; title?: unknown; description?: unknown; link?: unknown; altText?: unknown; }; try { body = (await req.json()) as typeof body; } catch { return NextResponse.json({ error: 'invalid json' }, { status: 400 }); } const boardId = typeof body.boardId === 'string' ? body.boardId.trim() : ''; const imageUrl = typeof body.imageUrl === 'string' ? body.imageUrl.trim() : ''; if (!boardId || !imageUrl) { return NextResponse.json({ error: 'boardId and imageUrl are required' }, { status: 400 }); } try { const pin = await createPin({ boardId, imageUrl, title: typeof body.title === 'string' ? body.title : undefined, description: typeof body.description === 'string' ? body.description : undefined, link: typeof body.link === 'string' ? body.link : undefined, altText: typeof body.altText === 'string' ? body.altText : undefined, }); console.log(`[pinterest/pin] ${user.email} pinned ${imageUrl} → board ${boardId} (pin ${pin.id})`); return NextResponse.json({ ok: true, pin }); } catch (e) { const msg = e instanceof Error ? e.message : 'create-pin failed'; console.error('[pinterest/pin]', msg); return NextResponse.json({ error: msg }, { status: 502 }); } }