import { NextRequest, NextResponse } from "next/server"; import { authenticateAPIRequest } from "@/app/lib/api-auth"; import { isAdmin } from "@/app/lib/permissions"; import { db } from "@/db"; import { user } from "@/db/schema"; import { eq } from "drizzle-orm"; /** * PATCH /api/v1/admin/users/[id]/verification * Kullanıcının email doğrulamasını değiştir (Sadece admin - JWT) */ export async function PATCH( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const auth = await authenticateAPIRequest(request); if (!auth.authenticated) { return NextResponse.json({ error: auth.error }, { status: 401 }); } // Admin kontrolü if (!isAdmin(auth.role!)) { return NextResponse.json( { error: "Bu işlem için yetkiniz yok. Sadece adminler doğrulama değiştirebilir." }, { status: 403 } ); } try { const { id: userId } = await params; const body = await request.json(); const { emailVerified } = body; // Boolean validasyonu if (typeof emailVerified !== "boolean") { return NextResponse.json( { error: "emailVerified boolean olmalıdır" }, { status: 400 } ); } // Email doğrulama durumunu güncelle const result = await db .update(user) .set({ emailVerified }) .where(eq(user.id, userId)) .returning(); if (result.length === 0) { return NextResponse.json({ error: "Kullanıcı bulunamadı" }, { status: 404 }); } return NextResponse.json({ success: true, message: `Email doğrulama ${emailVerified ? "aktif edildi" : "pasif edildi"}`, data: { userId, emailVerified, }, }); } catch (error: any) { console.error("Email doğrulama güncelleme hatası:", error); return NextResponse.json( { error: "Email doğrulama güncellenemedi" }, { status: 500 } ); } }