first commit

This commit is contained in:
Beyhan Oğur
2026-04-26 22:09:32 +03:00
commit 71eff2d979
78 changed files with 10173 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/lib/auth";
import { hasPermission, PERMISSIONS } from "@/app/lib/permissions";
import { db } from "@/db";
import { user, images, apiKeys } from "@/db/schema";
import { eq } from "drizzle-orm";
/**
* DELETE /api/admin/users/[id]
* Kullanıcıyı sil (Sadece admin - Web Session)
* Kullanıcının tüm resimleri ve API anahtarları da silinir
*/
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session?.user) {
return NextResponse.json({ error: "Giriş yapmalısınız" }, { status: 401 });
}
// Permission kontrolü
const userRole = (session.user as any).role || "user";
if (!hasPermission(userRole, PERMISSIONS.USER_DELETE)) {
return NextResponse.json(
{ error: "Bu işlem için yetkiniz yok. Sadece adminler kullanıcı silebilir." },
{ status: 403 }
);
}
try {
const { id: userId } = await params;
// Kendi hesabını silmeyi engelle
if (userId === session.user.id) {
return NextResponse.json(
{ error: "Kendi hesabınızı silemezsiniz" },
{ status: 400 }
);
}
// Kullanıcının var olup olmadığını kontrol et
const targetUser = await db.select().from(user).where(eq(user.id, userId)).limit(1);
if (targetUser.length === 0) {
return NextResponse.json({ error: "Kullanıcı bulunamadı" }, { status: 404 });
}
// Kullanıcının resimlerini sil
await db.delete(images).where(eq(images.userId, userId));
// Kullanıcının API anahtarlarını sil
await db.delete(apiKeys).where(eq(apiKeys.userId, userId));
// Kullanıcıyı sil
await db.delete(user).where(eq(user.id, userId));
return NextResponse.json({
success: true,
message: "Kullanıcı başarıyla silindi",
data: {
deletedUserId: userId,
deletedUser: targetUser[0].email,
},
});
} catch (error: any) {
console.error("Kullanıcı silme hatası:", error);
return NextResponse.json(
{ error: "Kullanıcı silinemedi" },
{ status: 500 }
);
}
}