Lumen & Gesso / website · lumengesso.com / app / api / admin / pinterest / pin / route.ts
| 1 | import { NextResponse, type NextRequest } from 'next/server'; |
| 2 | import { getCustomerUser } from '@/app/lib/lumen-auth'; |
| 3 | import { isAdmin } from '@/app/lib/admin'; |
| 4 | import { createPin } from '@/app/lib/pinterest'; |
| 5 | |
| 6 | export const runtime = 'nodejs'; |
| 7 | export const dynamic = 'force-dynamic'; |
| 8 | |
| 9 | // Admin-gated. Body: { boardId, imageUrl, title?, description?, link?, altText? }. |
| 10 | // Returns the created pin ({ id, url? }) or { error }. |
| 11 | export async function POST(req: NextRequest) { |
| 12 | const user = await getCustomerUser(); |
| 13 | if (!user || !isAdmin(user.email)) { |
| 14 | return NextResponse.json({ error: 'forbidden' }, { status: 403 }); |
| 15 | } |
| 16 | |
| 17 | let body: { |
| 18 | boardId?: unknown; |
| 19 | imageUrl?: unknown; |
| 20 | title?: unknown; |
| 21 | description?: unknown; |
| 22 | link?: unknown; |
| 23 | altText?: unknown; |
| 24 | }; |
| 25 | try { |
| 26 | body = (await req.json()) as typeof body; |
| 27 | } catch { |
| 28 | return NextResponse.json({ error: 'invalid json' }, { status: 400 }); |
| 29 | } |
| 30 | const boardId = typeof body.boardId === 'string' ? body.boardId.trim() : ''; |
| 31 | const imageUrl = typeof body.imageUrl === 'string' ? body.imageUrl.trim() : ''; |
| 32 | if (!boardId || !imageUrl) { |
| 33 | return NextResponse.json({ error: 'boardId and imageUrl are required' }, { status: 400 }); |
| 34 | } |
| 35 | |
| 36 | try { |
| 37 | const pin = await createPin({ |
| 38 | boardId, |
| 39 | imageUrl, |
| 40 | title: typeof body.title === 'string' ? body.title : undefined, |
| 41 | description: typeof body.description === 'string' ? body.description : undefined, |
| 42 | link: typeof body.link === 'string' ? body.link : undefined, |
| 43 | altText: typeof body.altText === 'string' ? body.altText : undefined, |
| 44 | }); |
| 45 | console.log(`[pinterest/pin] ${user.email} pinned ${imageUrl} → board ${boardId} (pin ${pin.id})`); |
| 46 | return NextResponse.json({ ok: true, pin }); |
| 47 | } catch (e) { |
| 48 | const msg = e instanceof Error ? e.message : 'create-pin failed'; |
| 49 | console.error('[pinterest/pin]', msg); |
| 50 | return NextResponse.json({ error: msg }, { status: 502 }); |
| 51 | } |
| 52 | } |