/** * POST /api/profile/phone * * Saves an optional phone number to the user's Clerk profile. * Called from /activate/[orderId] — never required, always skippable. * * Body: { phone: string } */ import { auth, clerkClient } from "@clerk/nextjs/server"; import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest): Promise { const { userId } = await auth(); if (!userId) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } let body: { phone?: string }; try { body = (await req.json()) as { phone?: string }; } catch { return NextResponse.json({ error: "Invalid request body" }, { status: 400 }); } const phone = body.phone?.trim(); if (!phone) { return NextResponse.json({ error: "phone is required" }, { status: 400 }); } try { const client = await clerkClient(); await client.users.updateUserMetadata(userId, { publicMetadata: { whatsappPhone: phone }, }); return NextResponse.json({ ok: true }); } catch (err) { const msg = err instanceof Error ? err.message : "Failed to update profile"; return NextResponse.json({ error: msg }, { status: 500 }); } }