export const runtime = "nodejs"; import { NextResponse } from "next/server"; import { z } from "zod"; import { getCustomerByPortalToken, appendCustomerReply } from "@/lib/tickets/service"; const schema = z.object({ token: z.string().min(1), body: z.string().min(1).max(10_000), }); export async function POST( request: Request, { params }: { params: Promise<{ ticketId: string }> }, ) { const { ticketId } = await params; const body = await request.json().catch(() => null); const parsed = schema.safeParse(body); if (!parsed.success) { return NextResponse.json({ error: "Invalid input" }, { status: 400 }); } const customer = await getCustomerByPortalToken(parsed.data.token); if (!customer) { return NextResponse.json({ error: "Invalid token" }, { status: 404 }); } try { const result = await appendCustomerReply({ ticketId, customerId: customer.id, authorName: customer.displayName, body: parsed.data.body, }); return NextResponse.json(result, { status: 201 }); } catch { return NextResponse.json({ error: "Ticket not found" }, { status: 404 }); } }