import { auth } from "@clerk/nextjs/server"; import { NextRequest, NextResponse } from "next/server"; import { claimCheckoutOrder } from "@/lib/checkout-order-claim"; export async function POST(req: NextRequest): Promise { const { userId } = await auth(); if (!userId) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } let body: { sessionId?: string; templateId?: string }; try { body = (await req.json()) as { sessionId?: string; templateId?: string }; } catch { return NextResponse.json({ error: "Invalid request body" }, { status: 400 }); } const sessionId = body.sessionId ?? ""; const templateId = body.templateId ?? ""; if (!sessionId || !templateId) { return NextResponse.json({ error: "sessionId and templateId are required" }, { status: 400 }); } const result = await claimCheckoutOrder(userId, sessionId, templateId); if (!result.ok) { return NextResponse.json({ error: result.error }, { status: result.status }); } return NextResponse.json({ ok: true, orderId: result.orderId }); }