import { NextResponse } from "next/server"; import { prisma } from "@/lib/db"; import { authErrorResponse } from "@/lib/auth/api"; import { requireSessionUser } from "@/lib/auth/session"; import { requireActiveThreadsAuth } from "@/lib/services/threads-auth-guard"; import { replyViaThreadsApi } from "@/lib/threads-api"; import { getActiveThreadsCredentials } from "@/lib/services/threads-credentials"; export async function POST( _request: Request, { params }: { params: Promise<{ id: string }> } ) { try { await requireSessionUser(); const authCheck = await requireActiveThreadsAuth(); if (!authCheck.ok) { return NextResponse.json({ error: authCheck.error }, { status: 401 }); } const { id } = await params; const draft = await prisma.replyDraft.findUnique({ where: { id }, include: { inboundReply: true }, }); if (!draft) return NextResponse.json({ error: "找不到回覆草稿" }, { status: 404 }); if (!draft.inboundReply.externalId) { return NextResponse.json({ error: "此留言缺少 Threads reply id,無法 API 回覆" }, { status: 400 }); } const credentials = await getActiveThreadsCredentials(); if (!credentials) return NextResponse.json({ error: "Threads API 尚未連線" }, { status: 401 }); const result = await replyViaThreadsApi(credentials, { replyToId: draft.inboundReply.externalId, text: draft.text, }); if (!result.success) { return NextResponse.json({ error: result.error ?? "發布回覆失敗" }, { status: 500 }); } await prisma.$transaction([ prisma.replyDraft.update({ where: { id }, data: { status: "PUBLISHED", publishedAt: new Date() }, }), prisma.inboundReply.update({ where: { id: draft.inboundReplyId }, data: { status: "REPLIED" }, }), ]); return NextResponse.json({ success: true, result }); } catch (error) { const authRes = authErrorResponse(error); if (authRes) return authRes; const message = error instanceof Error ? error.message : "發布回覆失敗"; return NextResponse.json({ error: message }, { status: 500 }); } }